谁一开始就是大神?

Yii2.0接受接口信息[2.0]

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&amp";
    }

    $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请求的参数。

赞(0) 打赏
未经允许不得转载:菜鸟之家 » Yii2.0接受接口信息[2.0]

评论 抢沙发

登录

找回密码

注册