微信获取token

  /**
     * 获取微信access_token
     *
     * @param [type] $data
     * @return void
     */
    public function getAccessTokenWX($data)
    {
        $appid=trim($data['appid']);
        $appsec=trim($data['appsec']);
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsec";
        $result_info = $this->httpGet($url);
        $result = json_decode($result_info, true);
        return $result;
    }
    protected function httpGet($url,$timeout=30,$header=array())
    {
        $ch=curl_init();
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        $result=curl_exec($ch);
        return $result;
    }
    protected function httpPost($url,$data_string)
    {
        $header = [
            'Content-Type: application/json',
            'Content-Length: ' . strlen($data_string)
        ];
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT,5);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        $data = curl_exec($ch);
        if (curl_errno($ch)) {
            return curl_error($ch);
        }
        curl_close($ch);
        return $data;
    }
   

  
 /**
   * 接口次数清零
  */
    public function clearInterface()
    {
         $access_token='31_L8dZNFn6cppya0tzB4CX20j75Mjfn7kT0RCfG1Z9RtTxml-sKYrxFeTq3357gUszFQa-eSpzXa3rcEu_CE4ttKTaGTim2KB-vTT8US2g8P-dZ8iL1ScdR1MPLY1Z_ebebtmoB7MH-haWg3T8NRZjAFAXBJ';
         $url="https://api.weixin.qq.com/cgi-bin/clear_quota?access_token=".$access_token;
         $data='
                {
                    "appid":"wx2859f31715f7bb28"
                }        
         ';
         $res=$this->httpPost($url,$data);
         var_dump($res);
    }
  /**
     * 获取微信access_token
     *
     * @param [type] $data
     * @return void
     */
    public function getAccessTokenWX($data)
    {
        $appid=trim($data['appid']);
        $appsec=trim($data['appsec']);
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsec";
        $result_info = $this->httpGet($url);
        $result = json_decode($result_info, true);
        return $result;
    }
    protected function httpGet($url,$timeout=30,$header=array())
    {
        $ch=curl_init();
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        $result=curl_exec($ch);
        return $result;
    }
    protected function httpPost($url,$data_string)
    {
        $header = [
            'Content-Type: application/json',
            'Content-Length: ' . strlen($data_string)
        ];
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT,5);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        $data = curl_exec($ch);
        if (curl_errno($ch)) {
            return curl_error($ch);
        }
        curl_close($ch);
        return $data;
    }
   

  
 /**
   * 接口次数清零
  */
    public function clearInterface()
    {
         $access_token='31_L8dZNFn6cppya0tzB4CX20j75Mjfn7kT0RCfG1Z9RtTxml-sKYrxFeTq3357gUszFQa-eSpzXa3rcEu_CE4ttKTaGTim2KB-vTT8US2g8P-dZ8iL1ScdR1MPLY1Z_ebebtmoB7MH-haWg3T8NRZjAFAXBJ';
         $url="https://api.weixin.qq.com/cgi-bin/clear_quota?access_token=".$access_token;
         $data='
                {
                    "appid":"wx2859f31715f7bb28"
                }        
         ';
         $res=$this->httpPost($url,$data);
         var_dump($res);
    }

 

posted @ 2020-03-15 21:53  PHP小媛  阅读(854)  评论(0编辑  收藏  举报