{"id":28723,"date":"2024-11-25T12:23:38","date_gmt":"2024-11-25T04:23:38","guid":{"rendered":"https:\/\/fwq.ai\/blog\/28723\/"},"modified":"2024-11-25T12:23:38","modified_gmt":"2024-11-25T04:23:38","slug":"%e5%be%ae%e4%bf%a1%e5%85%ac%e4%bc%97%e5%b9%b3%e5%8f%b0%e5%bc%80%e5%8f%91%e8%87%aa%e5%8a%a8%e6%9b%b4%e6%96%b0%e5%be%ae%e4%bf%a1access-token","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/28723\/","title":{"rendered":"\u5fae\u4fe1\u516c\u4f17\u5e73\u53f0\u5f00\u53d1\u81ea\u52a8\u66f4\u65b0\u5fae\u4fe1access token"},"content":{"rendered":"<p>\u672c\u6587\u4ecb\u7ecd\u5982\u4f55\u5b58\u50a8\u53ca\u66f4\u65b0 access token\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2>\u4e00\u3001Access Token<\/h2>\n<p>access_token\u662f\u516c\u4f17\u53f7\u7684\u5168\u5c40\u552f\u4e00\u7968\u636e\uff0c\u516c\u4f17\u53f7\u8c03\u7528\u5404\u63a5\u53e3\u65f6\u90fd\u9700\u4f7f\u7528access_token\u3002\u6b63\u5e38\u60c5\u51b5\u4e0b<strong>access_token\u6709\u6548\u671f\u4e3a7200\u79d2<\/strong>\uff0c\u91cd\u590d\u83b7\u53d6\u5c06\u5bfc\u81f4\u4e0a\u6b21\u83b7\u53d6\u7684access_token\u5931\u6548\u3002<\/p>\n<p>\u516c\u4f17\u53f7\u53ef\u4ee5\u4f7f\u7528AppID\u548cAppSecret\u8c03\u7528\u672c\u63a5\u53e3\u6765\u83b7\u53d6access_token\u3002AppID\u548cAppSecret\u53ef\u5728\u5f00\u53d1\u6a21\u5f0f\u4e2d\u83b7\u5f97\uff08\u9700\u8981\u5df2\u7ecf\u6210\u4e3a\u5f00\u53d1\u8005\uff0c\u4e14\u5e10\u53f7\u6ca1\u6709\u5f02\u5e38\u72b6\u6001\uff09\u3002<strong>\u6ce8\u610f\u8c03\u7528\u6240\u6709\u5fae\u4fe1\u63a5\u53e3\u65f6\u5747\u9700\u4f7f\u7528https\u534f\u8bae\u3002<\/strong><\/p>\n<p><strong>\u63a5\u53e3\u8c03\u7528\u8bf7\u6c42\u8bf4\u660e<\/strong><\/p>\n<pre>http\u8bf7\u6c42\u65b9\u5f0f: GET\r\nhttps:\/\/api.weixin.qq.com\/cgi-bin\/token?grant_type=client_credential&amp;appid=APPID&amp;secret=APPSECRET<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u53c2\u6570\u8bf4\u660e<\/strong><\/p>\n<table border=\"1\">\n<tbody>\n<tr>\n<th>\u53c2\u6570<\/th>\n<th>\u662f\u5426\u5fc5\u987b<\/th>\n<th>\u8bf4\u660e<\/th>\n<\/tr>\n<tr>\n<td>grant_type<\/td>\n<td>\u662f<\/td>\n<td>\u83b7\u53d6access_token\u586b\u5199client_credential<\/td>\n<\/tr>\n<tr>\n<td>appid<\/td>\n<td>\u662f<\/td>\n<td>\u7b2c\u4e09\u65b9\u7528\u6237\u552f\u4e00\u51ed\u8bc1<\/td>\n<\/tr>\n<tr>\n<td>secret<\/td>\n<td>\u662f<\/td>\n<td>\u7b2c\u4e09\u65b9\u7528\u6237\u552f\u4e00\u51ed\u8bc1\u5bc6\u94a5\uff0c\u65e2appsecret<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u8fd4\u56de\u8bf4\u660e<\/strong><\/p>\n<p>\u6b63\u5e38\u60c5\u51b5\u4e0b\uff0c\u5fae\u4fe1\u4f1a\u8fd4\u56de\u4e0b\u8ff0JSON\u6570\u636e\u5305\u7ed9\u516c\u4f17\u53f7\uff1a<\/p>\n<pre>{\"access_token\":\"ACCESS_TOKEN\",\"expires_in\":7200}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>&nbsp;\u4e09\u3001\u5b9e\u73b0&nbsp;<\/p>\n<\/p>\n<pre>class class_weixin\r\n{\r\n    var $appid = APPID;\r\n    var $appsecret = APPSECRET;\r\n\r\n    \/\/\u6784\u9020\u51fd\u6570\uff0c\u83b7\u53d6Access Token\r\n    public function __construct($appid = NULL, $appsecret = NULL)\r\n    {\r\n        if($appid &amp;&amp; $appsecret){\r\n            $this-&gt;appid = $appid;\r\n            $this-&gt;appsecret = $appsecret;\r\n        }\r\n\r\n        \/\/1. \u6570\u636e\u5e93\u5f62\u5f0f\r\n        \/*\r\n        DROP TABLE IF EXISTS `wx_token`;\r\n        CREATE TABLE IF NOT EXISTS `wx_token` (\r\n          `id` int(1) NOT NULL,\r\n          `type` varchar(20) NOT NULL,\r\n          `expire` varchar(16) NOT NULL,\r\n          `value` varchar(600) NOT NULL,\r\n          PRIMARY KEY (`id`)\r\n        ) ENGINE=MyISAM DEFAULT CHARSET=utf8;\r\n\r\n        INSERT INTO `wx_token` (`id`, `type`, `expire`, `value`) VALUES\r\n        (1, 'access_token', '1425534992', 't3oyW9fRnOWKQHQhZXoEH-pgThhjmnCqTVpaLyUD'),\r\n        (2, 'jsapi_ticket', '', '');\r\n        *\/\r\n        $con = mysql_connect(MYSQLHOST.':'.MYSQLPORT, MYSQLUSER, MYSQLPASSWORD);\r\n        mysql_select_db(MYSQLDATABASE, $con);\r\n        $result = mysql_query(\"SELECT * FROM `wx_token` WHERE `type` = 'access_token'\");\r\n        while($row = mysql_fetch_array($result))\r\n        {\r\n            $this-&gt;access_token = $row['value'];\r\n            $this-&gt;expires_time = $row['expire'];\r\n            break;\r\n        }\r\n        if (time() &gt; ($this-&gt;expires_time + 3600)){\r\n            $url = \"https:\/\/api.weixin.qq.com\/cgi-bin\/token?grant_type=client_credential&amp;appid=\".$this-&gt;appid.\"&amp;secret=\".$this-&gt;appsecret;\r\n            $res = $this-&gt;http_request($url);\r\n            $result = json_decode($res, true);\r\n            $this-&gt;access_token = $result[\"access_token\"];\r\n            $this-&gt;expires_time = time();\r\n            mysql_query(\"UPDATE `wx_token` SET `expire` = '$this-&gt;expires_time', `value` = '$this-&gt;access_token' WHERE `type` = 'access_token';\");\r\n        }\r\n\r\n        \/\/2. \u7f13\u5b58\u5f62\u5f0f\r\n        if (isset($_SERVER['HTTP_APPNAME'])){        \/\/SAE\u73af\u5883\uff0c\u9700\u8981\u5f00\u901amemcache\r\n            $mem = memcache_init();\r\n        }else {                                        \/\/\u672c\u5730\u73af\u5883\uff0c\u9700\u5df2\u5b89\u88c5memcache\r\n            $mem = new Memcache;\r\n            $mem-&gt;connect('localhost', 11211) or die (\"Could not connect\");\r\n        }\r\n        $this-&gt;access_token = $mem-&gt;get($this-&gt;appid);\r\n        if (!isset($this-&gt;access_token) || empty($this-&gt;access_token)){\r\n            $url = \"https:\/\/api.weixin.qq.com\/cgi-bin\/token?grant_type=client_credential&amp;appid=\".$this-&gt;appid.\"&amp;secret=\".$this-&gt;appsecret;\r\n            $res = $this-&gt;http_request($url);\r\n            $result = json_decode($res, true);\r\n            $this-&gt;access_token = $result[\"access_token\"];\r\n            $mem-&gt;set($this-&gt;appid, $this-&gt;access_token, 0, 3600);\r\n        }\r\n\r\n        \/\/3. \u672c\u5730\u5199\u5165\r\n        $res = file_get_contents('access_token.json');\r\n        $result = json_decode($res, true);\r\n        $this-&gt;expires_time = $result[\"expires_time\"];\r\n        $this-&gt;access_token = $result[\"access_token\"];\r\n\r\n        if (time() &gt; ($this-&gt;expires_time + 3600)){\r\n            $url = \"https:\/\/api.weixin.qq.com\/cgi-bin\/token?grant_type=client_credential&amp;appid=\".$this-&gt;appid.\"&amp;secret=\".$this-&gt;appsecret;\r\n            $res = $this-&gt;http_request($url);\r\n            $result = json_decode($res, true);\r\n            $this-&gt;access_token = $result[\"access_token\"];\r\n            $this-&gt;expires_time = time();\r\n            file_put_contents('access_token.json', '{\"access_token\": \"'.$this-&gt;access_token.'\", \"expires_time\": '.$this-&gt;expires_time.'}');\r\n        }\r\n\r\n        \/\/4. \u5b9e\u65f6\u62c9\u53d6\r\n        $url = \"https:\/\/api.weixin.qq.com\/cgi-bin\/token?grant_type=client_credential&amp;appid=\".$this-&gt;appid.\"&amp;secret=\".$this-&gt;appsecret;\r\n        $res = $this-&gt;http_request($url);\r\n        $result = json_decode($res, true);\r\n        $this-&gt;access_token = $result[\"access_token\"];\r\n        $this-&gt;expires_time = time();\r\n    }<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>&nbsp;\u66f4\u591a\u5fae\u4fe1\u516c\u4f17\u5e73\u53f0\u5f00\u53d1\u81ea\u52a8\u66f4\u65b0\u5fae\u4fe1access token\u76f8\u5173\u6587\u7ae0\u8bf7\u5173\u6ce8PHP\u4e2d\u6587\u7f51\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u4ecb\u7ecd\u5982\u4f55\u5b58\u50a8\u53ca\u66f4\u65b0 access token\u7684\u65b9\u6cd5\u3002 \u4e00\u3001Access Token access_token\u662f\u516c\u4f17\u53f7\u7684\u5168\u5c40\u552f\u4e00\u7968\u636e\uff0c\u516c\u4f17\u53f7\u8c03\u7528\u5404\u63a5\u53e3\u65f6\u90fd\u9700\u4f7f\u7528access_token\u3002\u6b63\u5e38\u60c5\u51b5\u4e0baccess_token\u6709\u6548\u671f\u4e3a7200\u79d2\uff0c\u91cd\u590d\u83b7\u53d6\u5c06\u5bfc\u81f4\u4e0a\u6b21\u83b7\u53d6\u7684access_token\u5931\u6548\u3002 \u516c\u4f17\u53f7\u53ef\u4ee5\u4f7f\u7528AppID\u548cAppSecret\u8c03\u7528\u672c\u63a5\u53e3\u6765\u83b7\u53d6access_token\u3002AppID\u548cAppSecret\u53ef\u5728\u5f00\u53d1\u6a21\u5f0f\u4e2d\u83b7\u5f97\uff08\u9700\u8981\u5df2\u7ecf\u6210\u4e3a\u5f00\u53d1\u8005\uff0c\u4e14\u5e10\u53f7\u6ca1\u6709\u5f02\u5e38\u72b6\u6001\uff09\u3002\u6ce8\u610f\u8c03\u7528\u6240\u6709\u5fae\u4fe1\u63a5\u53e3\u65f6\u5747\u9700\u4f7f\u7528https\u534f\u8bae\u3002 \u63a5\u53e3\u8c03\u7528\u8bf7\u6c42\u8bf4\u660e http\u8bf7\u6c42\u65b9\u5f0f: GET https:\/\/api.weixin.qq.com\/cgi-bin\/token?grant_type=client_credential&amp;appid=APPID&amp;secret=APPSECRET \u767b\u5f55\u540e\u590d\u5236 \u53c2\u6570\u8bf4\u660e \u53c2\u6570 \u662f\u5426\u5fc5\u987b \u8bf4\u660e grant_type \u662f \u83b7\u53d6access_token\u586b\u5199client_credential appid \u662f \u7b2c\u4e09\u65b9\u7528\u6237\u552f\u4e00\u51ed\u8bc1 secret \u662f \u7b2c\u4e09\u65b9\u7528\u6237\u552f\u4e00\u51ed\u8bc1\u5bc6\u94a5\uff0c\u65e2appsecret \u8fd4\u56de\u8bf4\u660e \u6b63\u5e38\u60c5\u51b5\u4e0b\uff0c\u5fae\u4fe1\u4f1a\u8fd4\u56de\u4e0b\u8ff0JSON\u6570\u636e\u5305\u7ed9\u516c\u4f17\u53f7\uff1a {&#8220;access_token&#8221;:&#8221;ACCESS_TOKEN&#8221;,&#8221;expires_in&#8221;:7200} \u767b\u5f55\u540e\u590d\u5236 &nbsp;\u4e09\u3001\u5b9e\u73b0&nbsp; class class_weixin { var $appid = APPID; var $appsecret = APPSECRET; \/\/\u6784\u9020\u51fd\u6570\uff0c\u83b7\u53d6Access Token public function __construct($appid = NULL, $appsecret = NULL) { if($appid &amp;&amp; $appsecret){ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-28723","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/28723","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=28723"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/28723\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=28723"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=28723"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=28723"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}