基于Imagga服务的PHP/cURL图像识别集成

基于Imagga服务的PHP/cURL图像识别集成,php,json,curl,Php,Json,Curl,我正在尝试使用cURL和PHP从Imagga图像识别(人工智能服务,新的V2版本)获取图像标签 我设法获得了有效的响应,cURL body响应如下所示: { "result":{ "tags":[ {"confidence":100,"tag":{"en":"pink"}}, {"confidence":92.6405181884766,"tag":{"en":"petal"}}, {"confidence":69.867607116699

我正在尝试使用cURL和PHP从Imagga图像识别(人工智能服务,新的V2版本)获取图像标签

我设法获得了有效的响应,cURL body响应如下所示:

{
"result":{
    "tags":[
        {"confidence":100,"tag":{"en":"pink"}},
        {"confidence":92.6405181884766,"tag":{"en":"petal"}},
        {"confidence":69.8676071166992,"tag":{"en":"flower"}},
        {"confidence":54.1640663146973,"tag":{"en":"bloom"}}
        ]
        }
,"status":{"text":"","type":"success"}
}
我试着换标签,但我遇到了麻烦

 $response = curl_exec($curl);

 $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
 $body = substr($response, $header_size);
 $header = substr($response, 0, $header_size);
 $rows = explode("\n", $header);

 $err = curl_error($curl);

 curl_close($curl);
 $resp = json_decode( $body, true );

 if ($err) {echo $err; } else {
     // foreach thought tags, and if tag confidence is above 60, than echo it, do something with it...
 }
如果标签可信度高于60,如何回显某个标签?

只需使用foreach即可

<?php
$body = '{
"result":{
    "tags":[
        {"confidence":100,"tag":{"en":"pink"}},
        {"confidence":92.6405181884766,"tag":{"en":"petal"}},
        {"confidence":69.8676071166992,"tag":{"en":"flower"}},
        {"confidence":54.1640663146973,"tag":{"en":"bloom"}}
        ]
        }
,"status":{"text":"","type":"success"}
}';


$resp = json_decode( $body, true );

foreach ($resp['result']['tags'] ?? $tags as $tag) {
    if (
        ($confidence = $tag['confidence'] ?? null) 
        && $confidence >= 60 
        && ($tagName = $tag['tag']['en'] ?? null)
    ) {
        echo $tagName . "\r\n";
    }
}

可能重复的var_dump($tag)显示如下数组:数组(2){[“confidence”]=>float(100)[“tag”]=>array(1){[“en”]=>string(5)“pink”}如何只回显标记名“pink”?如果置信度高于60,请将您的答案更新为echo just tag name?完成。但是你应该看一下,谢谢你的回答,它工作得很好,也谢谢你提供更多的信息^_^