DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Read Rpc List From File And Ping All - For Your Blog!

  • submit to reddit
        This is a PHP5-based XML-RPC ping script. It reads a one-column fully qualified URL-list from a file ($pingListFile). Such a script is extremely useful for bloggers ;-)

Please, edit the variables at the top of the file to fit your needs.

   // Please, edit these variables to your needs

   $blogTitle="Title Of your blog";
   $showDebugInfo=FALSE; // Do you want verbose output?

   // Stop editing here

   // PingRPC.php
   // 2007 by Sascha Tayefeh
   // This is a PHP5-based XML-RPC ping script. It reads a one-column
   // fully qualified URL-list from a file ($pingListFile). Here is
   // an example how this file must look like:
   // ----------------------
   // ----------------------

   $userAgent="pingrpc.php by tayefeh";

   // Read pinglist file. Must contain one fully qualified URL
   // (e.g: PER LINE (-> 
   // delimiter is an ASCII-linebreak)
   while ( ! feof( $fp) )
      $line = trim(fgets( $fp, 4096));
      // get the hostname
      $host=$line; // Make a copy of $line
      $host=preg_replace('/^.*http:\/\//','',$host); // Delete anything before http://
      $host=preg_replace('/\/.*$/','',$host); // Delete anything after behind the hostname

      // get the path 
      $path=$line; // Make another copy of $line
      $path=preg_replace('/^.*http:\/\/[a-zA-Z0-9\-_\.]*\.[a-zA-Z]{1,3}\//','',$path,-1,$replacementCount); // Delete anything before the path
      if(!$replacementCount) $path=''; // if there was no replacement (i.e. no explicit path), act appropiately
      if($host) $myList[$host]=$path;
   echo "<h1>Ping process started</h1>";

   echo "<p>Reading URLs from file $pingListFile: ";
   echo count($myList)." urls read.</p>";

   // Use DOM to create the XML-File
   $xml= new DOMDocument('1.0');

   // Create the xml structure

   // Set the node values

   $xmlrpcReq = $xml->saveXML(); // Write the document into a string
   $xmlrpcLength = strlen( $xmlrpcReq ); // Get the string length.

   echo "Here's the xml-message I generated (size: $xmlrpcLength bytes):";

   echo "\n<pre>\n";
   echo htmlentities($xmlrpcReq);
   echo "</pre>";

   echo "<dl>";

   // Proceed every link read from file
   foreach ( $myList as $host => $path)
      if($showDebugInfo) echo "<hr/>";

      echo "<dt><strong>Pinging host: $host  </strong>";
      $httpReq  = "POST /" . $path . " HTTP/1.0\r\n";
      $httpReq .= "User-Agent: " . $userAgent. "\r\n";
      $httpReq .= "Host: " . $host . "\r\n";
      $httpReq .= "Content-Type: text/xml\r\n";
      $httpReq .= "Content-length: $xmlrpcLength\r\n\r\n";
      $httpReq .= "$xmlrpcReq\r\n";
      echo "</dt>";

	 echo "<dd><strong>Request:</strong><pre><span style=\"color: #cc9900\">".htmlentities($httpReq)."</span></pre>";
	 echo "<strong>Answer</strong>:<span style=\"color: #99cc00\"><pre>";

      // Actually, send ping
      if ( $pinghandle = @fsockopen( $host, 80 ) )
	 @fputs( $pinghandle, $httpReq );
	 while ( ! feof( $pinghandle ) )
	    $pingresponse = @fgets( $pinghandle, 128 );
	    if($showDebugInfo) echo htmlentities($pingresponse);
	 @fclose( $pinghandle );
      if($showDebugInfo) echo "</span></pre></dd>";
   echo "</dl>";
   echo "<p>FINISHED</p>";


Here's an example input-file (pinglist.txt):


Snippets Manager replied on Tue, 2007/01/16 - 10:39pm

I am afraid, I didn't get your question. Do you mean whether this code can be used to make your non-feed homepage public to blog-directories? The answer is: No! The bots expect an atom- or rss-feed.

ed pudol replied on Sun, 2007/04/15 - 3:01am

Does this script can be use in none blog site, like for example in my personal site but there is an xml feed of it? Instead of the site url is posible to directly enter the xml feed url?