J
J
Justin Bieber2016-06-28 07:33:28
PHP
Justin Bieber, 2016-06-28 07:33:28

Why if the source does not work during parsing, then the site does not work either?

We parse the exchange rate from the National Bank, yesterday the website of the National Bank did not work, and because of this, the site that parses stops working.
If, for example, you specify the source incorrectly, then the site shows zeros. Why, if the source was not available, did it not show zeros on the site?

<?php  
     
    $sContent  = file_get_contents("http://www.nationalbank.kz/rss/rates_all.xml", "r");
    $oXml = simplexml_load_string($sContent);
    $iUSD = 0;
    $iRUR = 0;
    $iEUR = 0;
    foreach($oXml->channel->item AS $oValue) { 
        if ($oValue->title == "USD") { 
            $iUSD = $oValue->description; 
        } 
        if ($oValue->title == "RUB") { 
            $iRUB = $oValue->description; 
        } 
        if ($oValue->title == "EUR") { 
            $iEUR = $oValue->description; 
        } 
    } 
     
    echo "<span>USD " . $iUSD . "</span>";
    echo "<span>RUB " . $iRUB . "</span>";
    echo "<span>EUR " . $iEUR . "</span>";
?>

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexey Ukolov, 2016-06-28
@alexey-m-ukolov

Read the logs, everything is written there.

X
xmoonlight, 2016-06-28
@xmoonlight

because it is in the same thread, and there the execution of the code is always asynchronous.
1. parse and cache.
2. handle errors
3. use cache on errors

N
neol, 2016-06-28
@neol

Because default_socket_timeout in PHP is 60 (seconds) and file_get_contents maximum execution time depends on this setting. Accordingly, if the site from which you pull the information is down, then requests to your site return a response only after this timeout has expired.
Any PHP code that interacts with the network should be placed in tasks executed via cli via cron (although this is not always possible, for example, when authorizing through a third-party service), and cached results should be displayed on the site. At the same time, it is worth doing checks that this code will not run 100500 times.
Plus, you don't have to check that file_get_contents and simplexml_load_string return the correct result.

Оптимус Пьян, 2016-06-28
@marrk2

Завершается скрипт наверное когда данных нет.
Поставьте $sContent = @file_get_contents или в if оберните

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question