评论

php使用img_sec_check内容安全接口实例

被一个参数问题搞得头疼一天,希望对大家有点帮助

/*
* 调用微信接口验证图片是否合法
* $data 数组
/
public function Is_img()
{
$img_url = “通过网址访问的图片地址”;
$tokenArr = json_decode($this->send_post_token(“GET”));
$access_token = $tokenArr->access_token;
$info = $this->imgSecCheck($access_token,$img_url);
if($info[‘errcode’]){
return false;
}else{
return true;
}
}
/

* 检测图片
* $img 是可以通过网址访问的图片地址
* $access_token
/
public function imgSecCheck($access_token,$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;
$url = “https://api.weixin.qq.com/wxa/img_sec_check?acces s_token=$access_token”;
$info = $this->http_request($url,$file);
return json_decode($info,true);
}
/

* 获取access_token
*/
function send_post_token($method=‘POST’)
{
//获取ACCESS_TOKEN
$this->appid = ‘小程序appid’;
$this->secret = ‘小程序的secret’;
$url = “https://api.weixin.qq.com/cgi-bin/token?grant_type= client_credential&appid=”.$this->appid."&secret=".$t his->secret;
$post_data = array();
$postdata = http_build_query($post_data);
$options = array(
‘http’ => array(
‘method’ => $method, //or GET
‘header’ => ‘Content-type:application/x-www-form-urle ncoded’,
‘content’ => $postdata,
‘timeout’ => 15 * 60 // 超时时间(单位:s)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
return $result;
}

最后一次编辑于  10-12  
点赞 1
收藏
评论