json curl在php中显示http错误500

时间:2016-06-11 06:56:58

标签: php json curl

我将以下内容作为json输出:

{"productName":"Provision items","items":[
{"Tea":"milo","price1":1242,"price2":1500},
{"milk":"Cowmilk","price1":8031,"price2":8922},
{"sugar":"st. louis","price1":9062,"price2":9852}]}

我尝试使用下面的代码发回数据以进行更新。它显示错误:

  

mysite.com页面无效   mysite.com目前无法处理此请求。   HTTP ERROR 500。

我认为错误在于item属性的内容。有人可以帮我解决这个问题。感谢

代码     

$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 个答案:

答案 0 :(得分:1)

上面的php代码片段不是有效的php。数组初始化应该像这样完成:

$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)
);

请参阅 http://php.net/manual/en/language.types.array.php 了解更多详情