微信公众号开发:获取 access_token 的两种方式 cURL 函数 和 file_get_contents 函数

关于 access_token

access_token 是公众号的全局唯一接口调用凭据,公众号调用各接口时都需使用 access_token。
access_token 的存储至少要保留 512 个字符空间。access_token 的有效期目前为 2 个小时。

接口调用说明

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

参数
参数 是否必须 说明
grant_type Y 获取access_token填写client_credential
appid Y 第三方用户唯一凭证
secret Y 第三方用户唯一凭证密钥,即 appsecret

代码块

  • 方法一 :curl_init() 函数
  • 方法二 :file_get_contents 函数
效果图
  • $output

string(194) "{"access_token":"11_-S30IWoUhYZvZw2Qe......","expires_in":7200}"

  • json_decode($output)

object(stdClass)#6 (2) { ["access_token"]=> string(157) "11_AkasWeD0okdTqXDyqw4......" ["expires_in"]=> int(7200) }

  • json_decode($output, true)

array(2) { ["access_token"]=> string(136) "11_OuFwGg-aW8y6EC1Gt1dVi......" ["expires_in"]=> int(7200) }

你可能感兴趣的:(微信公众号开发:获取 access_token 的两种方式 cURL 函数 和 file_get_contents 函数)