How to Ping Pingomatic Using A XML-RPC Request, PHP, and CURL

email-addthis printer-addthis favorites-addthis facebook-addthis digg-addthis | Share

17 May 2010 08:41:42
Category: PHP Development

So I looked around trying to find documentation on what a well formed Pingomatic request looks like and what the response would be. I couldn't find it described well. I found pieces here and pieces there. But not all together in one.

So here we go. I'm going to try and document it.

First you should understand what XML-RPC is. Basically its more complex then a REST interface, like RSS or Atom, but not nearly as complicated as SOAP.

So what does an xml-rpc call look like to Pingomatic? Like this:

	<?xml version="1.0" encoding="iso-8859-1"?>

The third value, RSS location is optional. But the other two values are required. The other part to keep in mind is the method name.The method name for a pingomatic request is

That XML has to be POSTED, and I mean via a POST request, to the ping server found a this address:

I used CURL to pull off the POST request but there are other ways to do it if you don't have CURL available in PHP. Among many other requirements, the CURL request must include a user agent header. In my experimenting, Pingomatic did not accept requests sending the default CURL user agent headers. So I sent a Mozilla user agent header. Be sure to send a user agent.

	//Send the ping request
    $ch = curl_init();
	//Bring back any response
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	//Set the url
	curl_setopt($ch, CURLOPT_URL, $webservice);
	//Tell the service the type of content and the length
	curl_setopt($ch, CURLOPT_HTTPHEADER, 
				array('Content-type: text/xml'
				, 'Content-length: '.strlen($request)
				//I don't know why pingomatic requires a user agent but it does.
				//This one does nicely
,'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' ));

	//Send the data as a post a application/x-www-form-urlencoded
	curl_setopt($ch, CURLOPT_POST, true);
	//Set thd at to post
	curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
	//execute it
	$result = curl_exec($ch);

So how do you know if you are connecting via CURL? You'll get a non-empty string/xml response back. The Pingomatic server will return a 200 code even if something is wrong with your request. So don't get hung up on the server response code. If your CURL request is proper and your request xml is formatted correctly you'll get xml back that looks like this:

//Positive xml repsonse from pingomatic looks like this:
	//Pingomatic always returns a 200 response code even if you do it wrong
	/*<?xml version="1.0"?>
	  <member><name>message</name><value><string>Pings being forwarded to 13 services!</string></value></member>

If you hit the correct interface with badly formed XML, Pingomatic will kindly tell you so. If you get no response back then you're not hitting the proper interface with CURL yet. Keep trying.

Setting this up the first time was harder then it needed to be. I wonder why Pingomatic hasn't documented this well themselves.