How to ping search engines with curl

David Carr

Tutorials PHP & MySQL

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>";
    }

}

 

Laravel Modules Your Logo Your Logo Your Logo

Become a sponsor

Help support the blog so that I can continue creating new content!

Sponsor

My Latest Book

Modular Laravel Book - Laravel: The Modular way

Learn how to build modular applications with Laravel Find out more

Subscribe to my newsletter

Subscribe and get my books and product announcements.

Learn Laravel with Laracasts

Faster Laravel Hosting

© 2006 - 2024 DC Blog. All code MIT license. All rights reserved.