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"); [...]

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 ;
}
}

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);  [...]

IPhone detection

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

FTP file upload example

upload a single file to a FTP server

// FTP access parameters
$host = ‘ftp.myftp.com’;
$usr = ‘username’;
$pwd = ‘password’;

// file to move:
$local_file = ‘./example.txt’;
$ftp_path = ‘/data/example.txt’;

// connect to FTP server
$conn_id = ftp_connect($host, 21) or die (“Cannot connect to host”);

// send access parameters
ftp_login($conn_id, $usr, $pwd) or die(“Cannot login”);

// perform file upload
$upload = ftp_put($conn_id, $ftp_path, $local_file, FTP_ASCII);

// check upload [...]

US homeland threat level

US homeland threat level

<?php   
/*   
Copyright (c) 2009, reusablecode.blogspot.com; some rights reserved.       
This work is licensed under the Creative Commons Attribution License. To view  
 a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or   
send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California    94305, USA.    */
// Obtain the current Department of Homeland Security threat level.   
function getThreatLevel()   
{       
return eregi_replace(‘.*CONDITION=”(.*)” />’, ‘\1′, [...]