Php Google站点地图Ping成功

Php Google站点地图Ping成功,php,ping,sitemap,Php,Ping,Sitemap,我有一个创建xml站点地图的php脚本。最后,我使用 shell_exec('ping -c1 www.google.com/webmasters/tools/ping?sitemap=sitemapurl'); 将更新后的网站地图提交给谷歌网站管理员工具 阅读了谷歌文档后,我不确定是否每次都需要这样做。在代码中手动输入链接,会得到一个来自google的成功页面,但是使用ping命令我没有收到任何确认。我还想知道是否有任何方法可以检查该命令是否实际工作。由于像shell\u exec()、ex

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

shell_exec('ping -c1 www.google.com/webmasters/tools/ping?sitemap=sitemapurl');
将更新后的网站地图提交给谷歌网站管理员工具


阅读了谷歌文档后,我不确定是否每次都需要这样做。在代码中手动输入链接,会得到一个来自google的成功页面,但是使用ping命令我没有收到任何确认。我还想知道是否有任何方法可以检查该命令是否实际工作。

由于像
shell\u exec()
exec()
passthru()
等命令都被许多宿主阻止,因此您应该使用
curl
并检查200的响应代码

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

更新

找到了。我知道我在什么地方用过。有趣的巧合是:它出现在我的Sitemap类xD中 您可以在github上找到它:。它位于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上打开一个ticket/pull请求

由于诸如
shell\u exec()
exec()
passthru()
等命令被许多主机阻止,因此您应该使用
curl
并检查200的响应代码

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

更新

找到了。我知道我在什么地方用过。有趣的巧合是:它出现在我的Sitemap类xD中 您可以在github上找到它:。它位于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上打开一个ticket/pull请求

这里有一个脚本,可以自动将您的站点地图提交到google、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>";

希望这有帮助

这里有一个脚本,可以自动将您的站点地图提交到google、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>";

希望这有助于最简单的解决方案:
文件获取内容(“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";

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

最简单的解决方案:
文件获取内容(“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";

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

嗨,我怎么知道这是否有效?我已经输入了$url,但在运行脚本后,我会看到一个空白屏幕。(显然不是使用emaple.com)我曾尝试使用此脚本提交站点地图,但没有成功。不确定我做错了什么对不起,那个问题描述不太准确,所以我帮不了你。请更新您的问题(将其标记为更新并附加新内容),并展示您所做的和未“工作”的内容,以及您期望的内容。嗨,我如何知道这是否有效?我已经输入了$url,但在运行脚本后,我会看到一个空白屏幕。(显然不是使用emaple.com)我曾尝试使用此脚本提交站点地图,但没有成功。不确定我做错了什么对不起,那个问题描述不太准确,所以我帮不了你。请更新您的问题(将其标记为更新并附加新内容),并展示您所做的和未“工作”的内容,以及您期望的内容。Bing推荐另一个url(2016年2月):
http://www.bing.com/ping?sitemap=http%3A%2F%2Fwww.example.com/sitemap.xml
Resource:@Calaelen我认为用
http%3A%2F%2F
替换
http://
并不重要-两者都给出了相同的结果。必应推荐另一个url(2016年2月):
http://www.bing.com/ping?sitemap=http%3A%2F%2Fwww.example.com/sitemap.xml
Resource:@Calaelen我认为用
http%3A%2F%2F
替换
http://
并不重要-两者都给出了相同的结果。