赏学吧爬题API接口案例

接口调用方式

1.xxx.php?action=query&wd=关键词

2.xxx.php?action=data&id=题库ID

源代码

<?php   
/*****
*作者:蓝优
QQ群:443262323
*****/
header("Access-Control-Allow-Origin:*");
header('Content-type: application/json'); 
!empty($_REQUEST['action']) ? $action = $_REQUEST['action'] : exit(json_encode(['code'=>202,"msg"=>"缺少参数"],JSON_UNESCAPED_UNICODE));
if ($_REQUEST['action'] == 'query') {
	$api = myCurl('https://m.shangxueba.cn/search.aspx?Page='.$_REQUEST['pg'].'&key='.$_REQUEST['wd']);
	$list_preg = '/<p class="result_tit">.+<\/p>/Us';
	preg_match_all($list_preg,$api,$list);
	$min = str_replace(array(" "," ","\t","\n","\r"),"",$list[0]);//清除换行,空格
	foreach ($min as $var) {
		$a = 'href="';
		$b = '.html';
		$id = GetBetween($var,$a,$b);
		$a = 'search_title">';
		$b = '</a>';
		$title_one = GetBetween($var,$a,$b);
		$title = str_replace(array("<em>","</em>"),"",$title_one);
		$item[] = array('title'=>$title ,'id'=>$id);
	}
	$a = '最后页" href="?Page=';
	$b = '&key=';
	$count = GetBetween($api,$a,$b);
	if($item){
		$value = array('code'=>200,'msg'=>'成功','count'=>$count,'data'=>$item);
	}else{
		$value = array('code'=>202,'msg'=>'失败');
	}	
}
if ($_REQUEST['action'] == 'data') {
	$api = myCurl('https://m.shangxueba.cn/'.$_REQUEST['id'].'.html');
	$list_preg = '/<div class="s_mess2_m">.+<\/div>/Us';
	preg_match_all($list_preg,$api,$list);
	foreach ($list[0] as $k =>$var) {
		preg_match('/<p>(.*)<\/p>/',$var,$title);
		$explode = explode("</p><p>",$title[1]);
	}
	$api = myCurl('https://m.shangxueba.cn/GetAnswer.ashx?id='.$_REQUEST['id']);
	$data = json_decode($api,true);
	foreach ($data['data'] as $vue) {
		$str = str_replace("<br/>","",$vue["Context"]);
		$suv[] = array('time'=>$vue["AddDate"],'content'=>$str);	
	}
	if($suv){
		$value = array('code'=>200,'msg'=>'成功','data'=>$explode,'key'=>$suv);
	}else{
		$value = array('code'=>202,'msg'=>'失败');
	}	
}
echo json_encode($value,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE);

function myCurl($url){
    $ch = curl_init();     // Curl 初始化  
    $timeout = 30;     // 超时时间:30s  
    $ua='Mozilla/5.0( Linux; Android 8.1.0; PBCM30 Build/OPM1.171019011; wv)Apple Webkit/ 537.36(KHTML, like Gecko) Version/4.0 Chrome/62.0.3202. 84 Mobile Safari/537.36';
	$ip = mt_rand(11, 191) . "." . mt_rand(0, 240) . "." . mt_rand(1, 240) . "." . mt_rand(1, 240);
    curl_setopt($ch, CURLOPT_URL, $url);              // 设置 Curl 目标  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);      // Curl 请求有返回的值  
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);     // 设置抓取超时时间  
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);        // 跟踪重定向  
    curl_setopt($ch, CURLOPT_REFERER, $url);   // 伪造来源网址  
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:'.$ip, 'CLIENT-IP:'.$ip));  //伪造IP  
    curl_setopt($ch, CURLOPT_USERAGENT, $ua);   // 伪造ua   
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts  
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
	curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); //强制协议为1.0
	curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 ); //强制使用IPV4协议解析域名
    $content = curl_exec($ch);   
    curl_close($ch);    // 结束 Curl  
    return $content;    // 函数返回内容  
}
function GetBetween($content,$start,$end){ //截取ID
	$r = explode($start, $content);
	if (isset($r[1])){
		$r = explode($end, $r[1]);
		return $r[0];
	}
	return '';
}