Tutorials, PHP & MySQL

How to ping search engines with curl

When you add new story or update an existing one it may take a few weeks for search engine bots to re-index your site, I've noticed however that some sites get re-index very shortly after adding or updating content on their website, which got me thinking how did they do it? the answer they ping search engines when ever there's any changes which informs the search engine bots there's been a change and to read the site's sitemap again this process is usually done within a few minutes of sending the ping.

I wrote a small script with makes the process automatic this tutorial will show you how.

First create a function which will have two parameters the first ($sitemap) is the path sitemap and ($service) which is the search engine to ping.

function pingSE($sitemap,$service){

Next scan through the $service variable if a match is found then create another variable with the search engine ping path along with the sitemap path appended to the end. If no matches are found the function won't return anything, in other words it simply stops.

switch ($service) {
        case 'bing':
            $ping = "http://www.bing.com/webmaster/ping.aspx?siteMap=$sitemap";
            break;
        case 'ask':
            $ping = "http://submissions.ask.com/ping?sitemap=$sitemap";
            break;
        case 'google':
            $ping = "http://www.google.com/webmasters/sitemaps/ping?sitemap=$sitemap";
            break;
        case 'moreover':
            $ping = "http://api.moreover.com/ping?sitemap=$sitemap";
            break;
        default:
               return false;
    }

Next create a handle that curl then initialize it.

$curl_handle=curl_init();

Set the time the call will run for to stop delays in the event the service is down set this to a low number such as 2.

curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);

Next set a return tranfer in order to check if successful or not.

curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);

Next execute the stored commands and close the connection to curl.

$buffer = curl_exec($curl_handle);
    curl_close($curl_handle);

If you want to check if the ping was successful or not simply check if the $buffer is empty as the curl exec command as assigned to $buffer.

 if (empty($buffer))
    {
        echo "<p>Sorry, submission failed for $service.<p>";
    }
    else
    {
        echo "<p>$service success</p>";
    }

To call the function pass it the path to the sitemap and the service to ping.

pingSE('http://www.domain.com/sitemap.xml','bing');
pingSE('http://www.domain.com/sitemap.xml','ask');
pingSE('http://www.domain.com/sitemap.xml','google');
pingSE('http://www.domain.com/sitemap.xml','moreover');

Here's the full script:

function pingSE($sitemap,$service){

    switch ($service) {
        case 'bing':
            $ping = "http://www.bing.com/webmaster/ping.aspx?siteMap=$sitemap";
            break;
        case 'ask':
            $ping = "http://submissions.ask.com/ping?sitemap=$sitemap";
            break;
        case 'google':
            $ping = "http://www.google.com/webmasters/sitemaps/ping?sitemap=$sitemap";
            break;
        case 'moreover':
            $ping = "http://api.moreover.com/ping?sitemap=$sitemap";
            break;
        default:
               return false;
    }

    $curl_handle=curl_init();
    curl_setopt($curl_handle,CURLOPT_URL,$ping);
    curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
    curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
    $buffer = curl_exec($curl_handle);
    curl_close($curl_handle);

    if (empty($buffer))
    {
        echo "<p>Sorry, submission failed for $service.<p>";
    }
    else
    {
        echo "<p>$service success</p>";
    }

}

<h1>Update</h1>
For those who don't have curl installed on their server you can use a function called file_get_contents() instead here's an alternative function using file_get_contents()

function pingSE($sitemap,$service){

    switch ($service) {
        case 'bing':
            $ping = "http://www.bing.com/webmaster/ping.aspx?siteMap=$sitemap";
            break;
        case 'ask':
            $ping = "http://submissions.ask.com/ping?sitemap=$sitemap";
            break;
        case 'google':
            $ping = "http://www.google.com/webmasters/sitemaps/ping?sitemap=$sitemap";
            break;
        case 'moreover':
            $ping = "http://api.moreover.com/ping?sitemap=$sitemap";
            break;
        default:
               return false;
    }

    $success = file_get_contents($ping);

    if (empty($success))
    {
        echo "<p>Sorry, submission failed for $service.<p>";
    }
    else
    {
        echo "<p>$service success</p>";
    }

}

 

David Carr

David Carr

For the past 12 years, I’ve been developing applications for the web using mostly PHP. I do this for a living and love what I do as every day there is something new and exciting to learn.

In my spare time, the web development community is a big part of my life. Whether managing online programming groups and blogs or attending a conference, I find keeping involved helps me stay up to date. This is also my chance to give back to the community that helped me get started, a place I am proud to be apart of.

Besides programming I love spending time with friends and family and can often be found together going out catching the latest movie, staying in playing games on the sofa or planning a trip to someplace I’ve never been before.