Home >Backend Development >PHP Tutorial >Code implementation of php getting token (WeChat)

Code implementation of php getting token (WeChat)

不言
不言Original
2018-08-20 15:31:275924browse

The content of this article is about the code implementation of PHP to obtain token (WeChat). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Interface call request description

https请求方式: GET
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET

Parameter description

Parameter Is it necessary illustrate
grant_type yes Get access_token and fill in client_credential
appid yes Unique Credentials for Third-Party Users
secret yes Third-party user unique credential key, appsecret

Return instructions

Under normal circumstances, WeChat will return the following JSON data packet to the public account:

{"access_token":"ACCESS_TOKEN","expires_in":7200}

Parameter Description

Parameter illustrate
access_token Obtained credentials
expires_in Voucher validity time, unit: seconds

The above is the WeChat public account’s access_token document. This chapter briefly explains how to obtain the token in PHP and what to pay attention to.

1. The prepared parameters require a public account The appid and secret are two pieces of information. At the same time, it should be noted that after the secret is changed, what you save also needs to be changed, so it is not recommended to change it, just save it.

2. You need to set up a whitelist, which can be obtained based on the server's IP address. If you really don't know it, it doesn't matter, because you can know your own IP based on the error reported by the WeChat interface and then set it up.

3. The number of access_token calls per day is valid. If I remember correctly, it is 2K times per day, but the validity period of a token is 2 hours, so we must cache a token for 2 hours so that the interface will not be exceeded. number of calls.

<?php
    public function getAccessToken($appid,$secret){
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
        $res = $this->curl_get($url);
        $res = json_decode($res,1);
        if($res[&#39;errcode&#39;]!=0) throw new Exception($res[&#39;errmsg&#39;]);
        return $res[&#39;access_token&#39;];
    }
 public function curl_get($url) {
         $headers = array(&#39;User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36&#39;);
        $oCurl = curl_init();
        if(stripos($url,"https://")!==FALSE){
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        curl_setopt($oCurl, CURLOPT_TIMEOUT, 20);
        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);
        curl_close($oCurl);
        if(intval($aStatus["http_code"])==200){
            return $sContent;
        }else{
            return false;
        }
    }

The above is the code for php to obtain the token, which is relatively easy.

Related recommendations:

WeChat Mini Program Obtain detailed explanation and example code of WeChat OpenId

php implementation code to obtain server information

The above is the detailed content of Code implementation of php getting token (WeChat). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn