Php 如何在curl post中使用变量?
我正在创建一个php脚本,它将使用curl在解析数据库中添加一个新行。我在向setopt语句添加变量时遇到困难。如果有人能给我指引正确的方向,我将不胜感激 这是我执行curl语句的PHP代码:Php 如何在curl post中使用变量?,php,post,curl,parse-platform,Php,Post,Curl,Parse Platform,我正在创建一个php脚本,它将使用curl在解析数据库中添加一个新行。我在向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, arr
//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 );