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.

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.