目前有3個接口能夠生成小程序碼,開發者能夠根據本身的須要選擇合適的接口。
第一步:獲取 access_token javascript
public function getWxAccessToken(){ $appid='wxbcfaa005da7b16**'; $appsecret='****'; if(Session::get('access_token_'.$appid) && Session::get('expire_time_'.$appid)>time()){ return Session::get('access_token_'.$appid); }else{ $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret; $access_token = $this->makeRequest($url); $access_token = json_decode($access_token['result'],true); Session::set('access_token_'.$appid,$access_token); Session::set('expire_time_'.$appid,time()+7000); return $access_token; } } /** * 發起http請求 * @param string $url 訪問路徑 * @param array $params 參數,該數組多於1個,表示爲POST * @param int $expire 請求超時時間 * @param array $extend 請求僞造包頭參數 * @param string $hostIp HOST的地址 * @return array 返回的爲一個請求狀態,一個內容 */ public function makeRequest($url, $params = array(), $expire = 0, $extend = array(), $hostIp = '') { if (empty($url)) { return array('code' => '100'); } $_curl = curl_init(); $_header = array( 'Accept-Language: zh-CN', 'Connection: Keep-Alive', 'Cache-Control: no-cache' ); // 方便直接訪問要設置host的地址 if (!empty($hostIp)) { $urlInfo = parse_url($url); if (empty($urlInfo['host'])) { $urlInfo['host'] = substr(DOMAIN, 7, -1); $url = "http://{$hostIp}{$url}"; } else { $url = str_replace($urlInfo['host'], $hostIp, $url); } $_header[] = "Host: {$urlInfo['host']}"; } // 只要第二個參數傳了值以後,就是POST的 if (!empty($params)) { curl_setopt($_curl, CURLOPT_POSTFIELDS, http_build_query($params)); curl_setopt($_curl, CURLOPT_POST, true); } if (substr($url, 0, 8) == 'https://') { curl_setopt($_curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($_curl, CURLOPT_SSL_VERIFYHOST, FALSE); } curl_setopt($_curl, CURLOPT_URL, $url); curl_setopt($_curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($_curl, CURLOPT_USERAGENT, 'API PHP CURL'); curl_setopt($_curl, CURLOPT_HTTPHEADER, $_header); if ($expire > 0) { curl_setopt($_curl, CURLOPT_TIMEOUT, $expire); // 處理超時時間 curl_setopt($_curl, CURLOPT_CONNECTTIMEOUT, $expire); // 創建鏈接超時時間 } // 額外的配置 if (!empty($extend)) { curl_setopt_array($_curl, $extend); } $result['result'] = curl_exec($_curl); $result['code'] = curl_getinfo($_curl, CURLINFO_HTTP_CODE); $result['info'] = curl_getinfo($_curl); if ($result['result'] === false) { $result['result'] = curl_error($_curl); $result['code'] = -curl_errno($_curl); } curl_close($_curl); return $result; }
第二步:調用接口生成微信二維碼(這裏以接口B爲例)php
public function getWxcode(){ $ACCESS_TOKEN=$this->getWxAccessToken(); $url="https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$ACCESS_TOKEN['access_token']; $post_data= array( 'page'=>'pages/caregory/index', 'scene'=>'34,S853EE4QRP'//34%2CS853EE4QRP ); $post_data=json_encode($post_data); $data=$this->send_post($url,$post_data); $result=$this->data_uri($data,'image/png'); return '<image src='.$result.'></image>'; } /** * 消息推送http * @param $url * @param $post_data * @return bool|string */ protected function send_post( $url, $post_data ) { $options = array( 'http' => array( 'method' => 'POST', 'header' => 'Content-type:application/json', //header 須要設置爲 JSON 'content' => $post_data, 'timeout' => 60 //超時時間 ) ); $context = stream_context_create( $options ); $result = file_get_contents( $url, false, $context ); return $result; } //二進制轉圖片image/png public function data_uri($contents, $mime) { $base64 = base64_encode($contents); return ('data:' . $mime . ';base64,' . $base64); }
接口A: 適用於須要的碼數量較少的業務場景 接口地址:java
https://api.weixin.qq.com/wxa/getwxacode?access_token=ACCESS_TOKEN json
參數以下:
注意:經過該接口生成的小程序碼,永久有效,數量限制見文末說明,請謹慎使用。用戶掃描該碼進入小程序後,將直接進入 path 對應的頁面。
接口B:適用於須要的碼數量極多,或僅臨時使用的業務場景:小程序
https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN api
參數以下:
注意:經過該接口生成的小程序碼,永久有效,數量暫無限制。用戶掃描該碼進入小程序後,開發者需在對應頁面獲取的碼中 scene 字段的值,再作處理邏輯。使用以下代碼能夠獲取到二維碼中的 scene 字段的值。調試階段能夠使用開發工具的條件編譯自定義參數 scene=xxxx 進行模擬,開發工具模擬時的 scene 的參數值須要進行 urlencode數組
// 這是首頁的 js Page({ onLoad: function(options) { // options 中的 scene 須要使用 decodeURIComponent 才能獲取到生成二維碼時傳入的 scene var scene = decodeURIComponent(options.scene) } })
接口C:適用於須要的碼數量較少的業務場景: xcode
https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKEN 微信
參數以下:
注意:經過該接口生成的小程序二維碼,永久有效,數量限制見文末說明,請謹慎使用。用戶掃描該碼進入小程序後,將直接進入 path 對應的頁面。app