zl程序教程

您现在的位置是:首页 >  云平台

当前栏目

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

2023-06-13 09:15:29 时间

网站质量不错的网站可以在百度站长平台/数据提交/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

复制代码代码如下:


 functionsitemap_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$getHTTP/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;MSIE8.0;WindowsNT6.1;Trident/4.0)"."rn";
           
          $xml="<?xmlversion="1.0"encoding="UTF-8"?><urlset>";
          foreach($urlsas$url){
              $xml.="<url>";
              $xml.="<loc><![CDATA[".$url."]]></loc>";
              $xml.="<lastmod>".date("Y-m-d")."</lastmod>";
              $xml.="<changefreq>monthly</changefreq>";
              $xml.="<priority>0.8</priority>";
              $xml.="</url>";
          }
          $xml.="</urlset>";

          $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{
          returnfalse;
      }
  }
$return=sitemap_ping_baidu(array("http://www.yourdomain.com/a.php?id=1"));


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

复制代码代码如下:


 <?xmlversion="1.0"encoding="UTF-8"?>
    <methodResponse>
        <params>
            <param>
                <value>
                    <int>200</int>
                </value>
            <param>
        </params>     
    </methodResponse>

状态码含义如下

200 无使用方式错误,需要进一步观察返回的内容是否正确
400 必选参数未提供
405 不支持的请求方式,我们只支持POST方式提交数据
411 HTTP头中缺少Content-Length字段
413 推送的数据过大,超过了10MB的限制
422 HTTP头中Content-Length声明的长度和实际发送的数据长度不一致
500 站长平台服务器内部错误