PHP Get token implementation code (WeChat)

Source: Internet
Author: User
This article brings you the content is about PHP to obtain token code implementation (), there is a certain reference value, the need for friends can refer to, I hope to help you.

Interface Invocation Request Description

HTTPS Request mode: gethttps://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=appid&secret= Appsecret

Parameter description

Parameters whether you must Description
Grant_type Is Get Access_token Fill client_credential
AppID Is Third-party user unique credentials
Secret Is Third-party user unique credential key, i.e. Appsecret

Return description

Normally, the following JSON packets are returned to the public number:

{"Access_token": "Access_token", "expires_in": 7200}

Parameter description

Parameters Description
Access_token The credential that gets to
Expires_in Voucher valid time, unit: seconds

The above is the public number to obtain Access_token documentation, this chapter simply describes how PHP gets tokens and places to be aware

1. The prepared parameters need the public number of the AppID and secret the 2 information, but also note that the secret changes you save also need to change, so do not suggest changes, save OK.

2. Need to set the white list, can be based on the server's IP address to obtain, if you do not know, it does not matter, because you can according to the error of the interface to know your IP and then set it in.

3.access_token the number of times a day is valid, remember the word is 2K times a day, but a token of the validity of 2 hours, so we must cache a token for 2 hours, so that it does not exceed the number of calls to the interface.

<?php Public Function Getaccesstoken ($appid, $secret) {$url = "https://api.weixin.qq.com/cgi-bin/token?grant_t        ype=client_credential&appid={$appid}&secret={$secret} ";        $res = $this->curl_get ($url);        $res = Json_decode ($res, 1);        if ($res [' Errcode ']!=0) throw new Exception ($res [' errmsg ']);    return $res [' Access_token ']; The Public Function Curl_get ($url) {$headers = Array (' 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 ');        $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"]) {return $sContent; ==200)        }else{return false; }    }

The above is the code for PHP to get tokens, relatively difficult than the lower.

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.