Jump to content

'Cannot read properties of undefined' error when trying to print a JSON response in PHP


Tyssen
 Share

Recommended Posts

Although not strictly a Processwire question, I am trying to do this on a PW site, so…

I have the following code:

$url = 'https://json-api-url-domain.com/matches?id=XX&division=YY&teams=[ZZ]';

$cURL = curl_init();

curl_setopt($cURL, CURLOPT_URL, $url);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Accept: application/json'
));

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

$json = json_decode($result, true);
var_dump($json);

But with that I'm getting:

array (size=2)
  'name' => string 'TypeError' (length=9)
  'message' => string 'Cannot read properties of undefined (reading 'includes')' (length=56)

I get the same message if I var_dump $result.

I can visit the API URL directly in a web browser and see the JSON response.

Any idea why that is and how do I fix it?

Link to comment
Share on other sites

Hi, the code seem good, and what I understand from the result, is that the request is ok, as you get a result, but as a result, the server tell you there was an error, and it seem that the error come from a javascript backend. You could try to run curl from cmd line, and compare response headers.

Are you sure you do not need CURLOPT_FOLLOWLOCATION ?

try to dump error:

$url = 'https://json-api-url-domain.com/matches?id=XX&division=YY&teams=[ZZ]';

$cURL = curl_init();
try {
  curl_setopt($cURL, CURLOPT_URL, $url);
  curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Accept: application/json'
  ));

  $result = curl_exec($cURL);
  if (curl_errno($cURL)) {
    echo curl_error($cURL);
    die();
  }

  $http_code = curl_getinfo($cURL, CURLINFO_HTTP_CODE);
  if ($http_code == intval(200)) {

    $json = json_decode($result, true);
    echo "Resource OK: ";
    var_dump($json);
  } else {
    echo "Resource NOTOK: " . $http_code;
  }
} catch (\Throwable $th) {
  throw $th;
} finally {
  curl_close($cURL);
}
die();

 

Link to comment
Share on other sites

Thanks @flydev I've tried your code and get a 500 error which I really don't understand.

Since posting this, I found https://reqbin.com/curl and when I test my API URL, it works fine and I can see the same output as if I visit the URL directly in a browser.

That site gives you the option to generate code in different languages. The PHP code it generates is essentially what I already have. If I use the js/AJAX code it suggests, I can see the response printed in the console. ?

Link to comment
Share on other sites

If you are throwing the request from a ProcessWire template, then please show us the code. The 500 error is quite strange there, you should at least get a pw exception if $debug is true, and in reality, you shouldn't get a 500 error. (The code posted above run on PW with php-7/8 printing curl error if one exist)

Gve more details on your setup ? 

  

6 hours ago, Tyssen said:

I am trying to do this on a PW site

The site is the source or the target ?

Link to comment
Share on other sites

I've moved it out of PW for now and it's just a plain PHP file in the root of the public folder. I wanted to eliminate PW as being a possible cause of the problem. So what I've posted so far is literally all there is.

Link to comment
Share on other sites

5 minutes ago, flydev said:

The site is the source or the target ?

The PW site will ultimately be the target. Once I have access to the data in PHP I'll be using it as a variable which will then be handed off to Twig templates.

The source is already in JSON format.

Link to comment
Share on other sites

Ok, then try the following, you need to know what's the error. Which version of PHP you are running the code on ?

Also, checking the target logs might give you more hints on what's is missing. A cookie maybe ?

$url = 'https://json-api-url-domain.com/matches?id=XX&division=YY&teams=[ZZ]';

$cURL = curl_init();

curl_setopt($cURL, CURLOPT_URL, $url);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
  'Content-Type: application/json',
  'Accept: application/json'
));

$result = curl_exec($cURL);
if (curl_errno($cURL)) {
  echo curl_error($cURL);
  die();
}

$http_code = curl_getinfo($cURL, CURLINFO_HTTP_CODE);
if ($http_code == intval(200)) {

$json = json_decode($result, true);
  echo "Resource OK: ";
  var_dump($json);
} else {
  echo "Resource NOTOK: " . $http_code;
}

die();

 

Link to comment
Share on other sites

And to be sure, you can try the code above on a mock server:

// $url = 'https://json-api-url-domain.com/matches?id=XX&division=YY&teams=[ZZ]';
$url = 'https://my-json-server.typicode.com/typicode/demo/comments';

With pw, or not - should give you a resource ok message, a 200 http code, with an array of two objects as a result. 

Link to comment
Share on other sites

2 minutes ago, flydev said:

And to be sure, you can try the code above on a mock server:

// $url = 'https://json-api-url-domain.com/matches?id=XX&division=YY&teams=[ZZ]';
$url = 'https://my-json-server.typicode.com/typicode/demo/comments';

With pw, or not - should give you a resource ok message, a 200 http code, with an array of two objects as a result. 

Yes, I get successful output with that. Still get 500 with your updated code on my URL.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...