Back To Normal
Click Here To Subscribe Via Email

Subscribe To Our E-Mail Newsletter

Friday, October 24, 2014

How To Auto Ping Your Content to Search Engines


Couch ModePrint It



In this blog post, I will show you how to automatically ping your content to Search Engines for better indexing rates.

Doing SEO is not so easy if you don't have access to quality tools and state of the art techniques.

This technique will assure you that every page of your website or blog gets indexed fast and fully automated. You don't need to do anything, just use this code and forget it!


This code will ping using Ping-o-Matic's RPC service. The original code is provided by Barattalo.


Here goes the PHP function:

 /*
--------------------------------------------
 $title contains the title of the page you're sending
 $url is the url of the page
 $debug true print out the debug and show xml call and answer
--------------------------------------------
 the output is an array with two elements:
 status: ok / ko
 msg: the text response from pingomatic
--------------------------------------------
*/
function pingomatic($title,$url,$debug=false) {
    $content='<?xml version="1.0"?>'.
        '<methodCall>'.
        ' <methodName>weblogUpdates.ping</methodName>'.
        '  <params>'.
        '   <param>'.
        '    <value>'.$title.'</value>'.
        '   </param>'.
        '  <param>'.
        '   <value>'.$url.'</value>'.
        '  </param>'.
        ' </params>'.
        '</methodCall>';
 
    $headers="POST / HTTP/1.0\r\n".
    "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1) Gecko/20090624 Firefox/3.5 (.NET CLR 3.5.30729)\r\n".
    "Host: rpc.pingomatic.com\r\n".
    "Content-Type: text/xml\r\n".
    "Content-length: ".strlen($content);
 
    if ($debug) nl2br($headers);
 
    $request=$headers."\r\n\r\n".$content;
    $response = "";
    $fs=fsockopen('rpc.pingomatic.com',80, $errno, $errstr);
    if ($fs) {
        fwrite ($fs, $request);
        while (!feof($fs)) $response .= fgets($fs);
        if ($debug) echo "<xmp>".$response."</xmp>";
        fclose ($fs);
        preg_match_all("/<(name|value|boolean|string)>(.*)<\/(name|value|boolean|string)>/U",$response,$ar, PREG_PATTERN_ORDER);
        for($i=0;$i<count($ar[2]);$i++) $ar[2][$i]= strip_tags($ar[2][$i]);
        return array('status'=> ( $ar[2][1]==1 ? 'ko' : 'ok' ), 'msg'=>$ar[2][3] );
    } else {
        if ($debug) echo "<xmp>".$errstr." (".$errno.")</xmp>";
        return array('status'=>'ko', 'msg'=>$errstr." (".$errno.")");
    }
}





How to use: 
$title="How To Auto Ping Your Content to Search Engines";
$url="http://www.thedeveloper24.com/2014/10/auto-ping-your-content-to-search-engines.html";
$ping=pingomatic($title, $url);
if($ping['status']=='ok'){
   echo "Success: ".$ping['msg'];
} else {
   echo "Error: ".$ping['msg'];
}




Automation:
You can create a PHP script with the following content somewhere (e.g http://mysite.com/pinger.php)

<?php 
/*
--------------------------------------------
 $title contains the title of the page you're sending
 $url is the url of the page
 $debug true print out the debug and show xml call and answer
--------------------------------------------
 the output is an array with two elements:
 status: ok / ko
 msg: the text response from pingomatic
--------------------------------------------
*/
function pingomatic($title,$url,$debug=false) {
    $content='<?xml version="1.0"?>'.
        '<methodCall>'.
        ' <methodName>weblogUpdates.ping</methodName>'.
        '  <params>'.
        '   <param>'.
        '    <value>'.$title.'</value>'.
        '   </param>'.
        '  <param>'.
        '   <value>'.$url.'</value>'.
        '  </param>'.
        ' </params>'.
        '</methodCall>';
 
    $headers="POST / HTTP/1.0\r\n".
    "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1) Gecko/20090624 Firefox/3.5 (.NET CLR 3.5.30729)\r\n".
    "Host: rpc.pingomatic.com\r\n".
    "Content-Type: text/xml\r\n".
    "Content-length: ".strlen($content);
 
    if ($debug) nl2br($headers);
 
    $request=$headers."\r\n\r\n".$content;
    $response = "";
    $fs=fsockopen('rpc.pingomatic.com',80, $errno, $errstr);
    if ($fs) {
        fwrite ($fs, $request);
        while (!feof($fs)) $response .= fgets($fs);
        if ($debug) echo "<xmp>".$response."</xmp>";
        fclose ($fs);
        preg_match_all("/<(name|value|boolean|string)>(.*)<\/(name|value|boolean|string)>/U",$response,$ar, PREG_PATTERN_ORDER);
        for($i=0;$i<count($ar[2]);$i++) $ar[2][$i]= strip_tags($ar[2][$i]);
        return array('status'=> ( $ar[2][1]==1 ? 'ko' : 'ok' ), 'msg'=>$ar[2][3] );
    } else {
        if ($debug) echo "<xmp>".$errstr." (".$errno.")</xmp>";
        return array('status'=>'ko', 'msg'=>$errstr." (".$errno.")");
    }
}


$title=$_GET['title'];
$url=$_GET['url'];
if(empty($title) || empty($url)) die('missing parameters'); 
$ping=pingomatic($title, $url);
echo $ping['status']; 
?>



How to Automate:

Well, you can simple include some jQuery code in all of your pages to ping the content automatically (using the above PHP script).

$(function(){
 var title=document.title; /* get current page title or you can set your own too */
 var url=document.location.href; /* get current page url or better set it in backend e.g canonical url */
  $.ajax({
  url: "http://mysite.com/pinger.php?title="+title+"&url="+url,
  type: "GET",
  success: function(){
   console.log("pinged!");
  }
 });
});

  • Don't forget to include jQuery if you don't have it already. 
  • Don't forget to set your script path e.g replace mysite.com/pinger.php with the path where you have the php script.
Don't forget to share your thoughts in comments. Also have a look on some other great PHP snippets

:D
:)
:[
;)
:D
:O
(6)
(A)
:'(
:|
:o)
8)
(K)
(M)