2012-11-02 240 views
0

我有一個工作CLI捲曲CLI到PHP捲曲

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捲曲

$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沒有錯誤

感謝

+0

[轉換命令行卷曲到PHP捲曲]可能重複(http://stackoverflow.com/questions/1939609/convert-command-line-curl-to-php-curl) – Gajus

回答

1

您使用相同的密鑰發送多個標題,因此每個標題都會刪除以前的標題。你必須發送標題爲數組:

CURLOPT_HTTPHEADER => array(
    'Content-type: text/plain', 
    'Content-length: 100', 
    '...' 
) 
+0

感謝您的快速響應,我將其更改爲上述編輯的數組(男孩,我是否感覺像塗料),但仍然收到相同的「不存在」錯誤。它可以與SSL有關嗎? –

+0

我的問題是你提到的+未設置捲曲的SSL。謝謝 –

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 
)); 
+0

感謝您的快速響應,我將其更改爲上面編輯的數組(男孩,我覺得自己像一個塗料),但仍然收到「不存在「錯誤。它可以與SSL有關嗎? –

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