谷动谷力

 找回密码
 立即注册
查看: 1423|回复: 0
打印 上一主题 下一主题
收起左侧

获取网站地图自动推送的PHP代码

[复制链接]
跳转到指定楼层
楼主
发表于 2021-11-21 19:16:38 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 sunsili 于 2021-11-21 19:20 编辑

                                       
获取网站地图自动推送的PHP代码


自定义下述代码为任意文件名的php文件,配置好您的api,可实现自动读取网站的sitemap.xml地图文件,并像百度默认推送100条链接。
可通过计划任务进行定时定量推送,各位自行琢磨!
  1. <?php
  2. $token='http://data.zz.baidu.com/urls?site=www.sunsili.com&token=xxxxx';//去百度站长平台获取
  3. $xml_string = file_get_contents("http://www.xxx.cn/sitemap.xml");//网站xml地图
  4. $xml_string = trim($xml_string);
  5. $xml_object = simplexml_load_string($xml_string);
  6. $array=[];
  7. $i=0;
  8. foreach ($xml_object->url as $key=>$value){
  9.     //默认一次提交100,适合用于泛目录的xml地图(因为刷新一次就随机变),普通的网站的会出现重复提交,慎用,非要用就把$i数值改成2000 百度不允许超过2000
  10.     if($i===100){
  11.         break;
  12.     }
  13.     $array['aa']= get_object_vars($value);
  14.     $url.=$array['aa']['loc']."\n";
  15.     $i++;
  16. }
  17. $urls=explode("\n",$url);
  18. $ch = curl_init();
  19. $options =  array(
  20.     CURLOPT_URL => $token,
  21.     CURLOPT_POST => true,
  22.     CURLOPT_RETURNTRANSFER => true,
  23.     CURLOPT_POSTFIELDS => implode("\n", $urls),
  24.     CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
  25. );
  26. curl_setopt_array($ch, $options);
  27. $result = curl_exec($ch);
  28. echo $result;
  29. ?>
复制代码



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|深圳市光明谷科技有限公司|光明谷商城|Sunshine Silicon Corpporation ( 粤ICP备14060730号|Sitemap

GMT+8, 2024-4-26 03:02 , Processed in 0.064728 second(s), 33 queries .

Powered by Discuz! X3.2 Licensed

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表