Tuesday, April 22, 2008

Share some ping code

I want to share some code. So I share this function that will help you to ping search engine for you sitemap :

function pingSitemaps( $url_xml, $searchEngine )
{
switch($searchEngine) {
case "google":
$domain = "www.google.com";
$get = "/webmasters/sitemaps/ping?sitemap=";
break;
case "ask":
$domain = "submissions.ask.com";
$get = "/ping?sitemap=";
break;
case "yahoo":
$domain = "search.yahooapis.com";
$get = "/SiteExplorerService/V1/ping?sitemap=";
break;
case "live":
$domain = "webmaster.live.com";
$get = "/ping.aspx?siteMap=";
break;
case "more":
$domain = "api.moreover.com";
$get = "/ping?u=";
break;
}

$status = 0;
if( $fp=@fsockopen($domain, 80) )
{
$req = 'GET '.$get.'' .
urlencode( $url_xml ) . " HTTP/1.1\r\n" .
"Host: $domain\r\n" .
"User-Agent: Mozilla/5.0 (compatible; " .
PHP_OS . ") PHP/" . PHP_VERSION . "\r\n" .
"Connection: Close\r\n\r\n";
fwrite( $fp, $req );
while( !feof($fp) )
{
if( @preg_match('~^HTTP/\d\.\d (\d+)~i', fgets($fp, 128), $m) )
{
$status = intval( $m[1] );
break;
}
}
fclose( $fp );
}

return( $status );
}

You use it like that :

if( 200 === ($status=pingSitemaps($siteMap, "google")) )
echo "Ping to Google Sitemaps successful.
Status code: $status.";
else
echo "Cannot ping/connect to Google Sitemaps.
Status code: $status.";

Hope it will help, it supports Google, Ask, Yahoo, MoreOver and Live.

0 comments: