微(wei)信申請第三方之后可以獲(huo)取授權方的(de)很多(duo)權限,主要的(de)是生(sheng)碼(ma)和待開(kai)發(fa),生(sheng)碼(ma)的(de)第三方授權之前已經寫(xie)了(le)一篇文章,最近做(zuo)了(le)小程序待開(kai)發(fa),總結一下(xia)寫(xie)下(xia)來供大(da)家參考
注意事項:如果在調試過程中返回了錯(cuo)誤碼(ma)請到小程序代開發(fa)api頁面(mian)查(cha)看,
小程序代開發使用(yong)的域名(ming)(ming)是你申請第三方(fang)時候填寫的域名(ming)(ming),
小程序代碼模板最多只(zhi)有50個(ge),可以(yi)刪(shan)除(chu)然后(hou)重新添(tian)加。
準備工作:
申請微(wei)信第三方(fang)(fang)并且權(quan)限那邊要(yao)選上代(dai)開(kai)發,第三方(fang)(fang)申請成功之后就是準備小程(cheng)序了,需(xu)要(yao)兩個(ge)小程(cheng)序,一個(ge)作(zuo)為小程(cheng)序代(dai)碼庫(ku),一個(ge)作(zuo)為用戶測試用,需(xu)要(yao)在第三方(fang)(fang)授(shou)權(quan)。
添(tian)加(jia)小(xiao)(xiao)程(cheng)(cheng)(cheng)序(xu)(xu)代碼庫(ku): 在(zai)第三(san)方那(nei)邊將小(xiao)(xiao)程(cheng)(cheng)(cheng)序(xu)(xu)添(tian)加(jia)為開發(fa)(fa)小(xiao)(xiao)程(cheng)(cheng)(cheng)序(xu)(xu),然(ran)后(hou)該(gai)小(xiao)(xiao)程(cheng)(cheng)(cheng)序(xu)(xu)就成為了(le)第三(san)方的(de)開發(fa)(fa)小(xiao)(xiao)程(cheng)(cheng)(cheng)序(xu)(xu),之后(hou)該(gai)小(xiao)(xiao)程(cheng)(cheng)(cheng)序(xu)(xu)提交(jiao)的(de)代碼都會存(cun)入第三(san)方草稿箱(xiang),你可以選擇版本添(tian)加(jia)為模板(ban),一(yi)個第三(san)方最 多只能(neng)有50個模板(ban)。
代開(kai)發(fa)流程(cheng):
post請求公共(gong)方法,與微信服務器交(jiao)互用
代碼如下
1 protected function curl_post( $curlHttp, $postdata ) {
2 $ch = curl_init(); //用curl發送數據給api
3 curl_setopt( $ch, CURLOPT_POST, true );
4 curl_setopt( $ch, CURLOPT_POST, true );
5 curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
6 curl_setopt( $ch, CURLOPT_URL, $curlHttp );
7 curl_setopt( $ch, CURLOPT_POSTFIELDS, $postdata );
8 curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
9 curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, FALSE );
10
11 $response = curl_exec( $ch );
12 curl_close( $ch );
13 $result = json_decode( $response, true );
14 return $result;
15 }
get請求公(gong)共方法,與微(wei)信服(fu)務(wu)器交互用 代碼如下
1 protected function buildRequestForm( array $param, $method, $target='',$jump=false) {
2 $sHtml = "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' /><form id='autoSubmit' action='".$target."' method='".$method."'>";
3
4 if ( !empty( $param ) ) {
5 foreach( $param as $key => $value ) {
6 $sHtml.= "<input type='hidden' name='".$key."' value='".urldecode($value)."'/>";
7 }
8 }
9 $sHtml .= "</form>";
10
11 if($jump) $sHtml = $sHtml."<script>document.getElementById(\"autoSubmit\").submit();</script>";
12
13 return $sHtml;
14 }
獲取授權方(fang)(fang)api調(diao)用拼成(cheng)access_token公共方(fang)(fang)法 代碼如下:
1 protectd function getAccessToken( $appId ) {
2 $accessToken = '';
3
4 if ( empty( $appId ) ) {
5 return $accessToken;
6 }
7
8 // 中間的邏輯自己填充
9
10 return $accessToken;
11 }
首先是開(kai)發一套小(xiao)程序(xu)并且上傳,之后再第(di)三(san)方里邊把該版本設置(zhi)成(cheng)模板(ban),這個時(shi)候(hou)你就用(yong)了模板(ban)id(用(yong)于代碼指定用(yong))
通過(guo)調(diao)用微信接口,給用戶小(xiao)程序(xu)指定小(xiao)程序(xu)代碼
代碼(ma)如下(xia)
1 public function commitCode() {
2 $appId = input( 'app_id', '' );
3 $descript = input( 'descript', '測試代碼指定' );
4 $version = input( 'version', 'V.1.0' );
5 $templateId = input( 'template_id', 1 );
6 if ( empty( $appId ) ) {
7 $this->error( appid不能為空 );
8 return;
9 }
10
11 if ( empty( $templateId ) && ( $templateId != 0 ) ) {
12 $this->error( '模板id不能為空' );
13 return;
14 }
15
16 $accessToken = $this->getAccessToken( $appId );
17
18 // 個人信息我給清除了,空字符部分請自己補充
19 $extJson = array(
20 'extAppid' => $appId,
21 'ext' => array(
22 'attr1' => 'value1'
23 ),
24 'extPages' => array(
25 'pages/index/index' => array(
26 'navigationBarTitleText' => ''
27 ),
28 'pages/media/media' => array(
29 'navigationBarTitleText' => ''
30 )
31 ),
32 'pages' => array(
33 'pages/index/index',
34 'pages/media/media'
35 ),
36 'window' => array(
37 'backgroundColor' => '#f8f8f8',
38 'navigationBarTextStyle' => 'white',
39 "navigationBarTitleText" => "",
40 'navigationBarBackgroundColor' => '#2b3b48'
41 ),
42 'tabBar' => array(
43 'list' => array(
44 array(
45 'text' => '',
46 'pagePath' => 'pages/index/index',
47 ),
48 array(
49 'text' => '',
50 'pagePath' => 'pages/media/media',
51 )
52 )
53 ),
54 'networkTimeout' => array(
55 'request' => 10000,
56 'uploadFile' => 10000,
57 'downloadFile' => 10000,
58 'connectSocket' => 10000
59 )
60 );
61
62 $params = array(
63 'template_id' => $templateId,
64 'user_version' => $version,
65 'user_desc' => $descript,
66 'ext_json' => json_encode( $extJson, JSON_UNESCAPED_UNICODE )
67 );
68 $result = $this->curl_post( '//api.weixin.qq.com/wxa/commit?access_token='.$accessToken, json_encode( $params, JSON_UNESCAPED_UNICODE ) );
69 if ( empty( $result ) || !empty( $result['errcode'] ) ) {
70 $this->error( '代碼指定錯誤' );
71 return;
72 }
73
74 $this->success( '操作成功' );
75 return;
76 }
指定代碼之后就是查看功能是否正常了,所以就要(yao)調用微信接口獲取體(ti)驗二維碼掃碼體(ti)驗,
代(dai)碼如(ru)下
1 public function getExpCode() {
2 $appId = input( 'app_id', '' );
3 if ( empty( $appId ) ) {
4 $this->error( appid不能為空 );
5 return;
6 }
7
8 $accessToken = $this->getAccessToken( $appId );
9 if ( empty( $accessToken ) ) {
10 $this->error( '獲取授權accessToken錯誤' );
11 return;
12 }
13
14 $params = array(
15 'access_token' => $accessToken
16 );