百度ping方法使用示例 自动ping百度
使用方法访问的时候自动ping到百度,我的方法是生成缓存,更新缓存的时候自动ping百度,返回结果为0表示成功。
$pingarcurl='http://'.$_server['http_host'].$_server['request_uri'];
$arcping = new ping('',$pingarcurl,get_domain(),'//www.jb51.net');
$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='<?xmlversion=\"1.0\"encoding=\"utf-8\"?>';
$this->baiduxml.='<methodcall>';
$this->baiduxml.='<methodname>weblogupdates.extendedping</methodname>';
$this->baiduxml.='<params>';
$this->baiduxml.='<param><value><string>'.$this->hosturl.'</string></value></param>';
$this->baiduxml.='<param><value><string>'.$this->title.'</string></value></param>';
$this->baiduxml.='<param><value><string>'.$this->arturl.'</string></value></param>';
$this->baiduxml.='<param><value><string>'.$this->rssurl.'</string></value></param>';
$this->baiduxml.='</params>';
$this->baiduxml.='</methodcall>';
}
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,1);
curl_setopt($ch,curlopt_post,1);
curl_setopt($ch,curlopt_httpheader,$headers);
curl_setopt($ch,curlopt_postfields,$this->baiduxml);
$res=curl_exec($ch);
curl_close($ch);
//return$res;
return(strpos($res,"<int>0</int>"))?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),get_domain(),'http://www.abc.com/rss.php');
//echo$arc->pingbaidu();
//返回结果为0表示成功。