{ "resultcode": "200", "reason": "Return Successd!", "result": { "data": [{ "name": "北京市法雨合", "intro": "法雨合0层", "address": "北京市朝阳区朝阳区三里屯", "google_lat": "39.9372423", "google_lon": "116.4480615", "baidu_lat": "39.942952987502", "baidu_lon": "116.45464108129", "province": "北京市", "city": "北京市" } ], "pageinfo": { "pnums": 20, "current": 1 } } }
<?php /** * Created by PhpStorm. * User: FZS * Time: 2019/3/15 17:50 */ //---------------------------------- // 聚合 全国WIFI 调用类 //---------------------------------- class freeApi{ private $apiKey = false; //聚合appkey private $apiUrl = 'http://apis.juhe.cn/wifi/region'; public function __construct($apikey){ $this->apiKey = $apikey; } /** * 将JSON内容转为数据,并返回 * @param string $content [内容] * @return array */ public function returnArray($content){ return json_decode($content,true); } /** * 获取 全国WIFI 结果 * @return array */ public function getResult(){ $params = [ "city" => "",//城市名urlencode utf8; "page" => "",//页数,默认1 "key" => $this->apiKey,//应用APPKEY(应用详细页查询) "dtype" => "",//返回数据的格式,xml或json,默认json ]; $params = http_build_query($params); return $this->returnArray($this->freeApiCurl($this->apiUrl,$params,1)); } /** * 请求接口返回内容 * @param string $url [请求的URL地址] * @param string $params [请求的参数] * @param int $ipost [是否采用POST形式] * @return string */ public function freeApiCurl($url,$params=false,$ispost=0){ $ch = curl_init(); curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 ); curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 ); curl_setopt( $ch, CURLOPT_USERAGENT , 'free-api' ); curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 ); curl_setopt( $ch, CURLOPT_TIMEOUT , 60); 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 ); if ($response === FALSE) { return false; } curl_close( $ch ); return $response; } }