TP5.1 调用 阿里云图片审核

TP5.1 调用阿里云图片同步审核接口

  • 首先去阿里云下载相关文件
  • 我这里是TP5.1引用的外部文件 tp5可以直接使用vendor进行引用
  • 下面我也会附上对接阿里云视频审核接口


namespace app\api\controller\approval;

use app\common\master\Dynamic;
use think\Controller;
use think\facade\Env;
use app\api\exception\ParamException;
//引用阿里云核心文件
require Env::get('root_path') . 'extend/aliyuncs/aliyun-oss-php-sdk/autoload.php';
require Env::get('root_path') . 'extend/aliyuncs/aliyun-php-sdk-core/Config.php';
use Green\Request\V20180509 as Green;
use Green\Request\Extension\ClientUploader;
/**
 * @date: 2018-12
 * 阿里云图片审核
 */
class ImgApproval extends Controller
{

    /*
     * 场景参数【porn:图片智能鉴黄,terrorism:图片暴恐涉政,ad:图文违规,qrcode:图片二维码,live:图片不良场景,logo:图片logo】
     */
    public static function img_approval($img, $id)
    {
        $img = explode(',', $img);
        $domain = request()->domain();
        $accessKeyId = config('sf_config.aliyuncs.accessKeyId');
        $accessSecret = config('sf_config.aliyuncs.accessSecret');
// 请替换成您的AccessKey信息。
        $iClientProfile = \DefaultProfile::getProfile("cn-shanghai",'阿里云的accessKeyId', '阿里云的accessSecret');
        \DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
        $client = new \DefaultAcsClient($iClientProfile);
        $request = new Green\ImageSyncScanRequest();
        $request->setMethod("POST");
        $request->setAcceptFormat("JSON");
        foreach ($img as $k => $v) {
            $task1[]=[
                'dataId' => uniqid(),
                'url' => $domain . $v
            ];
        }
        $request->setContent(json_encode(
            array(
                "tasks" => $task1,
                "scenes" => array("porn",'terrorism','ad','live','logo')
            )
        ));
        try {
            $response = $client->getAcsResponse($request);
            if (200 == $response->code) {
                $taskResults = $response->data;
                foreach ($taskResults as $k=>$taskResult) {
                    if (200 == $taskResult->code) {
                        $sceneResults = $taskResult->results;
                        foreach ($sceneResults as $sceneResult) {
                            $scene = $sceneResult->scene;
                            $suggestion = $sceneResult->suggestion;
                            // 根据scene和suggetion设置后续操作。
                            // 根据不同的suggestion结果做业务上的不同处理。例如,将违规数据删除等。
                           
                        }
                    } else {
                        echo $response->code;
                    }
                }
            } else {
              echo $response->code
            }
        } catch (\Exception $e) {
          echo $e->getMessage();
        }
    }

}

视频审核接口:这是我调用阿里云视频审核的接口

你可能感兴趣的:(TP5.1,阿里云,接口,php)