最近收到了騰訊方面的警告,通知裏寫明瞭線上小程序沒有進行內容安全檢測,然後又開始折騰。=_=~
使用到的環境:
PHP7.3
微信第三方開發SDK:EasyWechat 4.1
文本和圖片檢測沒什麼太大區別,只是調用的方法不同(注意圖片最好爲絕對路徑,暫時不支持網絡圖片,可以在圖片上傳的時候進行檢測)
//檢測文本
$result = $app->content_security->checkText($content);
//檢測圖片
$result = $app->content_security->checkImage('/path/to/the/image');
easywechat會自動先請求accesstoken,然後將所需要檢測的內容上傳到微信服務器。
// 正常返回 0
{
"errcode": "0",
"errmsg": "ok"
}
// 當文件內含有敏感內容,則返回 87014
{
"errcode": 87014,
"errmsg": "risky content"
}
下面是TP代碼,寫在控制器或者服務層都可以。
別忘記導入EasyWechat
use EasyWeChat\Factory;
$config = [
'app_id' => config('app_id'),
'mch_id' => config('mch_id'),
'key' => config('mch_key'),
'cert_path' => config('cert_path'),
'key_path' => config('key_path'),
'secret' => config('wx.app_secret')
];
$app = Factory::miniProgram($config);
$title_res = $app->content_security->checkText("這裏傳入要檢測的標題");
if ($title_res['errcode'] == 87014 && $title_res['errmsg'] != "OK"){
throw new InfoException(['msg'=>'標題涉嫌違法違規']);
}
$content_res = $app->content_security->checkText("這裏傳入要檢測的內容");
if ($content_res['errcode'] == 87014 && $content_res['errmsg'] != "OK"){
throw new InfoException(['msg'=>'內容涉嫌違法違規']);
}