我有一個PHP腳本,創建一個XML網站地圖。最後,我使用谷歌網站Ping成功
shell_exec('ping -c1 www.google.com/webmasters/tools/ping?sitemap=sitemapurl');
將更新後的站點地圖提交給Google網站站長工具。
閱讀Google文檔後,我不確定每次是否需要這樣做。通過手動輸入代碼中的鏈接,可以從google獲得成功頁面,但使用ping命令我不會收到確認。我還想知道是否有任何方法來檢查命令是否真正起作用。
我有一個PHP腳本,創建一個XML網站地圖。最後,我使用谷歌網站Ping成功
shell_exec('ping -c1 www.google.com/webmasters/tools/ping?sitemap=sitemapurl');
將更新後的站點地圖提交給Google網站站長工具。
閱讀Google文檔後,我不確定每次是否需要這樣做。通過手動輸入代碼中的鏈接,可以從google獲得成功頁面,但使用ping命令我不會收到確認。我還想知道是否有任何方法來檢查命令是否真正起作用。
這樣以來shell_exec()
,exec()
,passthru()
等命令是由許多託管封鎖,你應該使用curl
和檢查的200
響應代碼你也可以使用fsockopen
如果捲曲不可用。我將檢查代碼段並在找到答案時更新答案。
UPDATE:
發現了它。我知道我在某個地方使用過它。有趣的指令:它在我的Sitemap類中xD 你可以在github上找到它:https://github.com/func0der/Sitemap。它位於Sitemap \ SitemapOrg類中。 還有一個實施捲曲調用的例子。
無論哪種方式,這裏是獨立實施的代碼。
/**
* 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上開闢票/拉請求,請。 ;)
這裏是一個腳本來自動提交你的網站地圖,谷歌,冰/ 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 = "[email protected]";
$subject = "Sitemap ping fail: ".$pingedURL;
$message = "Error code ".$returnedCode.". Go check it out!";
$headers = "From: [email protected]";
if($returnedCode != "200") {
mail($to, $subject, $message, $headers);
}
}
希望幫助
秉建議另一個URL(FEB-2016): 'HTTP://www.bing.com/ping地圖上的地址= HTTP%3A%2F%2Fwww.example.com/sitemap.xml' 資源:[ Bing Sitemaps](https://www.bing.com/webmaster/help/how-to-submit-sitemaps-82a15bd4) – Calaelen
簡單的解決方案: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";
至於如何你應該經常這樣做,只要你的網站可以處理來自谷歌的機器人額外的流量,而不會減慢,你應該這樣做,每次已經做出了改變。
嗨,我怎麼知道這是否有效?我輸入了$ url http://www.google.com/webmasters/tools/ping?sitemap=http://www.example.com/sitemap.xml,但在運行腳本後我得到一個空白屏幕。 (顯然不是emaple.com) – RouthMedia
我試圖使用這個腳本提交站點地圖,但它沒有奏效。不知道我在做什麼錯 – RouthMedia
對不起,這個問題描述不是很準確,所以我幫不了你。 請更新您的問題(將其標記爲更新並附加新內容)並炫耀,您做了什麼,什麼不「工作」以及您期待的是什麼。 – func0der