Google Sitemap Ping成功

时间:2014-03-13 09:39:49

标签: php ping sitemap

我有一个创建xml站点地图的php脚本。最后,我使用

shell_exec('ping -c1 www.google.com/webmasters/tools/ping?sitemap=sitemapurl');

将更新后的站点地图提交给Google网站管理员工具。

阅读完Google文档后,我不确定每次是否需要这样做。手动输入代码中的链接,从谷歌获得成功页面,但使用ping命令我没有得到确认。我还想知道是否有任何方法可以检查命令是否确实有效。

3 个答案:

答案 0 :(得分:5)

这是一个自动将您的站点地图提交到谷歌,bing / msn并询问:

的脚本
/*
* 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.
*/

//Set this to be your site map URL
$sitemapUrl = "http://www.example.com/sitemap.xml";

// cUrl handler to ping the Sitemap submission URLs for Search Engines…
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;
}

//Google
$url = "http://www.google.com/webmasters/sitemaps/ping?sitemap=".$sitemapUrl;
$returnCode = myCurl($url);
echo "<p>Google Sitemaps has been pinged (return code: $returnCode).</p>";

//Bing / MSN
$url = "http://www.bing.com/ping?siteMap=".$sitemapUrl;
$returnCode = myCurl($url);
echo "<p>Bing / MSN Sitemaps has been pinged (return code: $returnCode).</p>";

//ASK
$url = "http://submissions.ask.com/ping?sitemap=".$sitemapUrl;
$returnCode = myCurl($url);
echo "<p>ASK.com Sitemaps has been pinged (return code: $returnCode).</p>";

如果提交失败,您也可以给自己发送电子邮件:

function return_code_check($pingedURL, $returnedCode) {

    $to = "webmaster@yoursite.com";
    $subject = "Sitemap ping fail: ".$pingedURL;
    $message = "Error code ".$returnedCode.". Go check it out!";
    $headers = "From: hello@yoursite.com";

    if($returnedCode != "200") {
        mail($to, $subject, $message, $headers);
    }
}

希望有所帮助

答案 1 :(得分:0)

由于许多托管商阻止了shell_exec()exec()passthru()等命令,因此您应使用curl并检查响应代码为200。

如果curl不可用,您也可以使用fsockopen。我将检查代码片段并在找到答案时更新答案。

<强>更新

找到它。我知道我在某处使用它。有趣的coincedence:它在我的Sitemap类xD中 你可以在github上找到它:https://github.com/func0der/Sitemap。它位于Sitemap \ SitemapOrg类中。 还有一个实现curl调用的例子。

无论哪种方式,这里都是独立实现的代码。

/**
 * Call url with fsockopen and return the response status.
 *
 * @param string $url
 *  The url to call.
 *
 * @return mixed(boolean|int)
 *  The http status code of the response. FALSE if something went wrong.
*/
function _callWithFSockOpen($url) {
    $result = FALSE;

    // Parse url.
    $url = parse_url($url);
    // Append query to path.
    $url['path'] .= '?'.$url['query'];

    // Setup fsockopen.
    $port = 80;
    $timeout = 10;
    $fso = fsockopen($url['host'], $port, $errno, $errstr, $timeout);

    // Proceed if connection was successfully opened.
    if ($fso) {
        // Create headers.
        $headers = 'GET ' . $url['path'] . 'HTTP/1.0' . "\r\n";
        $headers .= 'Host: ' . $url['host'] . "\r\n";
        $headers .= 'Connection: closed' . "\r\n";
        $headers .= "\r\n";

        // Write headers to socket.
        fwrite($fso, $headers);

        // Set timeout for stream read/write.
        stream_set_timeout($fso, $timeout);

        // Use a loop in case something unexpected happens.
        // I do not know what, but that why it is unexpected.           
        while (!feof($fso)){
            // 128 bytes is getting the header with the http response code in it.               
            $buffer = fread($fso, 128);

            // Filter only the http status line (first line) and break loop on success.
            if(!empty($buffer) && ($buffer = substr($buffer, 0, strpos($buffer, "\r\n")))){
                break;
            }
        }

        // Match status.
        preg_match('/^HTTP.+\s(\d{3})/', $buffer, $match);
        // Extract status.
        list(, $status) = $match;

        $result = $status;
    }
    else {
        // @XXX: Throw exception here??
    }

    return (int) $result;
}

如果您发现此代码有任何损害或改进,请不要犹豫,在GitHub上打开一个票/拉请求。 ;)

答案 2 :(得分:0)

最简单的解决方案:file_get_contents("https://www.google.com/webmasters/tools/ping?sitemap={$sitemap}");

这将适用于所有主要的托管服务提供商。如果您需要可选的错误报告,请点击此处:

$data = file_get_contents("https://www.google.com/webmasters/tools/ping?sitemap={$sitemap}");
$status = ( strpos($data,"Sitemap Notification Received") !== false ) ? "OK" : "ERROR";
echo "Submitting Google Sitemap: {$status}\n";

至于你应该多久使用一次,只要你的网站能够在不减速的情况下处理谷歌机器人的额外流量,你就应该在每次更改时都这样做。