Soumettre un sitemap.xml à Google, et Bing

Bonjour,

Ce petit code va vous permettre de soumettre votre sitemap.xml aux 2 principaux moteurs de recherche, soit Google search et Bing.

<?php

class searchEngineSubmitter{
    
    private $urls;
    
    public function getUrls(){
        return  $this->urls ;
    }
    /**
     * @param array $urlsForSubmitting
     */
    public function __construct(array $urlsForSubmitting){
        $this->urls = $urlsForSubmitting;
    }
    
    /**
         * 
         * @param string $url
         * @return type
         */
    private function myCurl($url) {
        $ch       = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        return $httpCode;
    }
    
    /*
         * @abstract Sitemap Submitter Use this script to submit your site maps automatically to Google, Bing.MSN and Ask Trigger this script on a schedule of your choosing or after your site map gets updated.
     * @param string
    */
    public function submitSitemap($sitemapUrl) {

        foreach ( $this->getUrls() as $url) {
            $code = $this->myCurl($url . $sitemapUrl);
            if ($code == '200') {
                  return 'success: ' . parse_url($url, PHP_URL_HOST) . ' has been pinged (return code: ' . $code . ' )';
            } else {
                  return 'warning' . parse_url($url, PHP_URL_HOST) . ' return error (return code: ' . $code . ' )';
            }
        }
    }
    
}

Pour l’utiliser on fait comme ceci :

<?php

$submit = (new searchEngineSubmitter(
    ["http://www.google.com/webmasters/sitemaps/ping?sitemap=",
    "http://www.bing.com/webmaster/ping.aspx?siteMap="]))
    ->submitSitemap('http://www.mywebsite.com');

Et voilà 🙂

@ bientôt

Laisser un commentaire