json curl in php displaying http error 500 -
i have below json
output:
{"productname":"provision items","items":[ {"tea":"milo","price1":1242,"price2":1500}, {"milk":"cowmilk","price1":8031,"price2":8922}, {"sugar":"st. louis","price1":9062,"price2":9852}]}
here trying post data make updates using code below. displays error:
the mysite.com page isn’t working mysite.com unable handle request. http error 500.
i think error lies within content of items properties. can me fix that. thanks
code
$data = array("productname" => "new updates of provision items", "items" =>[{"tea":"milo","price1":1242,"price2":1500}, {"milk":"cowmilk","price1":8031,"price2":8922}, {"sugar":"st. louis","price1":9062,"price2":9852}] ); $data_string = json_encode($data); $ch = curl_init("mysite.com/api"); curl_setopt($ch, curlopt_customrequest, "post"); curl_setopt($ch, curlopt_postfields, $data_string); curl_setopt($ch, curlopt_returntransfer, true); curl_setopt($ch, curlopt_httpheader, array( 'content-type: application/json')); $result=curl_exec($ch); curl_close($ch); echo 'provision item updates<br>'; echo '<pre>' . print_r($result, true) . '</pre>'; ?>
the php code snippet above not valid php. array initialization should done this:
$data = array("productname" => "new updates of provision items", "items" =>array("tea"=>"milo","price1"=>1242,"price2"=>1500), array("milk"=>"cowmilk","price1"=>8031,"price2"=>8922), array("sugar"=>"st. louis","price1"=>9062,"price2"=>9852) );
see http://php.net/manual/en/language.types.array.php more details
Comments
Post a Comment