PHP content of the micro-channel [Security] "text, image" check code

Additional content:

1, micro-channel document https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/sec-check/security.imgSecCheck.html 
2, PHP5.4, json_encode function, a new parameter options, JSON_UNESCAPED_UNICODE, Chinese does not encode the content is output

The first part: [text] illegal checksum

File: index
 
/ **
 * micro channel - [text] violations detected
 * Mixed @return
 * /
public test_wx_check function ()
{
    $ param = 'Shending fly occurs cinnabar';
    $ type =. 1;
    $ Result = $ this-> wx_check ($ param, type $);
    echo '<pre>'; var_dump ($ Result); Exit ();
}

Part II: [Image] check compliance check

文件:index

a、表单部分
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
    <label for="file">Filename:</label>
    <input type="file" name="uploadImg" id="uploadImg" />
    <br />
    <input type="submit" name="submit" value="Submit" />
</form>

b、后端部分

/**
 * 微信-违规【图片】检测
 * @return mixed
 */
public function test_wx_check()
{
    $param = $_FILE['uploadImg'];
    $type = 1;
    $result = $this->wx_check($param, $type);
    echo'<pre>';var_dump($result);exit();
}

Part III: the core code section

File: index
 
/ **
 * micro channel - [violation text / image detection]
 * @param $ param
 * Mixed @return
 * /
public function wx_check ($ param, $ =. 1 type)
{
    // validate the parameters, direct return empty to false
    IF ($ param || type $!!) {
        return to false;
    }

    // parameter sorting process
    IF (type == $. 1) {
        $ Data = [ 'Content' => $ param];
    } the else IF ($ type 2 ==) {
        $ Data = [ 'Media' => $ param];
    }

    // Get the access_token
    $ this-> load->-Service ( 'order_service');
    $ the access_token = $ this-> order_service-> get_access_token () ;

    // the access_token not be empty
    IF (! $ the access_token) {
        return false;
    }

    // request address 
    $ this-> load-> config ( 'dict / dict_wx_check'); 
    $ config = $ this-> config-> Item ( 'wx_check_config'); 
    ? URL = $ ($ type == $ config. 1 [ 'CONTENT_URL']:. '? the access_token ='. $ config [ 'img_url']) $ the access_token; 

    // data request 
    $ result = $ this-> curl_post_weixin (URL $, $ data); 

    // inversion results JSON 
    return of json_decode ($ Result, to true); 
} 

/ ** 
 * the cURL POST data to the micro-channel mode 
 * @param string $ url request address 
 * @param array $ data transmission data 
 * @return Mixed 
 * @author LiuDongrang 
 * @time 2019/05 / . 19 
 * / 
Private function curl_post_weixin (URL $, $ Data) 
{ 
    IF (URL && $ COUNT ($ Data)) { 
        $ headers = [ 'the Type-the Content:application/json'];
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // 关键点
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data, JSON_UNESCAPED_UNICODE)); // 【* 关键点】
        $res = curl_exec($ch);
        curl_close($ch);
        return $res;
    }
}

文件:order_service

/**
 * 获取token
 * @author LiuDongrang
 * @time  2019/07/05
 */
public function take_access_token()
{
    $access_token = $this->get_access_token();
    // 获取存入redis
    $this->load->library('dbredis');
    $this->dbredis->set('redis_access_token', $access_token);
    return $this->dbredis->get('redis_access_token');
}

/**
 * 获取access_token值
 * @return string $access_token
 * @author LiuDongrang
 * @time   2019/05/19
 */
public function get_access_token()
{
    $appConfig = [
        'app_id' => 'wxsdfdsfsdfsdfdfdefc',
        'secret' => 'ecsdfsdfdfuyttrrlo9887jfa7d29106'
    ];
    $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appConfig['app_id'].'&secret='.$appConfig['secret'];
    $ ch = curl_init (); // create the handle
    curl_setopt ($ ch, CURLOPT_URL, $ url); // get the data URL 
    curl_setopt ($ CH, CURLOPT_RETURNTRANSFER,. 1); // get information returned as a stream file 
    curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, false ); // skip certificate validation 
    curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, false ); // check the SSL encryption certificate is 
    $ = Output of json_decode (the curl_exec (CH $)); 
    $ $ output- the access_token => the access_token; 
    curl_close (CH $) ; 
    return $ the access_token; 
} file: dict / dict_wx_check 
/ ** 
 * [micro channel graphic checksum violation] - configuration file 
 * / 
    'CONTENT_URL' => 'https://api.weixin.qq.com/wxa/msg_sec_check' , 
    'img_url' => 'https://api.weixin.qq.com/wxa/img_sec_check', 
];



 
/ ** graphic verification request address * / 
$ config [ 'wx_check_config'] = [

Concluded:

    Graphic content verification (Jurisprudence, involving government) does not have any difficulty, however, there is need to pay attention! 
    Picture this inspection, micro-channel requirements of picture parameters end media format is form-data, therefore, form properties must have enctype = "multipart / form-data ", otherwise, the test will be reported when "malformed" prompt, Remember! 
    Check this text, there is a key point, that is, content content, converted to json format for transmission time, json_encode this function, it is necessary to add "JSON_UNESCAPED_UNICODE" this parameter, otherwise, those sensitive words simply do not measure up! Why is this? Because json_encode in dealing with Chinese, the Chinese will be encoded into a similar "\ u ****" unreadable format , so that micro-channel receiving end are a bunch of such characters, of course, check out sensitive words up! And JSON_UNESCAPED_UNICODE this parameter, the added json_encode function, all the Chinese characters, is output , so that you can filter out sensitive content coming!
Published 59 original articles · won praise 2 · Views 5582

Guess you like

Origin blog.csdn.net/LDR1109/article/details/101221187