img_sec_check 微信图片检测41005

如果图片在服务器本地

$filePath = './20200429/bd53134a6add5f1a18dd4cec85546e72.jpg';
$ret = filter_image( $filePath );

/**
 * 敏感词过滤
 */
function filter_image( $content ) {
    $AccessToken = getAccessToken();
    $data = [];
    $url = "https://api.weixin.qq.com/wxa/img_sec_check?access_token=" . $AccessToken;
    
    $real_path=realpath($content);
    $obj = new CURLFile($real_path);
    $obj->setMimeType("image/jpeg");
    $data['media']=$obj;

//    $data = [
//        "media" => $content,
//    ];
    $ret1 = CURLSend($url, "post", $data );
    $ret = json_decode($ret1,true);
    return $ret;
}


/**
 * 模拟浏览器发送
 */
function CURLSend($url, $method = 'get', $data = '') {
    $ch = curl_init(); //初始化
    $headers = array('Accept-Charset: utf-8');
    curl_setopt($ch, CURLOPT_URL, $url); //指定请求的URL
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method)); //提交方式
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //不验证SSL
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); //不验证SSL
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); //设置HTTP头字段的数组
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible;MSIE5.01;Windows NT 5.0)'); //头的字符串
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1); //自动设置header中的Referer:信息
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //提交数值
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //是否输出到屏幕上,true不直接输出
    $temp = curl_exec($ch); //执行并获取结果
    curl_close($ch);
    return $temp; //return 返回值
}

如果图片是网络图片或者存在第三方

        $img = file_get_contents( $content );  // img就是远程的图片地址 http的图片地址
        $filePath = './1.jpg';  //注意这里是填写你保存这张远程图片在你服务器上的绝对路径地址
        file_put_contents($filePath, $img);

 

你可能感兴趣的:(img_sec_check 微信图片检测41005)