如何接入API:
protected function http_request($url,$params=false,$ispost=0){ try{ $httpInfo = array(); $ch = curl_init(); curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 ); curl_setopt( $ch, CURLOPT_USERAGENT , 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22' ); curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 30 ); curl_setopt( $ch, CURLOPT_TIMEOUT , 30); curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true ); if( $ispost ) { curl_setopt( $ch , CURLOPT_POST , true ); curl_setopt( $ch , CURLOPT_POSTFIELDS , $params ); curl_setopt( $ch , CURLOPT_URL , $url ); } else { if($params){ curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params ); }else{ curl_setopt( $ch , CURLOPT_URL , $url); } } $response = curl_exec( $ch ); $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE ); $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) ); curl_close( $ch ); return $response; }catch(Exception $e){ return false; } }
通过这个方法接入后,通过接入url地址获得自己想要的数据,
public function history(){ $url="http://apis.juhe.cn/lottery/history";//历史查询url地址 $params["key"]="84b252036374157d238c097fb8ed1ef0";//key $params["lottery_id"]="ssq";//id为双色球 $params["page_size"]=10;//默认条数是10条 $param=http_build_query($params); $result = $this->http_request($url,$param); $res = json_decode($result,true);//返回的是整个json数据 // dump($res); // dump($res['result']['lotteryResList']);die; $list = $res['result']['lotteryResList'];//根据dump出来的数据拆分['result']['lotteryResList']获得自己需要使用的二维数组 $this->assign("list",$list);//注册变量list $this->show();//显示在页面 }
上面是历史查询的url接口,根据请求参数说明,必填key lottery_id参数设置号,获得的list;
res数组是
res下的lotteryResList数组如下是获得的二维数组;拿到前端页面循环遍历list数组