Sven Kahn Sven Kahn - 7 months ago 27
PHP Question

PHP CURL Multidimensional/JSON Post

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
}
}

Answer

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,
(...)