Ping services with PHP

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"?>'.
		' <methodName></methodName>'.
		'  <params>'.
		'   <param>'.
		'    <value>'.$title.'</value>'.
		'   </param>'.
		'  <param>'.
		'   <value>'.$url.'</value>'.
		'  </param>'.
		' </params>'.

	$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".
	"Content-Type: text/xml\r\n".
	"Content-length: ".strlen($content);

	if ($debug) nl2br($headers);

	$response = "";
	$fs=fsockopen('',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.")");

Anything to say?