Php WeChat development experts to see if there is a syntax error, can you automatically get the latest access_token

Source: Internet
Author: User
{Code ...}
Class Wechat_base_api {private $ appid = 'wx5d4b7d2e8bfd5793 '; private $ appsecret = 'created'; // constructor public function _ construct ($ appid, $ appsecret) {$ this-> appid = $ appid; $ this-> appsecret = $ appsecret;} // obtain access_token (Supports automatic credential update) public function get_access_token () {$ this-> last_time = 1408851194; $ access_token = "token"; if (time ()> ($ this-> last_time + 7200 )) {// GET request url $ url =" https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid= {$ This-> appid} & secret = {$ this-> appsecret} "; $ access_token_Arr = $ this-> https_request ($ url ); $ this-> last_time = time (); return $ access_token_Arr ['Access _ token'];} return $ access_token ;}
// Https request (GET and POST supported) protected function https_request ($ url, $ data = null) {$ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); if (! Empty ($ data) {curl_setopt ($ ch, CURLOPT_POST, 1); // simulate POST curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data ); // POST content} $ outopt = curl_exec ($ ch); curl_close ($ ch); $ outopt = json_decode ($ outopt, true); return $ outopt ;}}

Reply content:
Class Wechat_base_api {private $ appid = 'wx5d4b7d2e8bfd5793 '; private $ appsecret = 'created'; // constructor public function _ construct ($ appid, $ appsecret) {$ this-> appid = $ appid; $ this-> appsecret = $ appsecret;} // obtain access_token (Supports automatic credential update) public function get_access_token () {$ this-> last_time = 1408851194; $ access_token = "token"; if (time ()> ($ this-> last_time + 7200 )) {// GET request url $ url =" https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid= {$ This-> appid} & secret = {$ this-> appsecret} "; $ access_token_Arr = $ this-> https_request ($ url ); $ this-> last_time = time (); return $ access_token_Arr ['Access _ token'];} return $ access_token ;}
// Https request (GET and POST supported) protected function https_request ($ url, $ data = null) {$ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); if (! Empty ($ data) {curl_setopt ($ ch, CURLOPT_POST, 1); // simulate POST curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data ); // POST content} $ outopt = curl_exec ($ ch); curl_close ($ ch); $ outopt = json_decode ($ outopt, true); return $ outopt ;}}

1. No syntax error
2. Who's the id and secret...

Great Problems

  • If the expiration time is written to an end, the interface will not be requested, and the interface request limit may be reached.

  • The access_token is also written to death. When the Interface request limit is reached, the returned access_token is invalid.

  • In the end, access_token is not cached.

The following code is changed based on the provided sample code:


  AppId = $ appId? $ AppId: $ this-> appId; $ this-> appSecret = $ appSecret? $ AppSecret: $ this-> appSecret; $ this-> file_path = $ file_path? $ File_path: $ this-> file_path;}/*** get AccessToken * @ return mixed */private function getAccessToken () {$ data = json_decode (file_get_contents ($ this-> file_path); if ($ data-> expire_time <time ()) {// determine whether the locally stored access_token has expired // If the enterprise ID is used, use the following URL to obtain the access_token // $ url =" https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid= $ This-> appId & Cortana cret = $ this-> appSecret "; // use this URL to obtain access_token $ url =" https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid= $ This-> appId & secret = $ this-> appSecret "; // access interface, pull information such as access_token $ res = json_decode ($ this-> httpGet ($ url )); // extract access_token $ access_token = $ res-> access_token; // determine whether the access_token exists and saved to a local file if ($ access_token) {$ data-> expire_time = time () + 7190; $ data-> access_token = $ access_token; $ fp = fopen ($ this-> file_path, "w "); fwrite ($ fp, json_encode ($ data); fclose ($ fp) ;}} else {// The locally stored access_token has not expired $ access_token = $ data-> access_token ;} // return $ access_token;}/*** pull data * @ param $ url * @ return mixed | string */private function httpGet ($ url) {// local request, the request if ('192. 127. 0.0.1 '= $ _ SERVER ['server _ ADDR']) {return '';} $ curl = curl_init (); curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, true ); curl_setopt ($ curl, CURLOPT_TIMEOUT, 500); curl_setopt ($ curl, timeout, false); curl_setopt ($ curl, expires, false); curl_setopt ($ curl, CURLOPT_URL, $ url); $ res = curl_exec ($ curl); curl_close ($ curl); return $ res ;}}

The PHP code in the official documentation seems to be a lot of pitfalls. It takes too much time to try one of your own interfaces (don't ask me how I know it ...).

Because our project uses the CI framework, in GitHub this project: On the basis of the https://github.com/dodgepudding/wechat-php-sdk a little change, completed the integration.

Reading this code is faster than reading the official documentation. I think so...

Dude, your appid and appsecret are exposed !!!
In addition, access_token has a limit on the number of times.
Remember to cache it. Otherwise, an error will occur if the number of requests is too large.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.