Pingback in php

Motivation: For a side project I am working on, I wanted to be able to send a trackback to WordPress blogs (or any blog with an XMLRPC endpoint), using PHP.   Simple enough.

Methodology: I had a few different options.  I could have manually made a POST request to the endpoint, hand-coding the XML (this brute force method).  Or I could stand on the shoulders of giants and leverage the power of open-source libraries.  I chose the latter, in keeping with my new guiding principle of pursuing beauty, truth and goodness.

To brush up on how an XMLRPC pingback works, from a 30,000 foot level, I checked out the Pingback 1.0 Specification.  As far as the libraries, I settled on phpxmlrpc.  First, the documentation existed and second, it was straightforward.  According to the documentation, the process goes like this:

  1. Create an xmlrpc client
  2. Set the debug level you think you need (optional)
  3. Create a new message
  4. Send the message
  5. Parse the response

Implementation: Again, this isn’t rocket science, but here’s what I did.  Suggestions and critiques are welcome.

1
2
3
4
5
6
7
8
9
$xmlrpc_client = new xmlrpc_client("xmlrpc.php", $host_url, 80);
$xmlrpc_client->setDebug(1); //this will print all the responses as they come back
$xmlrpc_message = new xmlrpcmsg("pingback.ping", array(new xmlrpcval($site_linking_from), new xmlrpcval($site_linking_to)));
$xmlrpc_response = $xmlrpc_client->send($xmlrpc_message);
if($xmlrpc_response->faultCode() == 0){
echo $xmlrpc_response->faultString();
}else{
echo "Pingback successful";
}

A couple of notes.  On line 3, “pingback.ping” is the XMLRPC API method that handles pingbacks.  Also, the order of the sites you send to the call would be your site, and the site you’re pinging.  I switched this order around the first time and got some interesting error messages.

 

Sources

http://www.quietearth.us/articles/2006/10/30/Coding-your-own-blog-Pingback-in-php

http://blog.perplexedlabs.com/2009/07/15/xmlrpc-pingbacks-using-php/

http://www.xiven.com/sourcecode/pingbackserver.php

Leave a comment