首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从codeigniter内部的CURL响应中提取所需的数据?

如何从codeigniter内部的CURL响应中提取所需的数据?
EN

Stack Overflow用户
提问于 2021-02-10 01:39:29
回答 1查看 95关注 0票数 0

我尝试使用https://rapidapi.com/lambda/api/face-recognition-and-face-detection/details的接口,得到的响应如下

代码语言:javascript
复制
    {
"visualization": [
{
"image": "http://api.lambdal.com/nsuploads/6ed566063d/rawImages/09Feb21_153425_102613.jpg",
"entryid": "Face931873b7a3"
},

{
"image": "http://api.lambdal.com/nsuploads/6ed566063d/rawImages/09Feb21_152054_338756.jpg",
"entryid": "Face2e3921dee2"
}
],
"string": "<Album(6ed566063d) len: 4>",
    "name": "6ed566063d",
    "entries": [
    "Face931873b7a3",
    "Face2e3921dee2"
    ],
    "model": "<Model len: 4>",
        "size": 4
        }

这是我的控制器:

代码语言:javascript
复制
public function viewAlbum($idx=null)
    {
        $id = $idx;
        if($idx == null || $idx == ''){
            redirect('Photo');
        } else {
            $dataAlbum = $this->admin->data_album($id);
            if($dataAlbum->num_rows() != 0){
                    $namaAlbum = $dataAlbum->row()->nama_album;
                    $albumKey = $dataAlbum->row()->kode_album;
                    $data['title'] = "Dashboard | FaceVoting Versi 1.0";
                    $data['getViewAlbum']= json_decode($this->cekAlbum($namaAlbum,$albumKey),true);
                    $view ='v_detailalbum';
                    $this->_template($data,$view);
            }else{
                redirect('Album');
            }
        }
    }

    private function cekAlbum($namaAlbum,$albumKey)
    {
        $curl = curl_init();

        curl_setopt_array($curl, [
            CURLOPT_URL => "https://lambda-face-recognition.p.rapidapi.com/album?album=".$namaAlbum."&albumkey=".$albumKey,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "GET",
            CURLOPT_HTTPHEADER => [
                "x-rapidapi-host: lambda-face-recognition.p.rapidapi.com",
                "x-rapidapi-key: 932571abf0msh45cf0f3cef74aacp19e151jsn33e9949a1974"
            ],
        ]);

        $response = curl_exec($curl);
        $err = curl_error($curl);

        curl_close($curl);

        if ($err) {
//          echo "cURL Error #:" . $err;
            return null;
        } else {
            return $response;
        }
    }

我已经在我的视图上尝试过了:

代码语言:javascript
复制
<?= $getViewAlbum->visualization[0][0]; ?>

我想要的是:我想在页面视图中显示数据可视化->图像作为图像源、名称和模型->大小

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-10 18:50:22

你的响应是JSON,你只需要像这样使用json_decode()

代码语言:javascript
复制
$result = json_decode($response);
$result->visualization[0]->image;

或者获取数组结果而不是对象

代码语言:javascript
复制
$result = json_decode($response, true);
$result['visualization'][0]['image'];
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66124131

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档