Home >Backend Development >PHP Tutorial >百度实时推送api接口应用示例_PHP

百度实时推送api接口应用示例_PHP

WBOY
WBOYOriginal
2016-05-31 19:29:051186browse

网站质量不错的网站可以在百度站长平台/数据提交/sitemap栏目下看到实时推送的功能, 目前这个工具是邀请开放, 百度的实时推送的api接口可以实时推送我们新发布的文章, 保证百度在第一时间收录.
 
百度站长平台 http://zhanzhang.baidu.com/


打开百度站长平台, 点开实时推送的添加新数据接口获得带token的api推送地址:
 
  http://ping.baidu.com/sitemap?site=www.yourdomain.com&resource_name=sitemap&access_token=xxxxxxx 
 

分享一段网友写的php实时推送代码: 

php 实时推送新发布的文章

socketopen方式推送sitemap

代码如下:


 function sitemap_ping_baidu($urls){
         $baidu_ping_url = 'ping.baidu.com';
         $get = '/sitemap?site=www.yourdomain.com&resource_name=sitemap&access_token=xxxxxxx';
         $port=80;
         if ( ( $io = fsockopen( $baidu_ping_url, $port, $errno, $errstr, 50 ) ) !== false )  {
             $send = "POST $get HTTP/1.1"."rn";
             $send .= 'Accept: */*'."rn";
             $send .= 'Cache-Control: no-cache'."rn";
 
           $send .= 'Host: '.$baidu_ping_url."rn";
           $send .= 'Pragma: no-cache'."rn";
           //$send .= "Referer: http://".$url.$get."rn";
           //$send .= 'User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)'."rn";
           
           $xml = '';
           foreach($urls as $url){
               $xml .= '';
               $xml .= '';
               $xml .= ''.date('Y-m-d').'';
               $xml .= 'monthly';
               $xml .= '0.8';
               $xml .= '
';
           }
           $xml .= '
';

           $send .= 'Content-Length:'.strlen($xml)."rn";
           $send .= "Connection: Closernrn";

           $send .= $xml."rn";

           fputs ( $io, $send );

           $return = '';
           while ( ! feof ( $io ) )
           {
               $return .= fread ( $io, 4096 );
           }
           return $return;
       }else{
           return false;
       }
   }
$return = sitemap_ping_baidu(array('http://www.yourdomain.com/a.php?id=1'));


推送后百度会返回的xml文档

代码如下:


 
    
        
            
                
                     200
                

            
        
     
    

状态码含义如下

200 无使用方式错误,需要进一步观察返回的内容是否正确
400 必选参数未提供
405 不支持的请求方式,我们只支持POST方式提交数据
411 HTTP头中缺少Content-Length字段
413 推送的数据过大,超过了10MB的限制
422 HTTP头中Content-Length声明的长度和实际发送的数据长度不一致
500 站长平台服务器内部错误
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn