October 20, 201115 yr Every webmaster likes a bit of free and ever changing fresh content especially if they can incorperate into a web pages HTML where those data hungry search bots can find it. RSS feeds are often ideal for this sort of thing but they can get tricky. There are of course several ways to get an RSS feed into a web page however most of these methods don't play well with images in the feed and get around this by just tweeking the image source URL for the pictures simply hotlinking to the originating site. Now as I'm fairly sure your all respectable and don't want some other webmaster screaming bandwidth theft read on, this little demo snippet swaps out image URL's and replaces them with the same image encoded as a base64 string that gets saved along with the regular HTML. The codes not very pretty and I've not covered saving the info to database or flat file but it should hopefully give you something to experiment with. <?php $grab=1; if ($grab==1){ $target_url= "http://en.wikinews.org/w/index.php?title=Special:NewsFeed&feed=rss&categories=Published¬categories=No%20publish%7CArchived%7CAutoArchived%7Cdisputed&namespace=0&count=30&hourcount=124&ordermethod=categoryadd&stablepages=only";// wiki news uk ; } /////////////////////////////// $xml = @simplexml_load_file($target_url) or die("feed not loading"); //echo "<pre>"; var_dump($xml); echo "</pre>";// debug view of raw xml foreach ($xml->channel->item as $new){ $temp_tit = $new->title; $temp_des = $new->description; $patterns = array();/// use regex to strip out any parts you don't need $patterns[0] = '/(\r\n|\r|\n)/'; // removes line-breaks and caraige returns (makes regex simpler) $patterns[1] = '/<div(.+?)infobox(.+?)<\/div>/'; // removes infobox div (unique to wiki news) $patterns[2] = '/and anything else you want removing and so on/'; /// $replacements = array(); $replacements[0] = ''; $replacements[1] = ''; $replacements[2] = 'whatever replacement html'; $temp_des =preg_replace($patterns, $replacements, $temp_des); /// grab all the image URL's $regex = '/upload(.+?)thumb(.+?)\"/';// extract images preg_match_all($regex,$temp_des,$match); foreach ($match as $images); /// loop through the images $i=0; while ($images[$i]){ /// fetch the image from host server $grab_link = "http://upload.wikimedia.org/wikipedia/commons/thumb".$images[$i]; //echo "grab url ".$grab_link."</br>"; /// convert to base64 string $image_coded = chunk_split(base64_encode(file_get_contents($grab_link))); if ($image_coded) echo "image_coded success </br>"; /// add escape slashes otherwise $patterns regex throws wobbly /// NB: command is "add C slashes" not plain addslashes $imageSlashed = addcslashes($images[$i],'/'); //echo "image slashed".$imageSlashed."</br>"; $patterns = '/src=(.+?)'.$imageSlashed.'/';// find this html pattern //echo "replacement pattern".$patterns."</br>+++++++++++++</br>"; $replacements = 'src="data:image/png;base64,'.$image_coded.'"';// swap in this html /// perform swap of image src URL for base64 string $temp_des =preg_replace($patterns, $replacements, $temp_des); $i++; } /// dumps out html echo $temp_tit."</br>--------------------------------------</br>".$temp_des."</br>+++++++++++++++++++++++++++++++++++++++</br>"; $i++; } ?>
October 20, 201115 yr Author Well that's three of us showing our age lol and oops , should of posted this in server side Edited October 20, 201115 yr by Sogo7
October 20, 201115 yr How does a Womble pick up a feed like this anyway? [moved to server side (by Orinocho)]
Create an account or sign in to comment