PHP Ping服务背景与注意事项
此前曾发布过一个基于类的PHP Ping服务代码,但不少用户反馈难以理解或直接集成使用。鉴于此,本文提供一份可直接嵌入项目使用的独立Ping函数版本,希望能为大家提供帮助。需要特别说明的是,当前的Ping机制效果已不如从前,因为百度和Google等搜索引擎可能增加了其他排名指标。即使Ping操作显示成功,也不代表网站内容会被百分之百收录。
具体实现代码如下:
<?php
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 = "
<?xml version=\"1.0\"
encoding=\"gb2312\"?>weblogUpdates.extendedPing
WEB开发笔记
https://www.jb51.net
https://www.jb51.net/001
https://www.jb51.net
";
$res
= postUrl('http://ping.baidu.com/ping/RPC2', $baiduXML);
if ( strpos($res, "
0") ){
echo
"PING成功";
}
else
{
echo "PING失败";
}
?>