Trying to get property 'Currency' of non-object in

Hello,

I get this error from time to time how can I fix it

$ch = curl_init(); 
curl_setopt ($ch, CURLOPT_URL, "https://www.tcmb.gov.tr/kurlar/today.xml") or die ('TCMB\'na baglanamadi'); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec ($ch); 
curl_close($ch);

$xml= simplexml_load_string($result);

if (is_array($xml->Currency) || is_object($xml->Currency)) // Error is here
{

Sometimes a cURL command will fail. It sometimes is due to the site being loaded to fast or just an internet glitch. Once you get the page, you should check to see if you actually got the page. You can add error checking to your cURL call. Once you EXEC the cURL, check if it exist. like this:

$result = curl_exec($ch);
if( ! $result ) {
  //  Page retrieval failed, do whatever...
} else {
  //  Page was retieved, now do your XML section...
}

Just an example. If the page is not loaded, the $result will be empty and therefore you should not bother trying to handle the data. The data would not be there and you would get a “non-object”. Often, in the fail section, you would post a notice to try again. Some people just place a second cURL call to retry it. I have found that sometimes, you need to pause the code for a second before calling the page again. you can do that with a sleep(1) command. But, this usually is only needed if you make several cURL’s one after another. Another thing that can cause this is the remote site being overwhelmed with requests.

I got it very well @ErnieAlex,
I don’t need to do anything for it to work again
Because Task Scheduler will query again if Exchange Rate has not been updated.
Thank you so much

Sponsor our Newsletter | Privacy Policy | Terms of Service