/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Parsing XML code

I’m new to PHP and am doing my best to learn it. Any help is GREATLY appreciated and if you can explain in detail that’s even better!

My goal is to change my site based upon the weather condition. I found this code on [url]http://css-tricks.com/using-weather-data-to-change-your-websites-apperance-through-php-and-css/[/url] but can’t get it to work..?!

[code=php] <?php

/* get the weather from Yahoo */
$data = get_data(“http://weather.yahooapis.com/forecastrss?p=97211&u=f”);
$weather = format_result(get_match(‘/<yweather:condition text=”(.*)”/isU’,$data));

/* debug */
//echo ‘<pre style=”background:#fff;font-size:12px;”>[‘; print_r($weather); echo ‘]</pre>’;

/* format the result */
function format_result($input)
{
return strtolower(str_replace(array(‘ ‘, ‘(‘, ‘)’), array(‘-‘, ‘-‘, ”), $input));
}

/* helper: does regex */
function get_match($regex,$content)
{
preg_match($regex,$content,$matches);
return $matches[1];
}

/* gets the xml data from Alexa */
function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$xml = curl_exec($ch);
curl_close($ch);
return $xml;
}

?>

<?php Echo(“$weather”); ?>[/code]

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@artemisApr 29.2008 — What's the problem exactly? Are you sure you have curl enabled?
Copy linkTweet thisAlerts:
@kierhonauthorApr 30.2008 — Yes curl is enabled. Does this script work for you? I'm trying to figure out if its my hosting server that's not letting it work or the code.
Copy linkTweet thisAlerts:
@roughageApr 30.2008 — I used an extensive piece of code for yahoo weather on one of my sites. I modified it like crazy to display live weather of 40 or so different regions.

I realize that is not what you are trying to do, but, do you have the appropriate images as referenced by the article. Without sunny, fair, cloudy, windy, etc., your masthead image will not change.
Copy linkTweet thisAlerts:
@kierhonauthorApr 30.2008 — Yeah i realize that the header wont change, but for testing purposes i'm not even concerned with the header. It's not even displaying the results from the XML feed when i <?php Echo("$weather"); ?>

So something else must be messed up.. maybe with the regexp? I don't know PHP expressions that well
Copy linkTweet thisAlerts:
@artemisApr 30.2008 — Yeah you're right, this works:

[code=php]<?php

/* get the weather from Yahoo */
$data = get_data("http://weather.yahooapis.com/forecastrss?p=97211&u=f");
$weather = format_result(get_match('#<yweather:condition(?:s+)text="(.*?)"#',$data));

/* debug */
//echo '<pre style="background:#fff;font-size:12px;">['; print_r($weather); echo ']</pre>';

/* format the result */
function format_result($input)
{
return strtolower(str_replace(array(' ', '(', ')'), array('-', '-', ''), $input));
}

/* helper: does regex */
function get_match($regex,$content)
{
preg_match($regex,$content,$matches);
return $matches[1];
}

/* gets the xml data from Alexa */
function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$xml = curl_exec($ch);
curl_close($ch);
return $xml;
}

?>

<?php Echo("$weather"); ?> [/code]
Copy linkTweet thisAlerts:
@kierhonauthorApr 30.2008 — You rock man, thanks a ton!
×

Success!

Help @kierhon spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.9,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...