Stison - Publishing workflow solutions

Support

Pingback server using PHP & XMLRPC (Help)

We had to create a pingback server for blogs. I found plenty of resources but none that did everything.

As a starting point we used Coding your own blog: Pingback in php but that includes the rpc.inc functions. We wanted to use the php-xmlrpc installed with php.

Step 1) Ensure you have php-xmlrpc installed:

rpm -qa | grep xmlrpc
If you can't see it:
yum install php-xmlrpc

Step 2) Restart httpd if you just installed php-xmlrpc.

Step 3) Ensure the page(s) you are serving have this before any html is served

header("X-Pingback: http://www.yoursite.com/pingback.php")
and/or this for the html header section:

<link rel="pingback" href="http://www.yoursite.com/pingback.php" />

Step 4) Copy the code below to pingback.php

Step 5) Put in your error handling

Step 6) Test is out! You can create a client using: Want Pingback in my custom built Blogs! : 5ubliminal's TellinYa

Feel free to use the code. It comes as is and has no warranties.

<?
header("Content-Type: application/xml");

error_reporting(1);

function pbprocess($method_name, $params, $app_data) {
        $source = $params[0];
        $dest = $params[1];

        # INSERT CODE
        # here we can check for valid urls in source and dest, security
        # lookup the dest article in our database etc..
        if (..) { # source uri does not exist
                return array('faultCode'=>16, 'faultString'=>'Source uri does not exist');>
        }

        if (..) # source uri does not have a link to target uri
                return array('faultCode'=>17, 'faultString'=>'Source uri does have link to target uri');
        }

        if (..) { # target uri does not exist
                return array('faultCode'=>32, 'faultString'=>'Target uri does not exist');
        }

        if (..) { # target uri cannot be used as target
                return array('faultCode'=>33, 'faultString'=>'Target uri cannot be used as target');
        }

        if (..) { # Pingback already registered
                return array('faultCode'=>48, 'faultString'=>'Target uri cannot be used as target');
        }

        if (..) { # Access denied
               return array('faultCode'=>49, 'faultString'=>'Access denied');
        }

        if (..) { # Could not communicate with upstream server or got error
               return array('faultCode'=>50, 'faultString'=>'Problem with upstream server');
        }

        if (..) { # Generic fault code if not applicable to above
               return array('faultCode'=>50, 'faultString'=>'Unkown error');
        }

        return array("Pingback registered.");
}

#create the xmlrpc server
if ($xmlrpc_server=xmlrpc_server_create()) {
        #register the function
        if($registered=xmlrpc_server_register_method($xmlrpc_server, "pingback.ping", "pbprocess" )) {
                #make the call
                echo xmlrpc_server_call_method($xmlrpc_server, $HTTP_RAW_POST_DATA, NULL, array('output_type' => "xml"));
                # An alternative to $HTTP_RAW_POST_DATA use : $request = file_get_contents('php://input');
        } else {
                trigger_error("failed to register method",E_USER_ERROR);
        }
        #destroy the server resource
        xmlrpc_server_destroy($xmlrpc_server)
} else {
        trigger_error("failed to create xml rpc server",E_USER_ERROR);
}

?>

Resources:
Coding your own blog: Pingback in php
Creating an XML-RPC Web Service
Finding the x-pingback ( xmlrpc ) url from any website ..
elHttpClient Evolution - PHP + cURL + HTTP
Want Pingback in my custom built Blogs! : 5ubliminal's TellinYa