<? php header('Content-Type:text/html; charset=utf-8'); /**Just modify the two links**/ $xml_url="https://Your domain name. com/sitemap. xml"// Modify the xml map link of your site here $baidu_api='Baidu webmaster gets the push interface link'// This is modified as the push interface you obtained from Baidu webmaster /***Just modify the two links**/ $xmldata =file_get_contents($xml_url); $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']; } //Baidu push interface only supports pushing 2000 pieces of data at a time, so if more than 2000 pieces of data are pushed, group push is required $url_group = array_chunk($url,2000); $count=count($url_group); for($i=0;$ i<$count;$i++){ $ch = curl_init(); $options = array( CURLOPT_URL => $baidu_api, CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_POSTFIELDS => implode("\n",$url_group[$i]), CURLOPT_HTTPHEADER => array('Content-Type:text/plain'), ); curl_setopt_array($ch, $options); $result =curl_exec($ch); curl_close($ch); echo $result; } ?>
<? php header('Content-Type:text/html; charset=utf-8'); // ini_set('max_execution_time',600);// Script timeout, in seconds, defined as needed /**Multiple sites only need to add arrays here**/ $data = array( array( 'url'=>'https://Your domain name. com/sitemap. xml',//here modify the xml map link of your site 'api'=>'The push interface link obtained by Baidu webmaster'//here is modified to the push interface obtained by Baidu webmaster ), array( 'url'=>'https://Your domain name. com/sitemap2. xml',//here modify the xml map link of your site 'api'=>'The push interface link obtained by Baidu webmaster'//here is modified to the push interface obtained by Baidu webmaster ), ); /**Multiple sites only need to add arrays here**/ foreach($data as $value){ echo '================================<br/>'; echo $value['url'].'< br/>'; post_Baidu($value['url'],$value['api']); } function post_Baidu($xml_url, $baidu_api) { $xmldata =file_get_contents($xml_url); $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']; } //Baidu push interface only supports pushing 2000 pieces of data at a time, so if more than 2000 pieces of data are pushed, group push is required $url_group = array_chunk($url,2000); $count=count($url_group); for($i=0;$ i<$count;$i++){ $ch = curl_init(); $options = array( CURLOPT_URL => $baidu_api, CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_POSTFIELDS => implode("\n",$url_group[$i]), CURLOPT_HTTPHEADER => array('Content-Type:text/plain'), ); curl_setopt_array($ch, $options); $result =curl_exec($ch); curl_close($ch); echo $result.'<br/>'; } } ?>