技术饭
微信小程序敏感图片(imgSecCheck)、内容检测(msgSecCheck)接口实现
先附上小程序接口说明文档地址:https://developers.weixin.qq.com/miniprogram/dev/api/open-api/sec-check/msgSecCheck.html
1、首先要获取access_token(需要appId、appSecret、grant_type这个是固定值); ###msgSecCheck接口:
2、用获取到的token带入到微信端的接口地址: https://api.weixin.qq.com/wxa/msg_sec_check?access_token=ACCESS_TOKEN。 这里需要注意的是:
1、message信息的格式要是JSON格式,不能直接传string,不然会报 47001,data format error hin 错误。 ###imgSecCheck接口:
3、用获取到的token带入到微信端的接口地址:https://api.weixin.qq.com/wxa/img_sec_check?access_token=ACCESS_TOKEN。 这里需要注意的是:
1、image的格式要是formdata 格式,不能直接传url,不然会报 41005,media data missing hin 错误。参数名应该使用:media,这是小程序定好的。 我这里是获取到微信上传的图片的url,然后把它下载到一个存放临时文件的区/dev/shm,然后再转为curlFile()对象 。 附上具体代码: /*微信图片敏感内容检测*/ public function imgSecCheck($img) { $img = file_get_contents($img); $filePath = '/dev/shm/tmp1.png'; file_put_contents($filePath, $img); $obj = new CURLFile(realpath($filePath)); $obj->setMimeType("image/jpeg"); $file['media'] = $obj; $token = $this->getAccessToken(); $url = "https://api.weixin.qq.com/wxa/img_sec_check?access_token=$token"; $info = $this->http_request($url,$file); return json_decode($info,true); } /*微信文字敏感内容检测*/ public function msgSecCheck($msg) { $token = $this->getAccessToken(); $url = "https://api.weixin.qq.com/wxa/msg_sec_check?access_token=$token"; $info = $this->http_request($url,json_encode($msg)); return json_decode($info,true); } /*获取access_token,不能用于获取用户信息的token*/ public function getAccessToken() { $token_file = '/dev/shm/heka_token.json'; $data = json_decode(file_get_contents($token_file)); if ($data->expire_time < time()) { $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret"; $res = json_decode($this->http_request($url)); $access_token = $res->access_token; if ($access_token) { $data->expire_time = time() + 7000; $data->access_token = $access_token; file_put_contents($token_file, json_encode($data)); } } else { $access_token = $data->access_token; } return $access_token; } //HTTP请求(支持HTTP/HTTPS,支持GET/POST) private function http_request($url, $data = null) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); if (!empty($data)) { curl_setopt($curl, CURLOPT_POST, TRUE); curl_setopt($curl, CURLOPT_POSTFIELDS,$data); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); $output = curl_exec($curl); curl_close($curl); file_put_contents('/tmp/heka_weixin.' . date("Ymd") . '.log', date('Y-m-d H:i:s') . "\t" . $output . "\n", FILE_APPEND); return $output; }
转载自: https://www.cnblogs.com/xinxinmifan/p/9722876.html
小程序,内容安全API,msgSecCheck不管传什么文字,都返回ok
/**
* [msgSecCheck 小程序消息检测验证]
*/
function msgSecCheck($key = '', $content = '') {
//验证参数
if($key == '' || $content == ''){
return ['code' => 1, 'msg' => '不验证'];
}
//获取配置
$appid = config('program.appid');
$appsecret = config('program.appsecret');
//获取access_token
$access_token = get_accessToken($appid, $appsecret);
//检测地址
$url = 'https://api.weixin.qq.com/wxa/msg_sec_check?access_token=' . $access_token;
//curl post
$res = http_request($url, json_encode(['content' => $content], JSON_UNESCAPED_UNICODE));
$res = json_decode($res, true);
if($res['errcode'] == 0){
//正常
return ['code' => 1, 'msg' => $res['errmsg']];
} else {
//违规
return ['code' => 0, 'msg' => $res['errmsg']];
}
}
/**
* [imgSecCheck 微信图片敏感内容检测]
*/
function imgSecCheck($key = '', $filePath = ''){
//验证参数
if($key == '' || $filePath == ''){
return ['code' => 1, 'msg' => '不验证'];
}
//验证文件是否存在
$realpath = realpath($filePath);
if(!file_exists($realpath)){
return ['code' => 0, 'msg' => '文件不存在'];
}
//curl文件
$obj = new \CURLFile($realpath);
$obj->setMimeType("image/jpeg");
$file['media'] = $obj;
//获取配置
$appid = config('program.appid');
$appsecret = config('program.appsecret');
//获取access_token
$access_token = get_accessToken($appid, $appsecret);
//检测地址
$url = 'https://api.weixin.qq.com/wxa/img_sec_check?access_token=' . $access_token;
//curl post
$res = http_request($url, $file);
$res = json_decode($res, true);
if($res['errcode'] == 0){
//正常
return ['code' => 1, 'msg' => $res['errmsg']];
} else {
//违规
return ['code' => 0, 'msg' => $res['errmsg']];
}
}
//HTTP请求(支持HTTP/HTTPS,支持GET/POST)
function http_request($url, $data = null)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)) {
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
/**
* @desc 获取微信asses_token
* @param $appid string 微信公众账号APPID
* @param $appsecret string 微信公众账号APPSECRET
* @return $arr['access_token'] string 返回asses_token
**/
function get_accessToken($appid, $appsecret)
{
//if($_SESSION['access_token'] && $_SESSION['expire_time']>time()){
//return $_SESSION['access_token'];
//}else{
//1.请求url地址
// $appid = APPID; //appid
// $appsecret = APPSECRET; //appsecret
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;//请求地址
//2初始化curl请求
$ch = curl_init();
//3.配置请求参数
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
curl_setopt($ch, CURLOPT_URL, $url); //请求
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //不直接输出数据
//4.开始请求
$res = curl_exec($ch); //获取请求结果
if( curl_errno($ch) ){
var_dump( curl_error($ch) ); //打印错误信息
}
//5.关闭curl
curl_close( $ch );
$arr = json_decode($res, true); //将结果转为数组
//$_SESSION['access_token']=$arr['access_token']; //将access_token存入session中,可以不存,每次都获得新的token
//$_SESSION['expire_time']=time()+7200;
return $arr['access_token'];
}
文明上网理性发言!