nackolysis
nackolysis

Reputation: 217

json curl in php displaying http error 500

I have this below as 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 am trying to post the data back to make an updates by using the code below. It displays error:

The mysite.com page isn’t working mysite.com is currently unable to handle this request. HTTP ERROR 500.

I think error lies within the content of items properties. Can someone help me to 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>';




?>

Upvotes: 2

Views: 237

Answers (1)

John Fawcett
John Fawcett

Reputation: 249

The php code snippet above is not valid php. The array initialization should be done like 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 for more details

Upvotes: 1

Related Questions