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

5
  • What does echo curl_error($crl) say? Commented Jan 6, 2017 at 0:13
  • Error: No URL set! Commented Jan 6, 2017 at 0:15
  • This is odd - the URL was set in $url Commented Jan 6, 2017 at 0:16
  • You have a real URL, not just the word ENDPOINT, right? Commented Jan 6, 2017 at 0:20
  • Yes, real URL that requires a token to be passed in the header in order to output a JSON response. I just wrote ENDPOINT in the post because the URL is not public. Commented Jan 6, 2017 at 0:21

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.