php实现了简单ping google代码,加快google收录
1.用wordpress发现一个好用的功能就是能够自动将发布的内容通知搜索引擎,看到很是实用,于是自己便用php实现了一个简单功能的实现ping google具体代码如下:
<?php
function post($url, $postvar) {
$ch = curl_init();
$headers = array(
"POST ".$url." HTTP/1.0",
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Content-length: ".strlen($postvar)
);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postvar);
$res = curl_exec ($ch);
//print_r(curl_getinfo($ch));
curl_close ($ch);
return $res;
}
// methodName 调用的方法名称
// $site_name 站点名称
// $site_url 站点的url
// $update_url 需要提交的url,一般是新的需要收录的url
// $update_rss 一般是sitemap,整站的引导的一类东西
/*$googleXML="
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<methodCall>
<methodName>weblogUpdates.extendedPing</methodName>
<params>
<param><value>{$site_name}</value></param>
<param><value>{$site_url}</value></param>
<param><value>{$update_url}</value></param>
<param><value>{$update_rss}</value></param>
</params>
</methodCall>";*/
$googleXML="
<?xml version='1.0' encoding='UTF-8'?>
<methodCall>
<methodName>weblogUpdates.extendedPing</methodName>
<params>
<param><value>shi bang english website</value></param>
<param><value>http://www.sbmchina.com/</value></param>
<param><value>http://www.sbmchina.com/topic/shanxijinmei/index.html</value></param>
<param><value>http://www.sbmchina.com/topic/shanxijinmei/index.html</value></param>
</params>
</methodCall>";
$res = post('http://blogsearch.google.com/ping/RPC2', $googleXML);
echo $res;
?>
如果成功返回结果如下
<?xml version="1.0"?>
<methodResponse><params>
<param><value><struct>
<member>
<name>flerror</name><value><boolean>0</boolean></value>
</member>
<member>
<name>message</name><value>Thanks for the ping.</value>
</member>
</struct></value></param>
</params></methodResponse>
2.直接通过get方式向google站长工具发送请求,提交sitemap
<?php
//http://www.google.com/webmasters/sitemaps/ping?sitemap=http://www.jbxue.com/sitemap.xml
function ping_google(){
$sitemapUrl = 'http://wubiao.site/sitemap.xml';
$pingUrl = "http://www.google.com/webmasters/sitemaps/ping?sitemap=".urlencode($sitemapUrl);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $pingUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch) or die (curl_error()); //执行curl请求
curl_close($ch);
return $result;
}
echo ping_google();
?>
返回结果