0

I am having an intermittent problem in my website

https://americascareercenters.us/jobs.php?q=92630

Sometimes the page loads fine. other times I get just a blank page with an error.

Fatal error: Uncaught Error: Call to a member function attributes() on boolean in /home/acc/public_html/jobs.php:95 Stack trace: #0 {main} thrown in /home/acc/public_html/jobs.php on line 95

Here is the section of code

$rCurl_f = curl_init($sUrl_f);
curl_setopt($rCurl_f, CURLOPT_RETURNTRANSFER, TRUE);
$sXml_f = curl_exec($rCurl_f);
curl_close($rCurl_f);
$oXml_f = simplexml_load_string($sXml_f);

$results_atributes_f = $oXml_f->attributes();
$total_f = $results_atributes_f['total'];
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • 5
    `simplexml_load_string()` will return false if it fails to load the response, which is probably why you get the error. You need to check the result from curl see if it is returning the XML your expecting. – Nigel Ren Apr 25 '19 at 18:07
  • You can also use `libxml_get_errors()` to get the errors – khartnett Apr 25 '19 at 18:10
  • The xml seems to be ok. I have added the link to the xml response to the top of the page – Justin James Apr 25 '19 at 18:11
  • I just tried the link and got a set of PHP messages - no XML. – Nigel Ren Apr 25 '19 at 18:13
  • Then it looks like whatever script you're requesting with that curl request is sometimes having errors – Stevish Apr 25 '19 at 18:31
  • 1
    Possible duplicate of [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – miken32 Apr 26 '19 at 19:10

1 Answers1

0

Yeah, you need to check if $oXml_f is false before trying to use its method:

if ( ! empty( $oXml_f ) ) {
    $results_atributes_f = $oXml_f->attributes();
    $total_f = $results_atributes_f['total'];
} else {
    //Whatever you want to do if it fails
    //Maybe $total_f = 0;
}
Stevish
  • 734
  • 5
  • 17
  • I added that in, but the problem still remains. Sometimes the page loads and sometimes it doesn't. – Justin James Apr 25 '19 at 18:18
  • Ok, I see. You should pursue what @khartnett suggested and check errors, and maybe print out the XML you're getting back to see why it might be failing to parse properly – Stevish Apr 25 '19 at 18:28
  • I'd do this with `echo $sXml_f;` or something right before `$oXml_f = simplexml_load_string($sXml_f);` – Stevish Apr 25 '19 at 18:30