点击查看代码
/**
* 图片内容安全校验
* @param $file_path
* @return array
*/
public static function imgSecCheck($file_path){
$result = WechatToken::getAppletAccessToken();
if ($result['code']) {
$access_token = $result['data'];
} else {
return ['code' => 0, 'msg' => $result['msg']];
}
$url = 'https://api.weixin.qq.com/wxa/img_sec_check?access_token=' . $access_token;
$file_data = array("media" => new \CURLFile($file_path['temp_name'],$file_path['type'],$file_path['name']));
$res = Helper::httpRequest($url, $file_data);
$result = json_decode($res, true); // 格式化为数组
if($result['errcode'] == 0){
return ['code' => 1, 'msg' => 'Ok'];
} else {
return ['code' => 0, 'msg' => '图片含有违规信息'];
}
}
* 图片内容安全校验
* @param $file_path
* @return array
*/
public static function imgSecCheck($file_path){
$result = WechatToken::getAppletAccessToken();
if ($result['code']) {
$access_token = $result['data'];
} else {
return ['code' => 0, 'msg' => $result['msg']];
}
$url = 'https://api.weixin.qq.com/wxa/img_sec_check?access_token=' . $access_token;
$file_data = array("media" => new \CURLFile($file_path['temp_name'],$file_path['type'],$file_path['name']));
$res = Helper::httpRequest($url, $file_data);
$result = json_decode($res, true); // 格式化为数组
if($result['errcode'] == 0){
return ['code' => 1, 'msg' => 'Ok'];
} else {
return ['code' => 0, 'msg' => '图片含有违规信息'];
}
}
参数示例
//前端表单提交图片
$file = UploadedFile::getInstanceByName('image');
$file_path = [
'temp_name' => $file->tempName,//文件的临时文件名
'type' => $file->type,
'name' => $file->name,
];
//校验服务器本地图片
$image = "0.jpg";//可以解析出绝对路径的真实图片
$file_path = [
'temp_name' => realpath($image), //服务器本地的绝对路径,
'type' => 'image/jpeg',
'name' => 'test_name',
];