The background
If you write a PHP program that is designed to go and fetch a webpage from the World Wide Web you soon find out that you are not allowed to because of a sensible restiction placed on the use of fopen(), simplexml_load_file and the like.This restiction is absolutely vital to maintain the integrity of a shared server system and you would have to be ten bob short of a quid to allow the retrieval of all that untrusted material from the web. So an intermediate step is used.
But cURL looks complicated, especially to me, and it just looks like something else to learn. But all you need is that webpage being fetched, so you only need a bit of cURL code to do that. The good news is that it has already been written. The bad news is that it is not obvious where to stick it!
The PHP portion
(save as rsayahoo.php) $xml = simplexml_load_file('http://rss.news.yahoo.com/rss/oddlyenough'); print "
- \n"; foreach ($xml->channel->item as $item){ print "
- $item->title \n"; } print "
The cURL portion
But the simplexml_load_file in the function is not allowed out into the wild. So you need an intermediary step, cURL, to go and fetch the page.So the cURL sample would be.
(save as geturl.php) $ch = curl_init("http://rss.news.yahoo.com/rss/oddlyenough"); $fp = fopen("example_htmlpage.html", "w"); curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_HEADER, 0); curl_exec($ch); curl_close($ch); fclose($fp); ?>
After the cURL portion has executed.
(upload as localrsayahoo.php) $xml = simplexml_load_file('example_htmlpage.html'); print "
- \n"; foreach ($xml->channel->item as $item){ print "
- $item->title \n"; } print "
Adding the cURL and PHP portions together
Although the two programs (the cURL part and the PHP) have been shown as two seperate actions you could of course combine them into one program.$ch = curl_init("http://rss.news.yahoo.com/rss/oddlyenough"); $fp = fopen("example_homepage.html", "w"); curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_HEADER, 0); curl_exec($ch); curl_close($ch); fclose($fp); $xml = simplexml_load_file('example_homepage.html'); print "
- \n"; foreach ($xml->channel->item as $item){ print "
- $item->title \n"; } print "
Replacement function for simplexml_load_file
This function uses the CURLOPT_RETURNTRANSFER option, so you don't have to write the results to a local file to retrieve them.function My_simplexml_load_file($URL) { $ch = curl_init($URL); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); $xml = simplexml_load_string(curl_exec($ch)); curl_close($ch); return $xml; } ?>
No comments:
Post a Comment