【问题标题】:How to use variables in a curl post?如何在 curl 帖子中使用变量?
【发布时间】:2015-03-20 18:55:35
【问题描述】:

我正在创建一个 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);

我得到的回复是:

{"code":107,"error":"无效 JSON"}

提前谢谢你!

【问题讨论】:

    标签: php post curl parse-platform


    【解决方案1】:

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

    例如(未经测试):

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

    【讨论】:

      猜你喜欢
      • 2011-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-31
      • 1970-01-01
      • 2014-11-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多