Twitter PHP script / Twitter API script

Webdesign

0 Trackbacks

De reden van deze blogpost is dezelfde als die van onze Cufon blogpost; we willen je graag helpen om tijd te besparen in de eindeloze wirwar van (Engelstalige) websites die allemaal iets vertellen over Twitter, hun API en hoe dit te combineren met PHP maar je net niet snel genoeg op weg kunnen helpen omdat het allemaal niet zo lekker werkt.

Hieronder het PHP script met eenvoudige cache functie omdat je de twitter API maar een aantal keer per uur mag aanroepen. Daarom adviseren wij om het script iedere 10 minuten via een cronjob te laten draaien.

<?
// VisionEars Reclame en Media 2010

// vars
$filename 	= '/twitter.rss.txt'; //place this txt file in the root of your webfolder and make it writable (775)
$user_id	= '20235403'; //http://help.twitter.com/entries/15360-how-to-find-your-user-id
$tweets 	= 3;
$handle		= 0;
$xml		= '';

function fake_user_agent_http_get($address) {
	$agents[] = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; WOW64; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; Media Center PC 5.0)";
	$agents[] = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)";
	$agents[] = "Opera/9.63 (Windows NT 6.0; U; ru) Presto/2.1.1";
	$agents[] = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5";
	$agents[] = "Mozilla/5.0 (X11; U; Linux i686 (x86_64); en-US; rv:1.8.1.18) Gecko/20081203 Firefox/2.0.0.18";
	$agents[] = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.16) Gecko/20080702 Firefox/2.0.0.16";
	$agents[] = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_6; en-us) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/3.2.1 Safari/525.27.1";
	
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $address);
	curl_setopt($ch, CURLOPT_HEADER, false);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
	curl_setopt($ch, CURLOPT_USERAGENT, $agents[rand(0,(count($agents)-1))]);
	$data = curl_exec($ch);
	curl_close($ch);
	return $data;
}

$xml = fake_user_agent_http_get('http://api.twitter.com/1/statuses/user_timeline/'.$user_id.'.rss?count='.$tweets);

if (is_writable($filename) AND !stristr($xml,'<error>') AND !empty($xml)) {
	
   // open file
   if (!$handle = fopen($filename, 'w')) {
		 print "Could not open file ($filename).";
		 exit;
   }

   // write file
   if (!fwrite($handle, $xml)) {
	   print "Could not write file ($filename).";
	   exit;
   }
   
   // close file
   fclose($handle);
}
?>

 Succes!

You may send a trackback for this article by using the following Trackback link

Domein Beschikbaarheid

Is uw domeinnaam nog vrij?

Nieuwsbrief

Aanmelden nieuwsbrief


Ontvang aanbiedingen, interessante blogs en het laatste nieuws als eerste.

Schrijf u nu in voor onze nieuwsbrief!

Naam:
E-mailadres:

Afmelden kan in iedere nieuwsbrief.