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.

$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("", 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();
echo "Pingback successful";

A couple of notes.  On line 3, “” 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.



Leave a comment