1

Can someone possibly tell me what's wrong with this? I've never tried to post multidim array over curl/php, any help would be mega appreciated.

Getting bad response from server, I think they're getting some of the data but their response isn't helpful. Trying to build on their code, so they won't help with this kind of thing. (it's not against their TOS, it's just something they don't support)

Also, I know the cookie files/auth are working because I do this on another function just fine, except that one isn't multidimensional.

$post_data = array (
    'tradeOrder' => array (
        'FirstOrder' => array (
            'Legs' => array (
                'Id' => '0',
                'SecurityId' => '643',
                'SecurityName' => 'AAPL',
                'SecurityExchange' => 'NASDAQ national market',
                'Side' => 'Buy',
                'Quantity' => '100'
            ),
            'SymbolLastPrice' => '93.72',
            'Price' => '93.75',
            'StopPrice' => '0.01',
            'Type' => 'Limit',
            'TimeInForce' => 'Day',
            'AllOrNone' => 'false',
            'Exchange' => 'AUTO',
            'TrailingAmount' => '0.01',
            'TrailingAmountType' => 'Absolute',
            'LimitOffset' => '0',
            'LimitOffsetType' => 'Absolute',
            'IsTrailingVisible' => 'false',
            'IsLimitOffsetVisible' => 'false',
            'IsTrailingAmountAbsolute' => 'true',
            'IsLimitOffsetAbsolute' => 'true',
            'ExecutionTarget' => 'New',
        ),
    'AdvancedOrderType' => 'Simple',
    'IsAdvancedOrder' => 'false'
    )
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://demo-trading.just2trade.com/TradeTicket/PlaceOrder");
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
//curl_setopt($ch, CURLOPT_HEADER, true);
//curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer ' . $result_array['access_token']]);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
curl_setopt($ch, CURLOPT_ENCODING,'gzip');
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

Forms I'm trying to post as follows

{
    "tradeOrder": {
        "FirstOrder": {
            "Legs": [{
                "Id": 0,
                "SecurityId": 643,
                "SecurityName": "AAPL",
                "SecurityExchange": "NASDAQ national market",
                "Side": "Buy",
                "Quantity": 100
            }],
            "SymbolLastPrice": 93.72,
            "Price": 93.72,
            "StopPrice": 0.01,
            "Type": "Limit",
            "TimeInForce": "Day",
            "AllOrNone": false,
            "Exchange": "AUTO",
            "TrailingAmount": 0.01,
            "TrailingAmountType": "Absolute",
            "LimitOffset": 0,
            "LimitOffsetType": "Absolute",
            "IsTrailingVisible": false,
            "IsLimitOffsetVisible": false,
            "IsTrailingAmountAbsolute": true,
            "IsLimitOffsetAbsolute": true,
            "ExecutionTarget": "New"
        },
        "AdvancedOrderType": "Simple",
        "IsAdvancedOrder": false
    }
} 
3
  • 1
    Your question is not about multidimensional array, but about JSON string. Are you sure that your JSON matches API rules? Commented May 1, 2016 at 23:34
  • Do you have a curl error ie did Curl fail or did the application using it fail. Have a look here php.net/manual/en/function.curl-error.php. Use that to get the code from curl after you make the call. Also, what HTTP response code did you get from the server, 200, 4nn, 5nn etc? Commented May 1, 2016 at 23:34
  • No Curl problems, and 200 response. Commented May 1, 2016 at 23:59

2 Answers 2

1

If you want to post exactly provided JSON sample, main error is in this line:

'Legs' => array ( ... ),

This code produce this JSON:

{"FirstOrder":{"Legs":{"Id":"0",...}},...}

instead of:

{"FirstOrder":{"Legs":[{"Id":"0",...}]},...}

Change the “Legs” line in this way:

'Legs' => array( array(
    'Id' => '0',
    'SecurityId' => '643',
    'SecurityName' => 'AAPL',
    'SecurityExchange' => 'NASDAQ national market',
    'Side' => 'Buy',
    'Quantity' => '100'
)),

Also (although I don't think that this can cause issues), if you want numbers encoded as numbers and booleans encoded as booleans, remove relative wrapping quotes:

'SymbolLastPrice' => 93.72,
(...)
'AllOrNone' => false,
(...)
'LimitOffset' => 0,
(...)
Sign up to request clarification or add additional context in comments.

Comments

0

Your terminology is somewhat confused. Neither PHP arrays nor JSON are ever multi-dimensional; they are nested. Further no HTML form produces JSON encoded data. And, yes, this is an API.

Leaving the comments aside, only the service provider can give you a definitive answer as to why your content is not acceptable, however the boolean values in your JSON example have been defined as strings in your PHP code, e.g.

    'IsTrailingAmountAbsolute' => 'true'

Should be

     'IsTrailingAmountAbsolute' => true

1 Comment

I meant it's not an API as in public API with documentation. I guess it is their internal API, so you are right.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.