//图片审查$res3 = imgSecCheck("https://*/2122615643.jpg");var_dump($res3);$str = array('content'=>"检查内容");//文本审查$res_str = msgSecCheck($str);var_dump($res_str);/*微信图片敏感内容检测*/function imgSecCheck($img){ //图片地址存本地 $img = file_get_contents($img); $filePath = 'tmp1.png'; file_put_contents($filePath, $img); resize_image($filePath, $filePath);//把尺寸缩放到规定大小(暂时把图片放在后端,小程序在审核中没办法改) //拼接文件发送格式 $minetype = 'image/jpeg'; $curl_file = curl_file_create($filePath,$minetype); $file['media'] = $curl_file; $token = getAccesstoken(); $url = "https://API.weixin.qq.com/wxa/img_sec_check?access_token=$token"; //发送数据 $info = HTTP_Request($url,$file); return Json_decode($info,true);}/*微信文字敏感内容检测*/function msgSecCheck($msg){ $token = getAccesstoken(); $url = "https://API.weixin.qq.com/wxa/msg_sec_check?access_token=$token"; $info = HTTP_Request($url,Json_encode($msg)); return Json_decode($info,true);}/*获取access_token,不能用于获取用户信息的token*/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;}//创建一个上传的图片文件function curl_file_create($filename, $mimetype = '', $postname = '') { return "@$filename;filename=" . ($postname ?: basename($filename)) . ($mimetype ? ";type=$mimetype" : '');}//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); file_put_contents('/tmp/heka_weixin.' . date("Ymd") . '.log', date('Y-m-d H:i:s') . "\t" . $output . "\n", file_APPEND); return $output;}// 重置图片文件大小function resize_image($filename, $tmpname, $xmax=750, $ymax=1334){ $ext = explode(".", $filename); $ext = $ext[count($ext)-1]; if($ext == "jpg" || $ext == "jpeg") $im = imagecreatefromjpeg($tmpname); elseif($ext == "png") $im = imagecreatefrompng($tmpname); elseif($ext == "gif") $im = imagecreatefromgif($tmpname); $x = imagesx($im); $y = imagesy($im); if($x <= $xmax && $y <= $ymax) return $im; if($x >= $y) { $newx = $xmax; $newy = $newx * $y / $x; } else { $newy = $ymax; $newx = $x / $y * $newy; } $im2 = imagecreatetruecolor($newx, $newy); imagecopyresized($im2, $im, 0, 0, 0, 0, floor($newx), floor($newy), $x, $y); return $im2;}
本来curl提交图片文件可以用 CURLfile 类,但是我们PHP环境太低,只有PHP5.3,所以上传图片文件,用的 自定义函数 curl_file_create().
参考:
https://www.cnblogs.com/xinxinmifan/p/9722876.HTML
https://www.cnblogs.com/xp796/p/8042040.HTML
https://developers.weixin.qq.com/community/operate/doc/00002a6a0b8d98a965993666a51001
总结以上是内存溢出为你收集整理的PHP 微信小程序敏感图片、内容检测接口全部内容,希望文章能够帮你解决PHP 微信小程序敏感图片、内容检测接口所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)