btamas86
2/21/2014 - 7:27 PM

Ping search engines

Ping search engines

<?php

function pingSearchEngines($sitemap, $service)
{
    $ping = '';
    switch (strtolower($service)) {
        case 'google':
            $ping = 'http://www.google.com/webmasters/sitemaps/ping?sitemap=[SITEMAP]';
            break;
        case 'bing':
            $ping = 'http://www.bing.com/webmaster/ping.aspx?siteMap=[SITEMAP]';
            break;
        case 'ask':
            $ping = 'http://submissions.ask.com/ping?sitemap=[SITEMAP]';
            break;
    }

    if ($ping === '') {
        throw new Exception('Service not exists.');
    }

    $ping = str_replace('[SITEMAP]', urlencode($sitemap), $ping);

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

    if (empty($buffer)) {
        throw new Exception("Sorry, submission failed for {$service}.");
    }

    return true;
}

pingSearchEngines('http://example.com/sitemap.xml', 'google');