2015-10-10 15:24:47 5446次浏览 1条回答 3 悬赏 40 金钱

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);
    
}

这个是要把这个接口的信息接收过来,转化成数组形式的,然后视图页面循环输出,但我看不懂,哪位大神指点一下什么意思?

最佳答案

  • b31o8321 发布于 2015-10-12 16:40 举报

    就是用curl模拟get请求和post请求去接口请求数据,然后把拿到的json数据decode一下,curl_setopt的那一段是配置http请求的参数。

    4 条回复
    回复于 2015-10-13 11:48 回复

    我想获取到他的返回值,判断一下接口是否正常,要怎么写啊?这个好像也没有写完的

    回复于 2015-10-15 15:01 回复

    $result = json_decode(curl_exec($ch), true); 这一句就是发送请求并且把请求回的东西解码

    回复于 2015-10-15 17:19 回复

    此问题已经解决 谢谢

    回复于 2017-01-24 16:19 回复

    你可以把你在yii2.0写好的接口发给我一份可以不?我 现在不会写接口

    没有找到数据。
您需要登录后才可以回答。登录 | 立即注册
辰枫夜雨
助理

辰枫夜雨

注册时间:2015-08-11
最后登录:2017-06-23
在线时长:18小时59分
  • 粉丝10
  • 金钱0
  • 威望0
  • 积分180

热门问题