如何在curl帖子中使用变量



我正在创建一个php脚本,该脚本将使用curl在我的Parse数据库中添加新行。我在向setopt语句添加变量时遇到困难。如果有人能把我带向正确的方向,我将不胜感激

这是我正在执行curl语句的PHP代码:

//curl commands to send information to parse
  $ch = curl_init('https://api.parse.com/1/classes/className');
curl_setopt($ch,CURLOPT_HTTPHEADER,
    array('X-Parse-Application-Id:secret1',
    'X-Parse-REST-API-Key:secret2',
    'Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS,  "{"Name":"$deviceName"}" );
echo curl_exec($ch);
curl_close($ch);

我得到的回复是:

{"代码":107,"错误":"无效JSON"}

提前谢谢!

在发送数据之前不能对数组进行编码有什么原因吗?

例如(未经测试):

$arr = [ "Name" => $deviceName ];
$arr_string = json_encode($arr);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($arr_string)
));
curl_setopt($ch, CURLOPT_POSTFIELDS, $arr_string );

最新更新