百度自动推送php实现代码

PHP与SEO 思享 841浏览
摘要:
我们知道主动推送有助于搜索引擎抓取和收录网站内容,wordpress或者主流CMS其实已经集成了推送功能。但是如果我们要定期归还,而不是只在发布的时候推一次。这个功能可以通过结合PHP和Pagoda Panel的预定任务来实现。$api = '百度站长的推送接口';

我们知道主动推送有助于搜索引擎对网站内容进行抓取收录,wordpress或主流的CMS其实都集成了推送功能。但是如果我们想定时退送,而不是只是在发布的时候推送一次。可以结合PHP和宝塔面板的计划任务实现这一功能

php代码

<?php
header('Content-Type:text/html;charset=utf-8');
$xmldata =file_get_contents("https://自己网站/sitemap.xml");
$xmlstring = simplexml_load_string($xmldata,'SimpleXMLElement',LIBXML_NOCDATA);
$value_array = json_decode(json_encode($xmlstring),true);
$url = [];
for ($i =0;$i < count($value_array['url']);$i++){
echo $value_array['url'][$i]['loc']."<br/>";
$url[]= $value_array['url'][$i]['loc'];
}
$api ='百度站长的推送接口';
$ch = curl_init();
$options = array(
CURLOPT_URL => $api,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => implode("n",$url),
CURLOPT_HTTPHEADER => array('Content-Type:text/plain'),
);
curl_setopt_array($ch, $options);
$result =curl_exec($ch);
echo $result;
?>

在网站根目录下新建php文件,文件名随意,例baidu.php,填写网站sitemap.xml地址和百度站长的推送接口,把自定义的文件地址添加在宝塔计划任务,选择访问URL,自定义执行时间后,保存即可。

推荐阅读

Function get_magic_quotes_gpc() is deprecated报错解决办法

在 PHP 7.4 及更高版本中, get_magic_quotes_gpc() 函数已被弃用。 This means that code using this function will raise a warning or error.以下是解决......

PHP提示:Warning: count():Parameter must be an array or an object that implements Countable

错误原因:PHP7.2以后,count()函数的参数无效时会抛出warning警告。...

PHP7.4 报错:Deprecated Functionality: implode(): Passing glue string after array is deprecated.

PHP7.4运行项目报错:Deprecated Functionality: implode(): Passing glue string after array is deprecated。只需交换 implode() 函数的两个参数!...