XML-RPC pinging (google and others)

dario111cro picture dario111cro · Mar 5, 2012 · Viewed 11.2k times · Source

I am trying to ping (SEO tactic called "ping" is used for new content to get robots index it faster) Google in PHP. Only thing I know is that I need to send my request to following url: http://blogsearch.google.com/ping/RPC2

Probably I can use PHP XML-RPC functions. I don't know how to format my request and which method to use.

Answer

hakre picture hakre · Mar 5, 2012

As far as you're concerned to do the XML-RPC request (Example #1).

If you follow the specification of a pingback, it would look like this:

$sourceURI = 'http://example.com/';
$targetURI = 'http://example.com/';
$service = 'http://blogsearch.google.com/ping/RPC2';

$request = xmlrpc_encode_request("pingback.ping", array($sourceURI, $targetURI));
$context = stream_context_create(array('http' => array(
    'method' => "POST",
    'header' => "Content-Type: text/xml",
    'content' => $request
)));
$file = file_get_contents($service, false, $context);
$response = xmlrpc_decode($file);
if ($response && xmlrpc_is_fault($response)) {
    trigger_error("xmlrpc: $response[faultString] ($response[faultCode])");
} else {
    print_r($response);
}

Which would give you the following output:

Array
(
    [flerror] => 
    [message] => Thanks for the ping.
)

Generally, if you don't know which method you call, you can also try XML-RPC Introspection - but not all XML-RPC servers offer that.


You asked in a comment:

According to specs, $targetURI should be: "The target of the link on the source site. This SHOULD be a pingback-enabled page". How can I make pingback enabled page, or more important, what is that actually?

A pingback enabled site is a website that announces an XML-RPC pinbback service as well. That's done with the HTMl <link> element in the <head> section. Example:

<link rel="pingback" href="http://hakre.wordpress.com/xmlrpc.php" />

The href points to an XML-RPC endpoint that has the pingback.ping method available.

Or it's done by sending a specifc HTTP response header:

X-Pingback: http://charlie.example.com/pingback/xmlrpc

See pingback-enabled resource.

So if you ping others, others should be able to ping you, too.