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

Loading

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"?>
<methodCall>
  <methodName>weblogUpdates.ping</methodName>
  <params>
    <param>
      <value>YOUR WEBLOG NAME HERE</value>
    </param>
    <param>
      <value>http://www.YOURWEBLOGURL.com/</value>
    </param>
  </params>
</methodCall>

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"?>
<methodCall>
<methodName>weblogUpdates.ping</methodName>
<params>
 <param>
  <value>
   <string>Tech Chorus</string>
  </value>
 </param>
 <param>
  <value>
   <string>http://techchorus.net</string>
  </value>
 </param>
</params>
</methodCall>

The next step is to create the streams context.

<?php
$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.

<?php
$server 
"http://rpc.technorati.com/rpc/ping";
$file file_get_contents($serverfalse$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.

<?php
$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.

<?php
$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($serverfalse$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.

About the author

Sudheer is an entrepreneur and software developer. Get more from Sudheer on Twitter.


Nice tutorial :) PD: $request

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.

Fixed the typo. Thanks.

other services?

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

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?

xml client

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?

Why not?

Awesome post, sudheer. Been

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'm not lucky with pingomatic

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

wow

nice example. thanks

Nice

It is very nice.....

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>. The supported tag styles are: <foo>, [foo].

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
2 + 14 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
By submitting this form, you accept the Mollom privacy policy.