在PHP中使用cURL解析错误发布数据

时间:2019-04-09 00:32:00

标签: php curl post

我正尝试使用PHP和cURL将POST数据发送到API,如下所示:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://sandbox.api.myAPIsite.com/values?api_key=MyAPIKey");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);

$data = array(
    "configuration"=> { 
         "vehicleId"=> 382245, 
         "vehicleOptionIds"=> [5154080] 
    }, 
    "mileage"=> 100000, 
    "zipCode"=> "02101", 
    "valuationDate"=> "2019-04-08T23:59:21.803Z" 
);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
$info = curl_getinfo($ch); 

但是,日志显示已发生解析错误。解决该问题的正确语法是什么?

日志详细信息:

PHP Parse error:  syntax error, unexpected '{' in /home/78ds6f76f8fds6f/public_html/desktop/values.php on line 11

1 个答案:

答案 0 :(得分:1)

您正在(错误地)在'configuration'数组键内使用JavaScript的对象表示法而不是PHP的数组表示法,要解决此问题,只需将您的 $ data 数组更改为以下:

  $ data = array(
    “配置” => [
         “ vehicleId” => 382245,
         “ vehicleOptionIds” => [5154080]
    ],
    “ mileage” => 100000,
    “ zipCode” =>“ 02101”,
    “ valuationDate” =>“ 2019-04-08T23:59:21.803Z”
);