Implementing ping with phpxmlrpc in CakePHP

Written: May 17th 2007, 21:56

On of the latest things on my to-do list has been implementing a ping function in my CakePHP blog. I was hoping to find some code to copy and paste directly from CakePHP Google Group, but it seems that none has done it yet, and I ended up doing it myself.



This is how I did it:


  1. Download the XMLRPC PHP class from http://sourceforge.net/projects/phpxmlrpc/, extract it and move the file libs/xmlrpc.inc into app/vendors. Remember to change the fileextension to .php
  2. Include the file by adding this in the start of your controller:
    vendor('xmlrpc');<br />class PostsController extends AppController {
  3. Now create a separat ping function:
    function ping() {<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;// Make an object to represent our server.<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$server = new xmlrpc_client('/rpc/ping','rpc.technorati.com', 80);<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<br /> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;$message = new xmlrpcmsg(&quot;weblogUpdates.ping&quot;, array(new xmlrpcval(&quot;Gersh.no&quot;),<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; new xmlrpcval(&quot;http://www.gersh.no&quot;)));<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;// Send a message to the server.<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<br /> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;$result = $server-&gt;send($message);<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if (!$result || $result-&gt;faultCode()) return(false);<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;else return(true);<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<br /> &nbsp;&nbsp;&nbsp; }
  4.  Call this function whenever you want to ping and update or change with:
    $this-&gt;ping();


What is ping?


any blog authoring tools automatically ping one or more servers each time the blogger creates a new post (or updates an old one.) That is, the tool sends an XML-RPC signal to one or more "ping servers," which can then generate a list of blogs that have new material.

(from wikipedia):

Back to posts list