возвращая URL вместо изображения - извлечение данных из XML с помощью регулярного выражения PHP - PullRequest
0 голосов
/ 13 декабря 2011

У меня есть эта функция с именем get_current_weather_data (), которая в основном получает погоду из фида Yahoo XML.Однако я могу вернуть все значения, которые мне нравятся, такие как местоположение, температура и текущее состояние;У меня проблемы с получением изображения, связанного с текущими условиями.

Это область, которую я пытаюсь получить на изображении:

// get weather icon url 
$description = $xml->channel->item->description;

//preg match regular expression to extract weather icon
$imgpattern = '/src="(.*?)"/i';
preg_match($imgpattern, $description, $matches);

$weather['icon_url'] = $matches[1];
echo $weather['icon_url'];

То, что возвращается из приведенного выше кода, это просто URL-адрес изображения, а не изображение.Любая идея?

URL фида:

http://weather.yahooapis.com/forecastrss?p=USNY0996&u=f

или, если хотите, фрагмент приведенного выше URL, который я добавляю: (Обратите внимание на img src, в которую я в настоящее время вхожу)

<description>
<![CDATA[
<img src="http://l.yimg.com/a/i/us/we/52/34.gif"/><br /> <b>Current Conditions:</b><br     /> Fair, 40 F<BR /> <BR /><b>Forecast:</b><BR /> Tue - Sunny. High: 44 Low: 32<br /> Wed -     Partly Cloudy. High: 47 Low: 39<br /> <br /> <a href="http://us.rd.yahoo.com/dailynews/rss/weather/New_York__NY/*http://weather.yahoo.com/forecast/USNY0996_f.html">Full Forecast at Yahoo! Weather</a><BR/><BR/> (provided by <a href="http://www.weather.com" >The Weather Channel</a>)<br/>

]]>

get_current_weather_data () функция:

function get_current_weather_data() {

// Get XML data from source
$feed = file_get_contents("http://weather.yahooapis.com/forecastrss?p=USNY0996&u=f");

// Check to ensure the feed exists
if (!$feed) {
    die('Weather not found! Check feed URL');
}

$xml = new SimpleXmlElement($feed);

$weather['city'] = $xml->channel->children('yweather', TRUE)->location->attributes()->city;
echo $weather['city'] . "<br />";

$weather['temp'] = $xml->channel->item->children('yweather', TRUE)->condition->attributes()->temp;
echo $weather['temp'] . "<br />";

$weather['conditions'] = $xml->channel->item->children('yweather', TRUE)->condition->attributes()->text;
echo $weather['conditions'] . "<br />";

// get weather icon url 
$description = $xml->channel->item->description;

//preg match regular expression to extract weather icon
$imgpattern = '/src="(.*?)"/i';
preg_match($imgpattern, $description, $matches);

$weather['icon_url'] = $matches[1];
echo $weather['icon_url'];

return $weather;
}

1 Ответ

0 голосов
/ 14 декабря 2011

Если вы хотите получить фактические данные изображения, вы можете использовать

file_get_contents($weather['icon_url']);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...