Receiving pingbacks using the PHP XML-RPC module

If you want to enable a Web site (for instance, a blog or community project) to receive pingbacks, you can use the PHP XML-RPC module.

All you need to do is to

  1. create an XML-RPC server
  2. register a function for pingback.ping
  3. call the server
  4. implement the function

The function itself, let’s call it do_pingback, only needs to process the pingback: verify its validity and register the ping somewhere, for instance in the database.

Sample code:

<?
/* /pingback-server.php */
require_once("include/config.php");

function do_pingback($method, $params, $extra)
{
        global $db;
        list ($sourceURI, $targetURI) = $params;

        // fetch the source URI to verify that the source does indeed link to the target
        $source = file_get_contents($sourceURI);
        if ($source == false)
                return 16;      // "The source URI does not exist."

        $source = html_entity_decode($source);
        if (strpos($source, $targetURI) === false)
                return 17;      // "The source URI does not contain a link to the target URI, and so cannot be used as a source."

        // TODO: check own data to ensure that the target exists and is a valid entry

        // register pingback (if not done already)
        $status = $db->exec("INSERT INTO pingbacks (source, target) VALUES (".$db->quote($sourceURI).", ".$db->quote($targetURI).")");
        if (PEAR::isError($status))
                return 48;

        return "Ping registered, thanks";
}


$server = xmlrpc_server_create();
xmlrpc_server_register_method($server, "pingback.ping", "do_pingback");
if ($response = xmlrpc_server_call_method($server, $HTTP_RAW_POST_DATA, null)) {
        header("Content-Type: text/xml");
        print $response;
}

?>

Of course, you have to announce the pingback server on your Web site. I prefer the HTTP header solution, so I added

header("X-Pingback: http://".GU_DOMAIN."/pingback-server.php");

to a global include.

I use this code for the pingback implementation on gimpusers.com / gimpusers.de.

2 Responses to “Receiving pingbacks using the PHP XML-RPC module”

  1. gimpusers.com now supports pingbacks - News - gimpusers.com said:

    Feb 04, 09 at 02:11

    […] then you can see your backlink directly above the comments. As a demonstration, we linked from our blog.dev001.net article (where you can find technical details of the implementation, if you are interested) to this […]

  2. How do you pingback a site when a link is added to it? - QuestionBin - Intelligent Answers for Smart Questions::Answer said:

    Nov 11, 09 at 20:14

    […] You need to let the pingback URL of the site know that you have linked to them. The pingback URL is is embedded in a <link rel and looks like this: <link rel="pingback" href="http://url/xmlrpc.php" /> You can find the pingback URL standards here: http://www.hixie.ch/specs/pingback/pingback You can find a PHP Pingback Example here: http://blog.dev001.net/2009/02/receiving-pingbacks-using-the-php-xml-rpc-module/ […]


Leave a Reply


blog.dev001.net is Digg proof thanks to caching by WP Super Cache