PHPの微信【内容安全】「文字・画像」チェックコード

5142 ワード

追加内容:
1、     https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/sec-check/security.imgSecCheck.html
2、PHP5.4,json_encode  ,         ,JSON_UNESCAPED_UNICODE,     ,      

第一部:【違反文字】チェックチェック
  :index

/**
 *   -  【  】  
 * @return mixed
 */
public function test_wx_check()
{
    $param = '        ';
    $type = 1;
    $result = $this->wx_check($param, $type);
    echo'
';var_dump($result);exit();
}

2 :【 】チェック
  :index

a、    
Filename:
b、
/**
* - 【 】
* @return mixed
*/
public function test_wx_check()
{
$param = $_FILE['uploadImg'];
$type = 1;
$result = $this->wx_check($param, $type);
echo'
';var_dump($result);exit();
}

:コアコード
  :index

/**
 *   -  【  /  】  
 * @param $param
 * @return mixed
 */
public function wx_check($param, $type=1)
{
    //        ,     false
    if(!$param || !$type){
        return false;
    }

    //       
    if($type==1){
        $data = ['content'=>$param];
    }else if($type==2){
        $data = ['media'=>$param];
    }

    //   access_token
    $this->load->service('order_service');
    $access_token = $this->order_service->get_access_token();

    // access_token    
    if(!$access_token){
        return false;
    }

    //     
    $this->load->config('dict/dict_wx_check');
    $config = $this->config->item('wx_check_config');
    $url = ($type==1?$config['content_url']:$config['img_url']).'?access_token='.$access_token;

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

    // json    
    return json_decode($result, true);
}

/**
 * cURL  POST     
 * @param string $url     
 * @param array $data     
 * @return mixed
 * @author LiuDongrang
 * @time   2019/05/19
 */
private function curl_post_weixin($url, $data)
{
    if($url && count($data)){
        $headers = ['Content-Type: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(); //     
    curl_setopt($ch, CURLOPT_URL, $url); //   url    
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //               
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//       
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //       SSL     
    $output = json_decode(curl_exec($ch));
    $access_token = $output->access_token;
    curl_close($ch);
    return $access_token;
}

  :dict/dict_wx_check

/**
 *   【  】     -     
 */

/**          **/
$config['wx_check_config'] = [
    'content_url'=>'https://api.weixin.qq.com/wxa/msg_sec_check',
    'img_url'=>'https://api.weixin.qq.com/wxa/img_sec_check',
];

にまとめます.
          (  、  )        ,  ,         !
          ,          media    form-data,  ,        enctype="multipart/form-data",  ,        “    ”   ,  !
          ,      ,   content  ,  json       ,json_encode    ,     "JSON_UNESCAPED_UNICODE"     ,  ,            !      ?  json_encode        ,       ,     "\u****"        ,  ,               ,            ! JSON_UNESCAPED_UNICODE    ,  json_encode    ,   ,  ,            !