PHP調(diào)用微信敏感詞檢測(msg_sec_check)、圖片檢測(img_sec_check)
php開發(fā)的小伙伴可能都會遇到,在使用敏感詞檢測接口(msg_sec_check)的時候,不管中文傳什么內(nèi)容返回的結(jié)果都是驗證通過的,原因是json_encode中文轉(zhuǎn)Unicode了。解決這個問題可以在json_encode加上參數(shù)JSON_UNESCAPED_UNICODE,禁止將中文轉(zhuǎn)Unicode,如:json_encode($data, JSON_UNESCAPED_UNICODE),這樣問題就解決了。
給大家伙詳細記錄代碼
如下:
第一步獲取token
/*獲取access_token*/
public function getAccessToken()
{
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$config['appid']}&secret={$config['appsecret']}";
$res = json_decode($this->http_request($url));
$access_token = $res->access_token;
return $access_token;
}
第二步請求設(shè)置
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);
return $output;
}
微信圖片敏感內(nèi)容檢測
$filePath = ROOT_PATH . ‘/public/dev/tmp1.png’;
一定要是絕對路徑
img可以是網(wǎng)址圖片
public function imgSecCheck($img)
{
$img = file_get_contents($img);
$filePath = ROOT_PATH . '/public/dev/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);
}
效果圖:文章來源:http://www.zghlxwxcb.cn/news/detail-648060.html
微信文字敏感內(nèi)容檢測
public function msgSecCheck($msg)
{
$data = json_encode(array('content' => $msg), JSON_UNESCAPED_UNICODE);
$token = $this->getAccessToken();
$url = "https://api.weixin.qq.com/wxa/msg_sec_check?access_token=$token";
$info = $this->http_request($url, $data);
return json_decode($info, true);
}
效果圖:
到此結(jié)束,感謝閱讀。
對您有幫助的話留下個關(guān)注,點贊收藏吧~文章來源地址http://www.zghlxwxcb.cn/news/detail-648060.html
到了這里,關(guān)于PHP調(diào)用微信敏感詞檢測(msg_sec_check)、圖片檢測(img_sec_check)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!