收藏
评论

关于内容安全的php经验分享

最近项目里面有论坛的功能,所以在api中加了小程序自带的内容安全去辅助内容违规检测。

msgSecCheck这个接口比较简单,放下代码。


$url = 'https://api.weixin.qq.com/wxa/msg_sec_check?access_token='.$access_token;

$par = '{"content":'.'"'.$title.$content.'"'.'}';

$msg = http_post($url,$par);

$msg['errcode']就是错误代码了,按照文档上的继续写自己的逻辑就好了。


imgSecCheck这个接口我也看了好久,试了很多次,最后我的解决方案是让用户先上传图片,存放到服务器,然后在用服务器中的图片文件post到这个接口中,去判断是否违规,放下代码。


//用户上传图片

public function uploadimg(){

$config=array(

'maxSize'=>3145728,

'exts'=>array('jpg', 'gif', 'png', 'jpeg'),

'rootPath'=>'./Uploads/luntan_img/',

'autoSub'=>false,

'savePath'=>date('Y/m')."/",

);

$upload = new \Think\Upload($config);

// 上传单个文件

$upload->saveName = time().uniqid();

$info   =   $upload->uploadOne($_FILES['file']);

$msg = $this->imgSecCheck($info);//图片违规检测

if(!$info) {// 上传错误提示错误信息

$data['info'] = $upload->getError();

$this->ajaxReturn($data);

}else{// 上传成功 获取上传文件信息

if($msg['errcode']>0){

$path = 'risky';

}else{

$path = $info['savepath'].$info['savename'];

}

$this->ajaxReturn($path);

}

}


// 图片检测方法

public function imgSecCheck($img){

$url = 'https://api.weixin.qq.com/wxa/img_sec_check?access_token='.$access_token;

$filePath = $_SERVER['DOCUMENT_ROOT'].'Uploads/luntan_img/'.$img['savepath'].$img['savename'];

$image = new \Think\Image();

$image->open($filePath);

$imgSecCheckfilePath = $_SERVER['DOCUMENT_ROOT'].'Uploads/luntan_img/thumb/thumb_'.$img['savename'];

$image->thumb(150, 150)->save($imgSecCheckfilePath);

$obj = new \CURLFile($imgSecCheckfilePath);

$obj->setMimeType("image/jpeg");

$file['media'] = $obj;

$info = $this -> sendCmd($url,$file);

$info =json_decode($info,true);

$obj1 = new \CURLFile($filePath);

$obj1->setMimeType("image/jpeg");

$file1['media'] = $obj1;

$info1 = $this -> sendCmd($url,$file1);

$info1 =json_decode($info1,true);

if($info['errcode']>0 ||$info1['errcode']>0){

rename($filePath, $_SERVER['DOCUMENT_ROOT'].'Uploads/luntan_img/risky/risky'.$img['savename']);

if($info['errcode']>0){

return $info;

}

if($info1['errcode']>0){

return $info1;

}

}

return $info;

}


// 发送psot请求

function sendCmd($url,$data)

{

$curl = curl_init(); // 启动一个CURL会话

curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检测

curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在

curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:')); //解决数据包大不能提交

curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转

curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer

curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求

curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包

curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循

curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回

$tmpInfo = curl_exec($curl); // 执行操作

if (curl_errno($curl)) {

echo 'Errno'.curl_error($curl);

}

curl_close($curl); // 关键CURL会话

return $tmpInfo; // 返回数据

}


因为本人刚接触php不久,对于一些语法还在摸索中,如果检测图片中的post请求使用http_post()方法,就会取不到返回值,我也不知道是不是参数或者其他问题,但是使用网上找的这个单独post方法就可以了,我在检测图片的时候检测了2次,因为图片宽高的原因,我会新生成一张150的缩略图,然后两个一起比对,一张存在问题,就按照违规图片进行处理,希望能够帮到大家。如果哪里写的不好,还请提出,以后改进。


最后一次编辑于  2018-11-01
收藏
登录 后发表内容