2

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>';




?>

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

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.