Categories

View an RSS feed

This example shows how to use the downloadable libraries from the book Webbots, Spiders, and Screen Scrapers to view an RSS feed.

5 lines of code and 3 are the include files

 

<?php
REQUIRE_ONCE ‘LIB_rss.php’;
REQUIRE_ONCE ‘LIB_http.php’;
REQUIRE_ONCE ‘LIB_parse.php’;
$rssfeed = download_parse_rss("http://newsrss.bbc.co.uk/rss/sportonline_world_edition/football/rss.xml");
display_rss_array($rssfeed)
?>

Make a directory

<?php
mkdir (“/htdocs/testdir”, 0644);
echo “Test directory made successfully”;
?>

Check a url exists


function url_exists($url)
{
$ch = curl_init($url);

curl_setopt($ch,CURLOPT_HEADER,true);
curl_setopt($ch,CURLOPT_POST,false);
curl_setopt($ch,CURLOPT_FAILONERROR,true);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_exec($ch);
$curlInfo = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close ($ch);

if ($curlInfo != 200 && $curlInfo != 302 && $curlInfo != 304)
{
return false;
}
else
{
return true ;
}
}

get the season with php

//get current month
$currentMonth=date(“m”);

//work out the season
if ($currentMonth>=”03″ && $currentMonth<=”05″)
$season = “spring”;
elseif ($currentMonth>=”06″ && $currentMonth<=”08″)
$season = “summer”;
elseif ($currentMonth>=”09″ && $currentMonth<=”11″)
$season = “autumn”;
else
$season = “winter”;

Is.gd short url

this example shows how to get a short url using the service from is.gd

 

<?php
/*
Get a is.gd short url using PHP
*/
function isgd($url) 

    $ch = curl_init(); 
    $timeout = 5; 
    curl_setopt($ch,CURLOPT_URL,’http://is.gd/api.php?longurl=’.$url); 
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); 
    $content = curl_exec($ch); 
    curl_close($ch);
    //return the data
    return $content; 
}

//how to use
$shorty = isgd(‘http://progged.net’);
echo $shorty;
?>

IPhone detection

if (stristr($_SERVER['HTTP_USER_AGENT'], ‘iPhone’))
{
// redirect to an iphone version
}