Mar 01 2010

PHP to get twitter infos and avatar

Category: Php, Spiders & webbotsadmin @ 10:23 pm

I’ve just updated the Mini Bot Php Class with an improved version of the twitterInfo function, here is the code of the new function, I’ve added the avatar image url:

/* this function is part of the Mini Bot Class */

	//
	// get twitter infos from nickname
	// and get avatar url
	public function twitterInfo($nick) {
		$user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
		$ch = curl_init();    // initialize curl handle
		curl_setopt($ch, CURLOPT_URL, "http://twitter.com/$nick"); // set url to post to
		curl_setopt($ch, CURLOPT_FAILONERROR, 1);              // Fail on errors
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);    // allow redirects
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
		curl_setopt($ch, CURLOPT_PORT, 80);            //Set the port number
		curl_setopt($ch, CURLOPT_TIMEOUT, 15); // times out after 15s
		curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
		$document = curl_exec($ch);
		preg_match_all('#<div class="stats">(.*)</div>#Uis', $document, $stats);
		preg_match_all('#<span[^>]*?>(.*)</span>#Uis', $stats[1][0], $spans);
		$o = array();
		for ($i=0;$i<count($spans[0]);$i++) {
			if ($this->attr($spans[0][$i],"id")=="following_count") $o['following'] = $spans[1][$i];
			if ($this->attr($spans[0][$i],"id")=="follower_count") $o['follower'] = $spans[1][$i];
			if ($this->attr($spans[0][$i],"id")=="lists_count") $o['lists'] = $spans[1][$i];
		}
		$o['avatar'] = "";
		preg_match_all('#<img [^>]*?>#Uis', $document, $t);
		for ($i=0;$i<count($t[0]);$i++) if (attr($t[0][$i],"id")=="profile-image") $o['avatar'] = attr($t[0][$i],"src");
		return $o;
	}
  • Share/Bookmark

Tags: , , , , ,


Feb 24 2010

Ping pingomatic.com services with PHP

Category: Php, Spiders & webbotsadmin @ 11:28 pm

Ping-o-matic is a service that calls (ping) server engines and popular services to notify them that you have new contents on your site. This will help you to get your pages indexed on that servers and so it should increase your traffic.
I don’t know if this system works, but it’s implemented also in any wordpress installation, so telling Ping-o-matic that you have new contents should be a good thing. :-)
I’ve searched the web for a function to do this with php, but I didn’t found it and I’ve decided to write it by myself.

Ping-o-matic as many other services works with XML-RPC protocol, the acronym means XML Remote Procedure Call. That is to say that you can call some servers and tell them to execute some action remotely. In this case we use XML-RPC protocol to call Ping-o-matic and tell him: “EHY! I’ve updated my site, here is the link!“.
Ping-o-matic take this link and send it to many other popular services.

The complex part, for me, was understing XML-RPC rules and build the correct headers to call the service. The call is made with fsockopen php function. Here is the function that will be added to the next version of Mini Bot Class.

/*
--------------------------------------------
 $title contains the title of the page you're sending
 $url is the url of the page
 $debug true print out the debug and show xml call and answer
--------------------------------------------
 the output is an array with two elements:
 status: ok / ko
 msg: the text response from pingomatic
--------------------------------------------
*/
function pingomatic($title,$url,$debug=false) {
	$content='<?xml version="1.0"?>'.
		'<methodCall>'.
		' <methodName>weblogUpdates.ping</methodName>'.
		'  <params>'.
		'   <param>'.
		'    <value>'.$title.'</value>'.
		'   </param>'.
		'  <param>'.
		'   <value>'.$url.'</value>'.
		'  </param>'.
		' </params>'.
		'</methodCall>';

	$headers="POST / HTTP/1.0\r\n".
	"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1) Gecko/20090624 Firefox/3.5 (.NET CLR 3.5.30729)\r\n".
	"Host: rpc.pingomatic.com\r\n".
	"Content-Type: text/xml\r\n".
	"Content-length: ".strlen($content);

	if ($debug) nl2br($headers);

	$request=$headers."\r\n\r\n".$content;
	$response = "";
	$fs=fsockopen('rpc.pingomatic.com',80, $errno, $errstr);
	if ($fs) {
		fwrite ($fs, $request);
		while (!feof($fs)) $response .= fgets($fs);
		if ($debug) echo "<xmp>".$response."</xmp>";
		fclose ($fs);
		preg_match_all("/<(name|value|boolean|string)>(.*)<\/(name|value|boolean|string)>/U",$response,$ar, PREG_PATTERN_ORDER);
		for($i=0;$i<count($ar[2]);$i++) $ar[2][$i]= strip_tags($ar[2][$i]);
		return array('status'=> ( $ar[2][1]==1 ? 'ko' : 'ok' ), 'msg'=>$ar[2][3] );
	} else {
		if ($debug) echo "<xmp>".$errstr." (".$errno.")</xmp>";
		return array('status'=>'ko', 'msg'=>$errstr." (".$errno.")");
	}
}
  • Share/Bookmark

Tags: , , , , , , , ,


Feb 22 2010

Reading mp3 informations with php (id3 tags)

Category: Phpadmin @ 12:39 am

Inside mp3 files there are often some usefull informations stored. Those data are called “id3 tags” and deal with Author name, Title and Album name.
There are also some other informations sometimes: such as the length of the track, or the track number of the album, and more…

Well, all of these infos are packed inside the mp3 into a block of bytes called “header“. When I say packed I mean that they are written in a block of bytes in which each byte must be read as binary data following pre-defined rules. The rules, for the mp3 id3 tags, are defined here in the ID3v2 site (at the chapter 3.1).

Reading binary data means that you have to understand each bit, because each bit as a meaning and the rules can say something like “the first 3 bytes identify the tag: the mp3 contains a ID3 tag if the first 3 bytes are ‘ID3′ string“. This rule is easy because you are used to watch bytes as chars as in text files, so you read the first 3 bytes and compare them to the ‘ID3′ string.

Another rule can say: “after the first 3 bytes you have a byte for the version and a byte for the revision number”. This is more complex because you cannnot use bytes as chars, since the number 3 is this octet 00000011 (an octet is a group of 8 bits), whereas the char “3″ is this octet 00110011, that is the number 51.
This means that you have to use functions like bin2hex and hexdec to convert bytes into chars to understand them.

I’ve found the following function on the Giuseppe Manzavino site and I’ve modified it to fix two really small bugs and I see it works fine with ID3 tags written in some files I’ve edited with iTunes.

<?
// From here: http://www.autistici.org/ermes/index.php?pag=1&post=15
// and fixed here: http://www.barattalo.it
// ------------------------------
// example:
// print_r( tagReader ("myfile.mp3") );
// ------------------------------
function tagReader($file){
	$id3v23 = array("TIT2","TALB","TPE1","TRCK","TDRC","TLEN","USLT");
	$id3v22 = array("TT2","TAL","TP1","TRK","TYE","TLE","ULT");
	$fsize = filesize($file);
	$fd = fopen($file,"r");
	$tag = fread($fd,$fsize);
	$tmp = "";
	fclose($fd);
	if (substr($tag,0,3) == "ID3") {
		$result['FileName'] = $file;
		$result['TAG'] = substr($tag,0,3);
		$result['Version'] = hexdec(bin2hex(substr($tag,3,1))).".".hexdec(bin2hex(substr($tag,4,1)));
	}
	if($result['Version'] == "4.0" || $result['Version'] == "3.0"){
		for ($i=0;$i<count($id3v23);$i++){
			if (strpos($tag,$id3v23[$i].chr(0))!= FALSE){
				$pos = strpos($tag, $id3v23[$i].chr(0));
				$len = hexdec(bin2hex(substr($tag,($pos+5),3)));
				$data = substr($tag, $pos, 9+$len);
				for ($a=0;$a<strlen($data);$a++){
					$char = substr($data,$a,1);
					if($char >= " " && $char <= "~") $tmp.=$char;
				}
				if(substr($tmp,0,4) == "TIT2") $result['Title'] = substr($tmp,4);
				if(substr($tmp,0,4) == "TALB") $result['Album'] = substr($tmp,4);
				if(substr($tmp,0,4) == "TPE1") $result['Author'] = substr($tmp,4);
				if(substr($tmp,0,4) == "TRCK") $result['Track'] = substr($tmp,4);
				if(substr($tmp,0,4) == "TDRC") $result['Year'] = substr($tmp,4);
				if(substr($tmp,0,4) == "TLEN") $result['Lenght'] = substr($tmp,4);
				if(substr($tmp,0,4) == "USLT") $result['Lyric'] = substr($tmp,7);
				$tmp = "";
			}
		}
	}
	if($result['Version'] == "2.0"){
		for ($i=0;$i<count($id3v22);$i++){
			if (strpos($tag,$id3v22[$i].chr(0))!= FALSE){
				$pos = strpos($tag, $id3v22[$i].chr(0));
				$len = hexdec(bin2hex(substr($tag,($pos+3),3)));
				$data = substr($tag, $pos, 6+$len);
				for ($a=0;$a<strlen($data);$a++){
					$char = substr($data,$a,1);
					if($char >= " " && $char <= "~") $tmp.=$char;
				}
				if(substr($tmp,0,3) == "TT2") $result['Title'] = substr($tmp,3);
				if(substr($tmp,0,3) == "TAL") $result['Album'] = substr($tmp,3);
				if(substr($tmp,0,3) == "TP1") $result['Author'] = substr($tmp,3);
				if(substr($tmp,0,3) == "TRK") $result['Track'] = substr($tmp,3);
				if(substr($tmp,0,3) == "TYE") $result['Year'] = substr($tmp,3);
				if(substr($tmp,0,3) == "TLE") $result['Lenght'] = substr($tmp,3);
				if(substr($tmp,0,3) == "ULT") $result['Lyric'] = substr($tmp,6);
				$tmp = "";
			}
		}
	}
	return $result;
}
?>

Anyone as seen something similar for reading the EXIF informations on the JPEG files?

  • Share/Bookmark

Tags: , , , ,


« Previous PageNext Page »