0

我有一个工作 CLI

curl -X POST \
-H "X-Parse-Application-Id: ID" \
-H "X-Parse-REST-API-Key: KEY" \
-H "Content-Type: application/json" \
-d '{
    "channels": [
      "Giants",
      "Mets"
    ],
    "data": {
      "alert": "The Giants won against the Mets 2-3."
    }
  }' \
https://api.parse.com/1/push

返回一个字符串{"result":"success"}

但是我的 php curl

$post = json_encode(array('channels'=>array('Giants','Mets'),'data'=>array('alert'=>'The Giants won against the Mets 2-3')));
$ch = curl_init();
curl_setopt_array($ch, array(
            CURLOPT_URL             =>  'https://api.parse.com/1/push',
            CURLOPT_HTTPHEADER      =>  array(
                                            'X-Parse-Application-Id: ID',
                                            'X-Parse-REST-API-Key: KEY',
                                            'Content-Type: application/json'
                                        ),
            CURLOPT_POST            =>  true,
            CURLOPT_POSTFIELDS      =>  $post,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_RETURNTRANSFER => true
        ));
$res = curl_exec($ch);

if (curl_error($ch)) {
     echo "Curl error: " . curl_error($ch);
}
curl_close($ch);
echo $res;

显示消息“您要查找的页面不存在”。然后在其下方的“1”是$res没有错误的

谢谢

4

3 回答 3

1

您正在使用相同的密钥发送多个标头,因此每个标头都会清除前一个。您必须将标头作为数组发送:

CURLOPT_HTTPHEADER => array(
    'Content-type: text/plain',
    'Content-length: 100',
    '...'
)
于 2012-11-02T19:02:18.650 回答
0

您在数组中传递相同的密钥:“CURLOPT_HTTPHEADER”

请试试:

curl_setopt_array($ch, array(
  CURLOPT_URL         =>  'https://api.parse.com/1/push',
  CURLOPT_HTTPHEADER      =>  "X-Parse-Application-Id: ID\n" .
        "X-Parse-REST-API-Key: KEY\n" .
        "Content-Type: application/json",
  CURLOPT_POST        =>  true,
  CURLOPT_POSTFIELDS  =>  $post,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_RETURNTRANSFER => true
));
于 2012-11-02T19:01:19.247 回答
0

请试试:

curl_setopt_array($ch, array(
      CURLOPT_URL         =>  'https://api.parse.com/1/push',
      CURLOPT_HTTPHEADER      =>  "X-Parse-Application-Id: ID\n" .
            "X-Parse-REST-API-Key: KEY\n" .
            "Content-Type: application/json",
      CURLOPT_POST        =>  true,
      CURLOPT_POSTFIELDS  =>  $post,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_SSL_VERIFYPEER => false//not SSL verification
    ));
于 2015-09-06T05:48:48.993 回答