
Yep, that's true. PHP and XML are really friends. Actually, they are like brothers! In this article, I'm going to explain why, and how you can leverage this for your own use.
First, I want to refer back to my post about PHP 4 being outdated. I said that there was new XML support. A very easy, and user-friendly library. The new SimpleXML library is way too cool. It's easy to use, and you can do a lot with it. Take for example an RSS feed that you want to parse. You can use regular expressions to find out about all tags, or explode all the tags, or do whatever you want. Or you can use SimpleXML. Let me give you a code to look at.
<?php
$xmlstr = file_get_contents("http://feeds.feedburner.com/phpcookies");
// This will grab the contents of my feed.
$xml = new SimpleXMLElement($xmlstr);
// Call the SimpleXMLElement class, and create a new instance of it.
echo "";
";
foreach($xml->channel->item AS $item)
{
echo "- {$item->title}
";
}
echo "
// List all the articles in a simple HTML list.
?>
I hope you understand this little script, but let me explain. First, it grabs the contents of the PhpCookies RSS feed, using a file_get_contents function. Then, the SimpleXML class (called SimpleXMLElement), is called, and a new instance is created, with as XML data our string $xmlstr, which contains the PhpCookies feed.
It will then list all the items (articles), inside the RSS channel, in a simple HTML list, with link to the article, and as text the article title. This can be used if you want to display a friends latest articles. Now, for the next part, I'll be talking on displaying Youtube movies on your site. And then I mean, dynamic ones. Not just the embedding code. We will use the first word of an article to display the video. I will use some custom functions, and some more advanced PHP in this part. See if you can understand it. If you don't understand a part, make sure to comment here, so that I can explain.
<?php
function get_articles($feed)
{
$xmlstr = file_get_contents($feed);
$xml = new SimpleXMLElement($xmlstr);
$articles = $xml->channel->item;
return $articles;
}
function parse_keywords($article)
{
$words = explode(" ", strtolower($article->title));
$use = $words[0];
return $use;
}
function get_youtube_url($mostused)
{
$url = "http://gdata.youtube.com/feeds/videos?vq=" . $mostused . "&start-index=1&max-results=10&alt=rss";
$youxml = file_get_contents($url);
$you = new SimpleXMLElement($youxml);
//print_r($xml);
$item = $you->channel->item[0];
$embed = str_replace("watch?v=","v/", $item->link);
return $embed;
}
$articles = get_articles("http://feeds.feedburner.com/phpcookies");
foreach($articles AS $article)
{
$keyword = parse_keywords($article);
$youtube = get_youtube_url($keyword);
$title = $article->title;
$link = $article->link;
echo <<<EOT
<a href = "$link">$title</a> - $keyword<br />
<object width="425" height="355"><param name="movie" value="$youtube"></param><param name="wmode" value="transparent"></param><embed src="$youtube" type="application/x-shockwave-flash" wmode="transparent" width="425" height="355"></embed></object><br /><br />
EOT;
}
?>
Great, so I used the new XML library to easily list a friends' articles, and with those articles, I grabbed a movie from youtube for every article, based on the first word in the title. Obviously, you don't want to run this in real-time. It takes a load of the server, this is just an example.
Were you hoping for another example on XML, because you really need it? Then comment! The comment section is there for YOU! For you to ask questions, or give, like the name says, comments.



