百度ping方法使用示例 自动ping百度

前端之家收集整理的这篇文章主要介绍了百度ping方法使用示例 自动ping百度前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

使用方法访问的时候自动ping百度,我的方法生成缓存,更新缓存的时候自动ping百度,返回结果为0表示成功。

代码如下:
$pingarcurl='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$arcping = new Ping('编程之家',$pingarcurl,get_domain(),'//www.jb51.cc');
$arcping->pingbaidu();

代码如下:
class ping{
private$title;//博客名称
private$hosturl;//博客首页地址
private$arturl;//新发文章地址
private$RSSurl;//博客RSS地址
private$baiduXML;//百度XML结构
private$baiduRPC;//百度XML地址

publicfunction__construct($title,$arturl,$hosturl,$RSSurl)
{
if(empty($title)||empty($arturl))
returnfalse;
$this->title=$title;
$this->hosturl=$hosturl;
$this->RSSurl=$RSSurl;
$this->arturl=$arturl;
$this->baiduRPC='http://ping.baidu.com/ping/RPC2';

$this->baiduXML='';
$this->baiduXML.='';
$this->baiduXML.='weblogUpdates.extendedPing';
$this->baiduXML.='';
$this->baiduXML.=''.$this->hosturl.'';
$this->baiduXML.=''.$this->title.'';
$this->baiduXML.=''.$this->arturl.'';
$this->baiduXML.=''.$this->RSSurl.'';
$this->baiduXML.='
';
$this->baiduXML.='
';
}
publicfunctionpingbaidu()
{
$ch=curl_init();
$headers=array(
'User-Agent:request',
'Host:ping.baidu.com',
'Content-Type:text/xml',
);
curl_setopt($ch,CURLOPT_URL,$this->baiduRPC);
curl_setopt($ch,CURLOPT_HEADER,1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,CURLOPT_POST,CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch,CURLOPT_POSTFIELDS,$this->baiduXML);
$res=curl_exec($ch);
curl_close($ch);
//return$res;
return(strpos($res,"0"))?true:false;
}
}
functionget_domain()
{
/*协议*/
$protocol='http://';
/*域名或IP地址*/
if(isset($_SERVER['HTTP_X_FORWARDED_HOST'])){
$host=$_SERVER['HTTP_X_FORWARDED_HOST'];
}elseif(isset($_SERVER['HTTP_HOST'])){
$host=$_SERVER['HTTP_HOST'];
}else{
/*端口*/
if(isset($_SERVER['SERVER_PORT'])){
$port=':'.$_SERVER['SERVER_PORT'];
if((':80'==$port&'http://'==$protocol)||(':443'==$port&'https://'==$protocol)){
$port='';
}
}else{
$port='';
}

if(isset($_SERVER['SERVER_NAME'])){
$host=$_SERVER['SERVER_NAME'].$port;
}elseif(isset($_SERVER['SERVER_ADDR'])){
$host=$_SERVER['SERVER_ADDR'].$port;
}
}

return$protocol.$host;
}
//$arc=newPing('网站标题','更新的URL地址','网站域名','RSS地址');
//$arc=newPing('你网站的title',get_arcurl($id),'http://www.abc.com/RSS.PHP');
//echo$arc->pingbaidu();
//返回结果为0表示成功。

猜你在找的PHP相关文章