百度多网址自动推送工具

百度站长平台提供链接索引的自动提交 JS 代码脚本。用百度自己的话讲:JS链接推送代码以网页为最小对象,服务于全平台多终端,PC站和移动站均可使用。安装代码的页面在任意平台(浏览器、微信、微博)被加载时,页面链接会被第一时间推送给百度,从而提高站点新内容的发现速度。

所以我用PHP写了一段基于百度自动推送的代码

演示网址:https://www.bunian.cn/gongjv/push/

<?php
/**
*不念博客  https://www.bunian.cn
*/
header('Access-Control-Allow-Origin:*');
header('Content-type:text/html;charset=utf-8');
error_reporting(0); 

if(!$_GET['mod'])exit;

if($_GET['mod']=='update'){
	if(update()){
		echo '自动推送链接(https、http端)更新成功';
	}else{
		echo '更新失败,请联系管理员';
		
	}

}

if($_GET['mod']=='push' AND $_POST['urls']){
	$urls=explode("\n", $_POST['urls']);
	echo push($urls);
	
}

if($_GET['mod']=='sitemap' AND $_POST['urls']){

	$data=curl($_POST['urls']);
	//print_r($_POST['urls']);
	echo json_encode(sitemap($data));
}



function curl($url,$referer=0)
{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,$url);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		$httpheader[] = "Accept:*/*";
		$httpheader[] = "Accept-Encoding:gzip,deflate,sdch";
		$httpheader[] = "Accept-Language:zh-CN,zh;q=0.8";
		$httpheader[] = "Connection:close";
		curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
		if($referer){
			curl_setopt($ch, CURLOPT_REFERER, $referer);
		}
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:123.125.71.128', 'CLIENT-IP:123.125.71.128')); 
		curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3314.0 Safari/537.36 SE 2.X MetaSr 1.0');
		curl_setopt($ch, CURLOPT_ENCODING, "gzip");
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		$ret = curl_exec($ch);
		curl_close($ch);

		return $ret;
}



//更新
function update(){
//https
$https=curl('https://zz.bdstatic.com/linksubmit/push.js');
preg_match('/var o="(.*?)"/is', $https,$https);
$https=$https??'';
//http
$http=curl('http://push.zhanzhang.baidu.com/push.js');
preg_match('/var n="(.*?)"/is', $http,$http);

$http=$http??'';

	if($https AND $http){
		file_put_contents("url.php",'<?php $http=\'http:'.$http['1'].'\';$https=\''.$https['1'].'\';');
		return true;	
	}else{
		return false;
	}
}
//提交
function push($urls=[]){
	require_once 'url.php';
	$urlm=urlencode($urls['0']??'https://www.aizhan.com/');
	foreach($urls as $v){
		$url=$http;
		 if(strpos($v,'https://') !==false){
			$url=$https;
		 }
		$upurls[]=$url.'?r='.$urlm.'&?l='. link_urldecode($v) ;
		//echo '<img src="'.$url.'?r='.$urlm.'&?l='.$v.'" border="0" style="display:none;"/>';
	}
	//print_r($upurls);
	return json_encode($upurls);

}

//SiteMap地图解析
function sitemap($data){
	if(strpos($data,'<urlset') !==false){
		preg_match_all('/<loc>(http:\/\/|https:\/\/)(.*?)<\/loc>/is', $data,$data);
		$count=count($data['1']);
		for($i=0;$i<$count;$i++){
			$urls[]=$data['1'][$i].$data['2'][$i];
		}
	}else{
		$urls=explode("\n",$data);

	}
	return $urls;
}

//URL转码
function link_urldecode($url) {
  $uri = '';
  $cs = unpack('C*', $url);
  $len = count($cs);
  for ($i=1; $i<=$len; $i++) {
    $uri .= $cs[$i] > 127 ? '%'.strtoupper(dechex($cs[$i])) : $url{$i-1};
  }
  return $uri;
}


//xml转为数组
function xmltoarr($path){//xml字符串转数组
        $xml= $path;//XML文件
        $objectxml = simplexml_load_string($xml);//将文件转换成 对象
        $xmljson= json_encode($objectxml );//将对象转换个JSON
        $xmlarray=json_decode($xmljson,true);//将json转换成数组
        return $xmlarray;
    }