💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# 服务器基础接口 WechatServerApi **目录 (Table of Contents)** [TOC] ## 类基本信息 **类简要描述:** - 微信公众平台服务端基础接口,主要用于获取API令牌和JS API令牌等。 **类名字空间:** - endor/wechat/server **类继承关系:** WechatServerApi -> WechatCommon ## 类成员函数/方法 ### __construct($configure) + 参数列表: |参数名|类型|必填项|说明 |:---- |:---|:----- |----- | |$configure| object (WechatConfig) | 是 | 微信配置参数对象 | + 返回值: `无` + 函数说明: `构造函数` ### getAccessToken(); + 参数列表: `无` + 返回值: `array` + 返回值(示例) ``` [ "access_token" => 4SyHI39Irn4ZEkQby2LTTQiAHAZZWXX .... "expires_in" => 7200 ] ``` + 错误返回 `如果函数内有错误或异常,抛出 WechatException异常 ` + 函数说明: `获取微信公众号API 令牌,该令牌请保存在数据库,有获取次数限制` + Eample: ```PHP $wxConfigure = new WechatConfig(); $wxConfigure->setParameter("appid","wx210100001"); $wxConfigure->setParameter("appsecret","BeU_91p2K7idjxcHaI6jcXkYHPlDj3"); $wechatServer = new WechatServerApi($wxConfigure); try{ $results = $wechatServer->getAccessToken(); $access_token = $results['access_token']; $epxires_in = $results['expires_in']; $wxConfigure->setParameter("access_token ", $access_token); } catch(WechatException $wechatException) { echo "[err code:] ". $wechatException->getCode(); echo "[error msg:] " . $wechatException->getMessage() ; } ``` ### getWechatJsapiTicket() + 参数列表: `无` + 返回值: `array` + 返回值(示例) ``` [ "ticket" => 4SyHI39Irn4ZEkQby2LTTQiAHAZZWXX .... "expires_in" => 7200 ] ``` + 错误返回 `如果函数内有错误或异常,抛出 WechatException异常 ` + 函数说明: `获取微信公众号JS API Ticket,该Ticket请保存在数据库,有获取次数限制` + Eample: ```PHP // 假设我们在这里已经通过getAccessToken()获取到了全局access_token,存在变量 // $access_token中 $wxConfigure = new WechatConfig(); $wxConfigure->setParameter("appid","wx210100001"); $wxConfigure->setParameter("appsecret","BeU_91p2K7idjxcHaI6jcXkYHPlDj3"); $wxConfigure->setParameter("access_token",$access_token); $wechatServer = new WechatServerApi($wxConfigure); try{ $results = $wechatServer->getWechatJsapiTicket(); $ticket = $results['ticket']; $epxires_in = $results['expires_in']; $wxConfigure->setParameter("jsapi_ticket",$ticket); } catch(WechatException $wechatException) { echo "[err code:] ". $wechatException->getCode(); echo "[error msg:] " . $wechatException->getMessage() ; } ``` ### getJsApiSign() + 参数列表: `无` + 返回值: `array` + 返回值(示例) ``` [ "appId" => 4SyHI39Irn4ZEkQbysdadsdaX .... "nonceStr" => 7200, "timestamp"=>1555533333, "url"=>"https://mobile.test.com/", "signature"=>"f3fd4sdadad4900e0305dab3048c171630984c8", "rawString"=>.... 此处略 ] ``` + 错误返回 `如果函数内有错误或异常,抛出 WechatException异常 ` + 函数说明: `获取微信公众号Js Api 签名,该返回值用于初始化 js wx 对象 ` + Eample: ```PHP // 假设我们在这里已经通过getWechatJsapiTicket()获取到了jsapi ticket,存在变量 // $ticket 中 $wxConfigure = new WechatConfig(); $wxConfigure->setParameter("appid","wx210100001"); $wxConfigure->setParameter("appsecret","BeU_91p2K7idjxcHaI6jcXkYHPlDj3"); $wxConfigure->setParameter("ticket",$ticket); $wechatServer = new WechatServerApi($wxConfigure); try{ $results = $wechatServer->getJsApiSign(); echo $results; // 将对象返回给JS页面 } catch(WechatException $wechatException) { echo "[err code:] ". $wechatException->getCode(); echo "[error msg:] " . $wechatException->getMessage() ; } ``` ```Javascript <html> <header> <script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.1.0.js"></script> </header> <body> </body> <script> // 假设前端获取到后端返回来的jsapi 签名对象为 results wx.config({ debug: false, appId:results.appId, timestamp:results.timestamp, nonceStr: 'results.nonceStr, signature:results.signature, jsApiList: [ // 所有要调用的 API 都要加到这个列表中 'checkJsApi', 'openLocation', 'getLocation', 'scanQRCode', 'translateVoice', ] }); </script> ``` ### getWechatServerIp() + 参数列表: `无` + 返回值: `array` + 返回值(示例) ``` [ "101.226.233.139", "101.226.233.140", .... ] ``` + 错误返回 `如果函数内有错误或异常,抛出 WechatException异常 ` + 函数说明: `获取微信公众号服务器IP列表 `