如何从 codeigniter 中的 CURL 响应中提取所需的数据?
How to extract required data from CURL response inside codeigniter?
我尝试使用 https://rapidapi.com/lambda/api/face-recognition-and-face-detection/details 中的 API,得到如下响应
{
"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
}
这是我的控制器:
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;
}
}
我已经尝试过这个:
<?= $getViewAlbum->visualization[0][0]; ?>
我想要的是:
我想在页面视图中显示数据可视化->图像作为图像源、名称和模型->大小
您的回复是JSON,您只需要使用json_decode()
function喜欢
$result = json_decode($response);
$result->visualization[0]->image;
或获取数组结果而不是对象
$result = json_decode($response, true);
$result['visualization'][0]['image'];
我尝试使用 https://rapidapi.com/lambda/api/face-recognition-and-face-detection/details 中的 API,得到如下响应
{
"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
}
这是我的控制器:
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;
}
}
我已经尝试过这个:
<?= $getViewAlbum->visualization[0][0]; ?>
我想要的是: 我想在页面视图中显示数据可视化->图像作为图像源、名称和模型->大小
您的回复是JSON,您只需要使用json_decode()
function喜欢
$result = json_decode($response);
$result->visualization[0]->image;
或获取数组结果而不是对象
$result = json_decode($response, true);
$result['visualization'][0]['image'];