-
Вебмастеру
(
http://forum.oszone.net/forumdisplay.php?f=22)
leonid.poydolov@fb |
02-12-2014 16:09 2438659 |
PHP - Добавить в парсер замену url
Подскажите в какую строку подставить "str_replace" чтобы подменить "yandex.st/weather/1.2.83/i" на "localhost" ?
PHP код:
$content = str_replace("yandex.st/weather/1.2.83/i", "localhost", $content);
PHP код:
<?php $url = 'http://pogoda.yandex.ru/moscow/details'; //загрузит
$beginBlock = '<div class=\"b-navigation-city\">'; $endBlock = '<div class=\"b-print b-noprint\">';
$cachefile = dirname(__FILE__).'/cache/weather.php'; // файл кеша
$cache_time_config=7200; //время актуальности кеша 2 часа //Проверяем наличие кеша if(file_exists($cachefile)){ $cachetime = filemtime($cachefile); // время создание кеша if ((time() - $cachetime) < $cache_time_config) { //если кеш не устарел - берем значение из него $cache_used=true; include $cachefile; } }
if(!$cache_used) // если не использовали кеш - заправшиваем страницу с сервера погоды { $pattern = '%' . $beginBlock . '(.*)' . $endBlock . '%isU';
$fp = @fopen($url, 'r'); if (!$fp) exit(); $data = ''; while(!feof($fp)) { $data .= fgets($fp, 8192); } fclose($fp); if (!preg_match($pattern, $data, $matches)) // страница не доступна { if(file_exists($cachefile)){ // берем из устаревшего кеша данные include $cachefile; } else // кеш отсутствует { echo 'Сервер погоды временно недоступен'; } } else // сохраняем в кеш новое значение {
file_put_contents($cachefile,'<?php $matches='.var_export($matches,1).'; ?>'); } }
echo isset($matches[1]) ? $matches[1] : ''; ?>
|
leonid.poydolov@fb, делайте замену после получения данных:
PHP код:
<?php $url = 'http://pogoda.yandex.ru/moscow/details'; //загрузит $beginBlock = '<div class=\"b-navigation-city\">'; $endBlock = '<div class=\"b-print b-noprint\">'; $cachefile = dirname(__FILE__) . '/cache/weather.php'; // файл кеша $cache_time_config = 7200; //время актуальности кеша 2 часа //Проверяем наличие кеша if (file_exists($cachefile)) { $cachetime = filemtime($cachefile); // время создание кеша if ((time() - $cachetime) < $cache_time_config) //если кеш не устарел - берем значение из него { $cache_used = true; include $cachefile; } } if (!$cache_used) // если не использовали кеш - заправшиваем страницу с сервера погоды { $pattern = '%' . $beginBlock . '(.*)' . $endBlock . '%isU'; $fp = @fopen($url, 'r'); if (!$fp) exit(); $data = ''; while (!feof($fp)) { $data .= fgets($fp, 8192); } fclose($fp); $data = str_replace("yandex.st/weather/1.2.83/i", "localhost", $data); if (!preg_match($pattern, $data, $matches)) // страница не доступна { if (file_exists($cachefile)) // берем из устаревшего кеша данные { include $cachefile; } else // кеш отсутствует { echo 'Сервер погоды временно недоступен'; } } else // сохраняем в кеш новое значение { file_put_contents($cachefile, '<?php $matches=' . var_export($matches, 1) . '; ?>'); } } echo isset($matches[1]) ? $matches[1] : '';
|
Время: 21:06.
© OSzone.net 2001-