Cody Raspien
Cody Raspien

Reputation: 1835

curl with authentication header token - GET - PHP

I have a token (string held in $token) and I need to retrieve a response (JSON) from an online service using curl.

The API requires GET.

$crl = curl_init();

$headr = array();
$headr[] = 'Content-length: 0';
$headr[] = 'Content-type: application/json';
$headr[] = 'Authorization: OAuth '.$token;

curl_setopt($crl, CURLOPT_HTTPHEADER,$headr);
$url = "ENDPOINT"; 
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPGET, 1);

  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$rest = curl_exec($crl);

curl_close($crl);

var_dump($rest);

Dumping $rest gives me:

 bool(false)

I based the above on this thread

Upvotes: 0

Views: 4783

Answers (1)

segFault
segFault

Reputation: 4054

You are mixing variable names, ie. $crl and $ch, just clean that up and it should be fine or at least you should get something else besides false.

I would suggest you also enable error_reporting in your php.ini, you will easily find bugs like this in your code as you are developing.

In case it helps, this is what I am getting locally when running your code:

PHP Notice:  Undefined variable: ch in /home/sebastianforsberg/env/test.php on line 13
PHP Warning:  curl_setopt() expects parameter 1 to be resource, null given in /home/sebastianforsberg/env/test.php on line 13
PHP Notice:  Undefined variable: ch in /home/sebastianforsberg/env/test.php on line 14
PHP Warning:  curl_setopt() expects parameter 1 to be resource, null given in /home/sebastianforsberg/env/test.php on line 14
PHP Notice:  Undefined variable: ch in /home/sebastianforsberg/env/test.php on line 16
PHP Warning:  curl_setopt() expects parameter 1 to be resource, null given in /home/sebastianforsberg/env/test.php on line 16
bool(false)

Upvotes: 2

Related Questions