XML RPC Client In PHP - A Real World Example: Ping Technorati

If you are familiar with blogosphere you know what is Technorati.

Technorati is an Internet search engine for searching blogs. You can submit your blog to Technorati and ping their servers when your blog site is updated. when you ping Technorati their web crawlers read the updated information on your site.

Technorati offers an XML-RPC ping web service to allow blogs to notify content changes.

In this article we discuss how to write a PHP script to programatically ping Technorati when there is new content in your site.

The URL to ping is rpc.technorati.com/rpc/ping.

The method name to call is weblogUpdates.ping.

The request XML must contain two parameters - your blog site name the blog site URL.

From the API documentation of Technorati, the sample request XML is as below:

<?xml version="1.0"?>

weblogUpdates.ping

YOUR WEBLOG NAME HERE

http://www.YOURWEBLOGURL.com/

Let's start writing the XML-RPC client to ping Technorati.

Our first step is to generate the required XML request string.

<?php
$site_name = "Tech Chorus";
$site_url = "http://techchorus.net";

$request = xmlrpc_encode_request("weblogUpdates.ping", array($site_name, $site_url));

The function xmlrpc_encode_request as the name suggests, creates the request XML string. If you echo $request variable you will see output like below:

<?xml version="1.0" encoding="iso-8859-1"?>

weblogUpdates.ping

Tech Chorus

http://techchorus.net

The next step is to create the streams context.

$context = stream_context_create(array('http' => array(
'method' => "POST",
'header' => "Content-Type: text/xml\r\nUser-Agent: PHPRPC/1.0\r\nHost: rpc.technorati.com\r\n",
'content' => $request
)));

The code snippet states that we are preparing to make an HTTP POST request to the server rpc.technorati.com.

The next step is to make the request.


$server = "http://rpc.technorati.com/rpc/ping";
$file = file_get_contents($server, false, $context);

We now decode the response XML using the function xmlrpc_decode(). We determine whether the response XML contains a fault structure using the xmlrpc_is_fault() function.


$response = xmlrpc_decode($file);
if (is_array($response) and xmlrpc_is_fault($response)){
echo "Failed to ping Technorati";
} else {
echo "Successfully pinged Technorati";
}

The entire script is pasted below.

$site_name = "Tech Chorus";

/**
* The URL of your site
*/
$site_url = "http://techchorus.net";

$request = xmlrpc_encode_request("weblogUpdates.ping", array($site_name, $site_url));

#echo $request;

$context = stream_context_create(array('http' => array(
'method' => "POST",
'header' => "Content-Type: text/xml\r\nUser-Agent: PHPRPC/1.0\r\nHost: rpc.technorati.com\r\n",
'content' => $request
)));

$server = "http://rpc.technorati.com/rpc/ping";
$file = file_get_contents($server, false, $context);

$response = xmlrpc_decode($file);

if (is_array($response) and xmlrpc_is_fault($response)){
echo "Failed to ping Technorati";
} else {
echo "Successfully pinged Technorati";
}

The source code is available from the Code Album Github repository.

Comments

Nice tutorial :)

PD:

$request = xmlrpc_encode_request(""weblogUpdates.ping", array($site_name, $site_url));

Look at the duplicated double quote ""weblogUpdates.ping" ;-).

Fixed the typo. Thanks.

what needs to be added to this script in order for it to be used with other serives? Like ping-o-matic?

Just change the server URL and run the script. It should work well if ping-o-matic supports the same method - "weblogUpdates.ping" with the same parameters.

Otherwise you may have to change the method name and modify the parameters.

Can you point me to their API documentation?

hey, there are actually a whole list of > 20 ping servers. wordpress supports all this in a single list(i'm not sure if they are using a single code for that) . do you think your ping client can be functionized and support all pings as well?

thanks

Why not?

Awesome post, sudheer.

Been looking for a script like this for awhile (new to the pinging thing and have been using the REST client but feel its time to get a bit more intelligent about it).

Before I start, just a quick point out: the link in the article to the API Documentation is broken...seems like Technorati is going through a bunch of API construction...just went their developer section and says that it is under construction.

Anyways, thanks for the read....bookmarked it and going to give it a go later today to implement it into a php look for all the services I want to ping so i can use XML-RPC instead of the REST client.

Thanks for the post!

Eric

I dont know why I can't ping to pingomatic service.
Can you give an example? Thanks

nice example. thanks

It is very nice.....

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.