J
J
Justin Bieber2016-02-26 14:03:08
Yandex
Justin Bieber, 2016-02-26 14:03:08

Why can the parser go wrong?

I assembled a simple weather parser from Yandex, but there was such a problem:
d6ovj6it.jpgd6ovj6pu.jpg
As you can see, it shows, then it doesn’t. Question because of what there can be such trabl? ;(

<?php 
$city_id = 35229; 
$data_file = "http://export.yandex.ru/weather-ng/forecasts/$city_id.xml"; 

$xml = simplexml_load_file($data_file); 

$city = $xml->fact->station; 
$temp = $xml->fact->temperature; 
$pic = $xml->fact->{'image-v3'}; 
$type = $xml->fact->weather_type; 

if ($temp > 0) { 
$temp = '+' . $temp; 
} 

echo ("Актобе"); 
echo (" $temp °C <img src=\"https://yandex.st/weather/v-1/i/icons/30x30/$pic.png\" alt=\"$type\" title=\"$type\">"); 

?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
theg4sh, 2016-03-22
@theg4sh

Ilya says the point, update the xml once a minute and parse the file, not the url, otherwise you will shoot at sparrows from a cannon in the hope of understanding the reason:
Imagine that 1000 people are accessing your host, and these 1000 people are in addition to downloading your site, they also make a request in the background before Yandex weather, Yandex will block you with so many requests per minute / second / other unit of measurement :)
In general, pull any data from external sites without caching and pre-processing - evil.
Just imagine how long your page will take to load if an external site suddenly becomes unavailable.

I
Ilya, 2016-03-03
@glebovgin

I suggest taking $data_file via curl() or at least file_get_contents(), cache it for a while, and parse it from a local copy.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question