0

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

Community
  • 1
  • 1
Cody Raspien
  • 1,753
  • 5
  • 26
  • 51

1 Answers1

2

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)
miken32
  • 42,008
  • 16
  • 111
  • 154
segFault
  • 3,887
  • 1
  • 19
  • 31