-3

I am using the following:

<?php
$url = '...';
$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL => $url,
    CURLOPT_CUSTOMREQUEST => "GET"
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

$response = json_decode($response, true);
echo gettype($response); /$response/ THIS RETURNS INTEGER FOR SOME REASON!
?>

So the $response I am getting is of type integer, and I am unable to read the elements of this JSON.

Also note that when I echo the response, 1 is printed after it.

halfer
  • 19,824
  • 17
  • 99
  • 186
hello_its_me
  • 743
  • 2
  • 19
  • 52
  • can you print_r($response) and add it to your questions? – Tobias Gassmann May 07 '18 at 13:25
  • 1
    "THIS RETURNS INTEGER FOR SOME REASON!" — Presumably, because the JSON consists of an integer. What did you expect it to be? How about providing a [mcve] (which would need example input to be complete) – Quentin May 07 '18 at 13:26
  • I would like to repeat that I did not downvote your question – Tobias Gassmann May 07 '18 at 13:29
  • @TobiasGassmann okay, thanks. I dont understand what is provoking the downvoting. Is my question this inappropriate? Anyways, Yoshi seems to have helped me so thank you all. – hello_its_me May 07 '18 at 13:31
  • as Quentin wrote, include a MCVE in your question. without it the question is incomplete and subject of downvotes. – Andreas May 07 '18 at 13:39

2 Answers2

1

Check the manual for the possible return values. You need to at least use CURLOPT_RETURNTRANSFER.

Returns TRUE on success or FALSE on failure. However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, FALSE on failure.

<?php
$url = '...';
$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL => $url,
    CURLOPT_CUSTOMREQUEST => "GET",
    CURLOPT_RETURNTRANSFER => true  // <<<< add this
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

$response = json_decode($response, true);

if (json_last_error() !== JSON_ERROR_NONE) {
    var_dump(json_last_error_msg());
}
Yoshi
  • 54,081
  • 14
  • 89
  • 103
0

'$result' is not defined in your code, double check it? Perhaps the line "$response = json_decode($response, true);" needs to be changed. A default value for undefined variable is NULL/False

  • I edited my question. This isn't the issue, I am getting the type of $response not $result. – hello_its_me May 07 '18 at 13:26
  • 1
    The function curl_exec returns a true/false value, that is what is stored within $response. Is that what you wanted to check? See: http://php.net/manual/en/function.curl-exec.php – user3754362 May 07 '18 at 13:29