SiteController代码:
public function actionIndex()
{
$data = json_decode(api_request(url), true);
return $this->render('index', [
'list' => $data
]);
}
common/functions.php里面的方法:
const API_HEAD = 'http://api.weidu51.com/v2000/';
function api_request($uri, $params, $method = 'GET'){
$apiUrl = API_HEAD . $uri;
if($method == 'get'){
$url = $apiUrl."page=10&psize=10&";
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
if($method == 'POST') {
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = json_decode(curl_exec($ch), true);
}
这个是要把这个接口的信息接收过来,转化成数组形式的,然后视图页面循环输出,但我看不懂,哪位大神指点一下什么意思?
就是用curl模拟get请求和post请求去接口请求数据,然后把拿到的json数据decode一下,curl_setopt的那一段是配置http请求的参数。