0
votes

I am trying to execute a curl command that I can execute successfully in the terminal but it fails in PHP script with the following error:

HTTP/1.1 401 Unauthorized Access-Control-Allow-Origin: * Access-Control-Request-Method: * Cache-Control: no-cache Content-Type: application/json; charset=utf-8 Date: Mon, 06 Feb 2012 00:38:56 GMT Server: nginx/1.0.4 Set-Cookie: _parse_session=XXXXXX; domain=.parse.com; path=/; expires=Sun, 06-Feb-2022 00:38:56 GMT; HttpOnly Status: 401 Unauthorized WWW-Authenticate: Basic realm="Parse" X-Runtime: 0.002486 X-UA-Compatible: IE=Edge,chrome=1 Content-Length: 24 Connection: keep-alive {"error":"unauthorized"}

This is the command executed in terminal that executes successfully:

curl -H "Accept: application/json" -H "X-Parse-Application-Id: XXXXXXX" -H "X-Parse-REST-API-Key: XXXXXXXXX" -X GET "https://api.parse.com/1/classes/XXXXXX"

This is the PHP code:

$fields = array('Accept: '=>'application/json',
            'X-Parse-Application-Id:' => 'XXXXX',
            'X-Parse-REST-API-Key:' => 'XXXXX');

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://api.parse.com/1/classes/XXXX');

curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_HEADER, $fields);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt(CURLOPT_USERPWD, 'XXXXXX');

$result = curl_exec($ch);
curl_close($ch);

The interesting note is that when executed in the terminal authentication info is not required.

Help will be appreciated.

Thanks

2

2 Answers

5
votes

Your header fields shouldn't have the : in the array defnition:

'X-Parse-REST-API-Key:' => 'XXXXX');
                     ^---remove these

That makes the : part of the field name, so you're actually sending:

X-Parse-REST-API-Key:: XXXXX
                    ^^---note the doubled colons
0
votes

Your header is set using CURLOPT_HTTPHEADER, not CURLOPT_HEADER which expects a boolean value