Recommended:

  • phpclasses.org
  • jsclasses.org
  • jsmag.com
  • siteapps.com
  • View our reviews on Hot Scripts
  • JS Tutorial
  • scripts.com
  • securesignup.com




Recent Comments

Powered by Disqus




Back to articles

Submit your sitemap to Google, Yahoo, Ask, Bing and Moreover

Here is a function that allows you easily submit your sitemap to search engines and services like Google, Yahoo, Ask, Bing and Moreover with passing just one parameter, your sitemap URL. Then you'll have a response array, which you can us to check if there any connection related curl errors, or submitting related error responses, or submitting confirmations from services.

<?php
//$sitemap_url - url of your site map like http://code-snippets.co.cc/sitemap.php
//$title - your site title (optional)
//$siteurl - your site url (optional)
function submit_sitemap($sitemap_url, $title ="", $siteurl = ""){
    // for curl handlers
    $curl_handlers = array();
    $response = array();
    $sitemap_url = trim($sitemap_url);
    if(strpos($sitemap_url, "http") !== 0)
    {
        $sitemap_url = "http://".$sitemap_url;
    }
    $site = explode("//", $sitemap_url);
    $start = $site[0];
    $site = explode("/", $site[1]);
    $middle = $site[0];
    if(trim($title) == "")
    {
        $title = $middle;
    }
    if(trim($siteurl) == "")
    {
        $siteurl = $start."//".$middle;
    }
    //urls to ping
    $urls[0] = "http://www.google.com/webmasters/tools/ping?sitemap=".urlencode($sitemap_url);
    $urls[1] = "http://www.bing.com/webmaster/ping.aspx?siteMap=".urlencode($sitemap_url);
    $urls[2] = "http://search.yahooapis.com/SiteExplorerService/V1/updateNotification".
            "?appid=YahooDemo&url=".urlencode($sitemap_url);
    $urls[3] = "http://submissions.ask.com/ping?sitemap=".urlencode($sitemap_url);
    $urls[4] = "http://rpc.weblogs.com/pingSiteForm?name=".urlencode($title).
            "&url=".urlencode($siteurl)."&changesURL=".urlencode($sitemap_url);

    //setting curl handlers
    foreach ($urls as $url) 
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $curl_handlers[] = $curl;
    }
    //initiating multi handler
    $multi_curl_handler = curl_multi_init();

    // adding all the single handler to a multi handler
    foreach($curl_handlers as $key => $curl)
    {
        curl_multi_add_handle($multi_curl_handler,$curl);
    }
    
    // executing the multi handler
    do 
    {
        $multi_curl = curl_multi_exec($multi_curl_handler, $active);
    } 
    while ($multi_curl == CURLM_CALL_MULTI_PERFORM  || $active);
    
    // check if there any error
    $submitted = true;
    foreach($curl_handlers as $key => $curl)
    {
        //you may use curl_multi_getcontent($curl); for getting content
        //and curl_error($curl); for getting errors
        if(curl_errno($curl) == CURLE_OK)
        {
            $response[] =  curl_multi_getcontent($curl);
        }
        else
        {
            $response[] = curl_error($curl);
        }
    }
    curl_multi_close($multi_curl_handler);
    return $response;
}

//getting response array
$arr = submit_sitemap("http://code-snippets.co.cc/sitemap.php");

//outputting response
foreach($arr as $val)
{
    echo "<p>".strip_tags($val)."</p>";
}
?>

You may also be interested in:

Powered by BlogAlike.com

blog comments powered by Disqus