通过后台PHP程序来获取第三方服务器接口数据
GET方式
public function curl_get($url){ $httph = curl_init($url); //不验证SSL curl_setopt($httph,CURLOPT_SSL_VERIFYPEER,0); curl_setopt($httph,CURLOPT_SSL_VERIFYHOST,0); //设置获取的信息以文件流的形式返回,而不是直接输出 curl_setopt($httph,CURLOPT_RETURNTRANSFER,1); curl_setopt($httph,CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"); //可以拿到请求头信息 curl_setopt($httph,CURLOPT_HEADER,0); $res = curl_exec($httph); curl_close($httph); return $res; }
调用上面的方法获取数据
public function index() { $url = "http://********/city/101030100"; $json_str = $this->curl_get($url); //将获取的JSON字符串转换成PHP数组格式 $data = json_decode($json_str,true); //dump($data); }