Posts tagged: pingback

Sending a pingback using the PHP CURL module

To send pingbacks from your site to other blogs/communities/etc., you can use the PHP CURL module:

<?
function send_pingback($sourceURI, $targetURI)
{
        $target = file_get_contents($targetURI);
        // HTTP headers are now in $http_response_header, see docs of HTTP wrapper

        foreach ($http_response_header as $header)
                if (preg_match("/^X-Pingback:\s*([^\s]+)\s*$/", $header, $matches))
                        $serverURI = $matches[1];

        if (!$serverURI) {
                $target = html_entity_decode($target);
                if (preg_match("/<link rel=\"pingback\" href=\"([^\"]+)\" ?\/?>/", $target, $matches))
                        $serverURI = $matches[1];
        }

        if (!$serverURI)
                return;

        $xml = ‘<?xml version="1.0"?><methodCall><methodName>pingback.ping</methodName><params>’.
                ‘<param><value><string>’.$sourceURI.‘</string></value></param>’.
                ‘<param><value><string>’.$targetURI.‘</string></value></param>’.
                ‘</params></methodCall>’;

        $rq = curl_init();
        curl_setopt($rq, CURLOPT_URL, $serverURI);
        curl_setopt($rq, CURLOPT_POST, 1);
        curl_setopt($rq, CURLOPT_POSTFIELDS, $xml);
        curl_exec($rq);
        curl_close($rq);
}

send_pingback("… source uri …", "… target uri …");

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.

Image | WordPress Themes