仅供参考
<?php
set_time_limit(300);
class sitemap
{
private $key = '';
private $secret = '';
private $access_token;
/*入口*/
public function index()
{
$this->access_token = $this->getAccessToken();
if( !$this->access_token ){ echo "GET ACCESS_TOKEN ERROR!"; die();}
while(true){
$this->api();
sleep(2);
}
}
/**
* sitemap api 提交
* POST
*/
public function api()
{
$base = "https://openapi.baidu.com/rest/2.0/smartapp/access/submitsitemap/api?access_token=";
$url_list = $this->list();
if( !$url_list ){ echo "EMPTY CURL_LIST!"; die();}
$params=[
'access_token'=>$this->access_token,
'type'=>0, //周
'url_list'=>$url_list
];
$res = $this->curl($base.$this->access_token, 1, $params, 1);
if(!$res){ echo "CURL FAIL!"; die();}
$res = json_decode($res, true);
var_dump($res);
if( $res['errno'] == 0 ){ echo 'SUCCESS!'; }
switch ( $res['errno'] ) {
case '-1024':
echo '业务异常';die();
break;
case '500':
echo '对不起,服务器出错了,请稍候再试';die();
break;
case '2002':
echo '小程序不存在/选择的收录级别不合法/url数量不合法';die();
break;
case '30001':
echo '参数有误';die();
break;
case '30013':
echo '数量超上限';die();
break;
case '47005':
echo '文件上传失败';die();
break;
case '60005':
echo '尚未绑定熊掌ID,请先绑定熊掌ID';die();
break;
default:
break;
}
}
/*
* 生成path
* '/pages/question/question?qid='+ qid
*/
public function list()
{
$start = file_get_contents('tmp');
if( !$start ){ echo '请在tmp文件中写入初始页码'; }
$str = '';
for($i=1;$i<2000;$i++){
$qid = $start - $i;
$str .= "/pages/question/question?qid=".$qid.',';
}
$time = date('YmdHi');
file_put_contents('map-'.$time.'.txt', $str);
file_put_contents('record-'.date('Ymd').'.txt', 'start:'.$start.',end:'.$qid.PHP_EOL, FILE_APPEND);
file_put_contents('tmp', $qid);
$str = rtrim($str,',');
return $str;
}
/**
* 获取TOKEN
* POST
* 官方示例URL https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=Va5yQRHlA4Fq4eR&client_secret=0rDSjzQ20XUj5itV7WRtznPQS&scope=smartapp_snsapi_base
*/
private function getAccessToken()
{
$file = './accesstoken';
if( file_exists($file) ){
$content = file_get_contents( $file );
$content = json_decode($content, true);
if( time()-filemtime($file) < $content['expires_in'] ){
return $content['access_token'];
}
}
$params = [
'grant_type'=>'client_credentials',
'scope' =>'smartapp_snsapi_base',
'client_id'=>$this->key,
'client_secret'=>$this->secret
];
$res = $this->curl("https://openapi.baidu.com/oauth/2.0/token", 1, $params, 1);
if( $res == false ){ return false; }
$arr = json_decode($res, true);
if( !isset($arr['error']) ){
file_put_contents($file, $res);
return $arr['access_token'];
}
return false;
}
public function curl($url, $https = 0, $params = false, $ispost = 0, $header=[])
{
$httpInfo = [];
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($https) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
}
if ($ispost) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_URL, $url);
} else {
if ($params) {
if (is_array($params)) {
$params = http_build_query($params);
}
curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);
} else {
curl_setopt($ch, CURLOPT_URL, $url);
}
}
if(count($header)>0){
curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
}
$response = curl_exec($ch);
if ($response === false) {
//echo "cURL Error: " . curl_error($ch);
return false;
}
//$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
//$httpInfo = array_merge($httpInfo, curl_getinfo($ch));
curl_close($ch);
//$ret = json_decode($response, true);
return $response;
}
}
$obj = new sitemap();
$res = $obj->index();
建立一个tmp文件,填上初始的页码,在这个页码上递减
运行。