当前位置:Gxlcms > PHP教程 > 手动给网站增添ping服务forphp

手动给网站增添ping服务forphp

时间:2021-07-01 10:21:17 帮助过:2人阅读

手动给网站添加ping服务for php

转自:http://lwxshow.com/php-setting-ping-manual-google-baidu

?

手动添加ping服务

【1】谷歌的ping服务的php的实现

关于RPC的详细介绍可以移步维基百科,谷歌ping服务的标准:
RPC端点: http://blogsearch.google.com/ping/RPC2
调用方法名: weblogUpdates.extendedPing
参数: (应按照如下所列的相同顺序传送)
站点名
站点URL
需要检查更新的页面URL
相应的RSS、RDF或Atom种子的URL
可选:页面内容的分类名称(或标签)。您可以指定多个值,之间用'|'字符进行分隔。
首先要写一个CURL的函数,来POST谷歌的RPC端点:

代码如下:

function postUrl($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);
curl_close ($ch);
return $res;
}

主要的curl写好了之后,剩下就是要根据谷歌的XML-RPC标准组装发送的数据了,详细的请求例子可以参考官方案例,点这里。
例如我的代码是这样写的:

代码如下:

$googleXML = <<

weblogUpdates.extendedPing


Ping服务的php实现方法,让网站快速被收录


http://s.jb51.net


http://s.jb51.net/archives/47.html


http://s.jb51.net/feed



END;
$res = postUrl(‘http://blogsearch.google.com/ping/RPC2′, $googleXML);
//下面是返回成功与否的判断(根据谷歌ping的接口说明)
if (strpos($res, "0"))
echo "PING成功";
else
echo "PING失败";

OK,这个就可以简单的实现谷歌的PING服务了。可以根据代码自行修改实现这个功能。
【*2】百度的ping服务的php的实现(这个标题真DT)
百度的ping服务xml代码是跟谷歌不同的,百度总是会有自己的特点:
介绍下百度博客ping服务,百度博客Ping服务的详细介绍,请移步:http://www.baidu.com/search/blogsearch_help.html#n7。
百度的ping服务也是基于XML-RPC标准协议,但是与谷歌ping服务不同的是百度的ping发送的xml格式不同,我们需要使用string节点包裹内容。
例如:

代码如下:



weblogUpdates.extendedPing


Ping服务的php实现方法,让网站快速被收录


http://s.jb51.net/


http://s.jb51.net/archives/47.html


http://s.jb51.net/feed


根据上面提到的谷歌接口,我们只要改变一下提交的xml内容即可,当然百度ping服务返回的判断也是跟谷歌的不同,也可以做相应的修改,
下面是php的代码:

代码如下:

$baiduXML = <<

weblogUpdates.extendedPing

Ping服务的php实现方法,让网站快速被收录
http://s.jb51.net
http://s.jb51.net/archives/47.html
http://s.jb51.net/feed


EOT;
$res = postUrl(‘http://ping.baidu.com/ping/RPC2′, $baiduXML);
//下面是返回成功与否的判断(根据百度ping的接口说明)
if (strpos($res, "0"))
echo "PING成功";
else
echo "PING失败";

上面的代码就可以实现php的ping服务了。好吧,下面再给各位看管提供一个百度的ping服务代码,没办法谁让他那么独特那?

代码如下:

function postUrl($url, $postvar)
{
$ch = curl_init();
$headers = array(
"POST ".$url." HTTP/1.0″,
"Content-type: text/xml; charset=\"gb2312\"",
"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);
curl_close ($ch);
return $res;
}
$baiduXML = "

weblogUpdates.extendedPing

脚本之家
http://www.gxlcms.com
http://www.gxlcms.com/a/15222.html
http://www.gxlcms.com

";
$res = postUrl(‘http://ping.baidu.com/ping/RPC2′, $baiduXML);
if ( strpos($res, "0") )
{
echo "PING成功";
}
else
{
echo "PING失败";
}
?>

人气教程排行