0

I have a working 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

which returns a string {"result":"success"}

But my 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;

shows the message "The page you were looking for doesn't exist." and then below that a "1" which is the $res with no error

Thanks

Andrew Park
  • 1,489
  • 1
  • 17
  • 26
  • possible duplicate of [Convert command line cURL to PHP cURL](http://stackoverflow.com/questions/1939609/convert-command-line-curl-to-php-curl) – Gajus Aug 10 '14 at 14:17

3 Answers3

1

You're sending multiple headers with the same key, thus each is wiping out the previous. You have to send the headers as an array:

CURLOPT_HTTPHEADER => array(
    'Content-type: text/plain',
    'Content-length: 100',
    '...'
)
Alex Howansky
  • 50,515
  • 8
  • 78
  • 98
  • Thanks for the quick response, I changed it to an array (boy, do I feel like a dope) as edited above but still receiving the same "doesn't exist" error. Could it have something do with SSL? – Andrew Park Nov 02 '12 at 19:18
  • My problems were what you mentioned + not setting the SSL for curl. Thanks – Andrew Park Nov 02 '12 at 23:19
0

you are in the array pass the same key: "CURLOPT_HTTPHEADER"

Please try:

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
));
ZhukV
  • 2,892
  • 6
  • 25
  • 36
  • Thanks for the quick response, I changed it to an array (boy, do I feel like a dope) as edited above but still receiving the same "doesn't exist" error. Could it have something do with SSL? – Andrew Park Nov 02 '12 at 22:38
0

Please try:

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
    ));
Aaron
  • 1
  • 1