当前位置:首页 > PHP教程 > php应用 > 列表

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

发布:smiling 来源: PHP粉丝网  添加日期:2020-08-31 16:22:08 浏览: 评论:0 

使用方法访问的时候自动ping到百度,我的方法是生成缓存,更新缓存的时候自动ping百度,返回结果为0表示成功,代码如下:

  1. $pingarcurl='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; 
  2. $arcping = new Ping('php粉丝网',$pingarcurl,get_domain(),'https://www.phpfensi.com'); 
  3. $arcping->pingbaidu(); 

代码如下:

  1. class ping{ 
  2. private$title;//博客名称 
  3. private$hosturl;//博客首页地址 
  4. private$arturl;//新发文章地址 
  5. private$rssurl;//博客rss地址 
  6. private$baiduXML;//百度XML结构 
  7. private$baiduRPC;//百度XML地址 
  8.  
  9. publicfunction__construct($title,$arturl,$hosturl,$rssurl
  10. if(emptyempty($title)||emptyempty($arturl)) 
  11. returnfalse; 
  12. $this->title=$title
  13. $this->hosturl=$hosturl
  14. $this->rssurl=$rssurl
  15. $this->arturl=$arturl
  16. $this->baiduRPC='http://ping.baidu.com/ping/RPC2'
  17.  
  18. $this->baiduXML='<?xmlversion=\"1.0\"encoding=\"UTF-8\"?>'
  19. $this->baiduXML.='<methodCall>'
  20. $this->baiduXML.='<methodName>weblogUpdates.extendedPing</methodName>'
  21. $this->baiduXML.='<params>'
  22. $this->baiduXML.='<param><value><string>'.$this->hosturl.'</string></value></param>'
  23. $this->baiduXML.='<param><value><string>'.$this->title.'</string></value></param>'
  24. $this->baiduXML.='<param><value><string>'.$this->arturl.'</string></value></param>'
  25. $this->baiduXML.='<param><value><string>'.$this->rssurl.'</string></value></param>'
  26. $this->baiduXML.='</params>'
  27. $this->baiduXML.='</methodCall>'
  28. publicfunctionpingbaidu() 
  29. $ch=curl_init(); 
  30. $headers=array
  31. 'User-Agent:request'
  32. 'Host:ping.baidu.com'
  33. 'Content-Type:text/xml'
  34. ); 
  35. curl_setopt($ch,CURLOPT_URL,$this->baiduRPC); 
  36. curl_setopt($ch,CURLOPT_HEADER,1); 
  37. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 
  38. curl_setopt($ch,CURLOPT_POST,1); 
  39. curl_setopt($ch,CURLOPT_HTTPHEADER,$headers); 
  40. curl_setopt($ch,CURLOPT_POSTFIELDS,$this->baiduXML); 
  41. $res=curl_exec($ch); 
  42. curl_close($ch); 
  43. //return$res; 
  44. return(strpos($res,"<int>0</int>"))?true:false; 
  45. functionget_domain() 
  46. /*协议*/ 
  47. $protocol='http://'
  48. /*域名或IP地址*/ 
  49. if(isset($_SERVER['HTTP_X_FORWARDED_HOST'])){ 
  50. $host=$_SERVER['HTTP_X_FORWARDED_HOST']; 
  51. }elseif(isset($_SERVER['HTTP_HOST'])){ 
  52. $host=$_SERVER['HTTP_HOST']; 
  53. }else
  54. /*端口*/ 
  55. if(isset($_SERVER['SERVER_PORT'])){ 
  56. $port=':'.$_SERVER['SERVER_PORT']; 
  57. if((':80'==$port&'http://'==$protocol)||(':443'==$port&'https://'==$protocol)){ 
  58. $port=''
  59. }else
  60. $port=''
  61.  
  62. if(isset($_SERVER['SERVER_NAME'])){ 
  63. $host=$_SERVER['SERVER_NAME'].$port
  64. }elseif(isset($_SERVER['SERVER_ADDR'])){ 
  65. $host=$_SERVER['SERVER_ADDR'].$port
  66.  
  67. return$protocol.$host
  68. //$arc=newPing('网站标题','更新的URL地址','网站域名','rss地址'); 
  69. //$arc=newPing('你网站的title',get_arcurl($id),get_domain(),'http://www.abc.com/rss.php'); 
  70. //echo$arc->pingbaidu(); 
  71. //返回结果为0表示成功。 

Tags: 自动ping百度

分享到: