Web site automatically push Baidu API source code (PHP source code)

  • <?php
  • //baidu push api
  • //https://ziyuan.baidu.com/linksubmit/index 这里提供的 api 接口,普通收录,主动提交,php代码
  • $api = 'http://data.zz.baidu.com/urls?site=网址&token=秘钥';
  • //get site urls
  • function GetUrls($api):string{
  • $site = "网站地图";
  • //获取 xml 信息
  • $XmlUrls = curl($site,null);
  • //将 xml 转为数组
  • $resource = xml_parser_create();
  • xml_parse_into_struct($resource,$XmlUrls,$value,$index);
  • xml_parser_free($resource);
  • //$urlArr 即为接受所有 url 的数组
  • $urlArr = [];
  • foreach ($value as $content){
  • if ($content["tag"] === "LOC"){
  • $urlArr[] = $content["value"];
  • }
  • }
  • //开始上传
  • $response = curl($api,$urlArr,1);
  • //将响应 json 转为 对象
  • $result = json_decode($response);
  • if ($result->success != 0){
  • return "主动提交成功!";
  • }else{
  • return "主动提交失败!";
  • }
  • }
  • function curl($url,$params,$is_post = 0){
  • $ch = curl_init();
  • curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,60);
  • curl_setopt($ch,CURLOPT_TIMEOUT,60);
  • curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
  • curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
  • curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);//禁止验证对等证书,默认为true
  • curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);//检查服务器SSL证书中是否存在一个公用名,值0|2,
  • if($is_post){
  • curl_setopt($ch,CURLOPT_POST,true);
  • curl_setopt($ch,CURLOPT_POSTFIELDS,$params);
  • curl_setopt($ch,CURLOPT_URL,$url);
  • }else{
  • if($params){
  • curl_setopt($ch,CURLOPT_URL,$url.'?'.$params);
  • }else{
  • curl_setopt($ch,CURLOPT_URL,$url);
  • }
  • }
  • $response = curl_exec($ch);
  • return $response;
  • }
  • echo GetUrls($api);

Web site automatically push Baidu API source code (PHP source code)

好了关于Web site automatically push Baidu API source code (PHP source code)的分享就到这里了,更多资讯请关注Peng Yue, right?!

THE END
分享
二维码


【免责声明】本站发布的信息大都来源于互联网,不代表本站立场;
如若转载请注明出处:https://pengy8.com/en/baidu-push-api-source-code/
如若侵犯了您的权益,投诉请联系我们,收到后立即处理。



< <上一篇
下一篇>>
目 录