微信公衆號開發Day02:access_token 獲取用戶關注列表,網頁授權(有bug),生成帶參數的二維碼

目錄

0x00 access_token:

0x01 獲取access_token

0x02 自定義菜單的創建

0x03 獲取用戶關注列表

0x04 網頁授權

#實戰:

0x05 生成帶參數的二維碼


0x00 access_token:

access_token 是公衆號的全局唯一接口調用憑據,公衆號調用各種接口時都需要access_token

access_token 每天調用的次數上限是2000次,一個access_token的有效時間是2個小時,所以我們必須在Linux服務器上設置一個定時

任務,每隔2小時就獲取一次access_token

0x01 獲取access_token

<?php
$appid='你的appid';
$secret='你的secret';
$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";
// 將請求獲得的json字符串轉化爲關聯數組
//ps:json_decode()將json字符串轉化爲對象
//json_decode(xxx,true) 將json字符串轉化爲關聯數組
$access_token=json_decode(file_get_contents($url),true);
// 將獲取到的access_token保存到文件中
file_put_contents('access_token.txt',$access_token['access_token']);
?>

設置定時任務:

crontab -e

分 時 日 月 周

* */1 * * * /usr/local/php/bin/php /home/weixin/access.php > /dev/null 2>&1

0x02 自定義菜單的創建

最多3個一級菜單,一個一級菜單最多5個二級菜單

menu.php 發起請求,設置菜單

<?php
//獲取文件中保存的access_token
$access_token = file_get_contents('access_token.txt');
//獲取菜單中的json配置文件
$menu_json=file_get_contents('menu.json');
//發起請求
$msg = http_post($access_token,$menu_json);
echo $msg;

function http_post($access_token,$data){
    $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=$access_token";
    $ch  = curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_POST,1);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
    $res = curl_exec($ch);
    curl_exec($ch);
    return $res;
}

?>

menu.json 菜單的配置文件

{
    "button":[
        {
            "name":"站點",
            "sub_button":[
                {
                    "name":"DataBank西電站",
                    "type":"view",
                    "url":"http://101.200.142.148:90/"
                },
                {
                    "name":"DataBank哈商大站",
                    "type":"view",
                    "url":"http://101.200.142.148:8080/"
                },
                {
                    "name":"掃碼推事件",
                    "type":"scancode_push",
                    "key":"rselfmenu_0_1",
                    "sub_button":[ ]
                },
                {
                    "name":"掃碼帶提示",
                    "type":"scancode_waitmsg",
                    "key":"rselfmenu_0_0",
                    "sub_button":[ ]

                }
                
            ]
        },
        {
            "name":"測試三",
            "sub_button":[
                {
                    "type":"pic_sysphoto",
                    "name":"系統拍照發圖",
                    "key":"rselfmenu_1_0",
                    "sub_button":[ ]
                },
                {
                    "type":"pic_photo_or_album",
                    "name":"拍照或者相冊發圖",
                    "key":"rselfmenu_1_1",
                    "sub_button":[ ]
                },
                {
                    "type":"pic_weixin",
                    "name":"微信相冊發圖",
                    "key":"rselfmenu_1_2",
                    "sub_button":[ ]
                }
            ]
        },
        {
            "name":"關於我們",
            "type":"click",
            "key":"test_1"
        }

    ]

}

掃碼推事件:一掃二維碼/條形碼,如果是網址就直接跳轉

掃碼帶提示:一掃二維碼/條形碼,就會將二維碼/條形碼的內容就爲以post的形式發送到服務器上

0x03 獲取用戶關注列表

需要認證號,否則將會報

{"errcode":48001,"errmsg":"api unauthorized hints: [thbCMEMre-hzMTVa!]"}

1.獲取用戶關注列表

https://api.weixin.qq.com/cgi-bin/user/get?access_token=你的access_token

2.獲取用戶的基本信息

https://api.weixin.qq.com/cgi-bin/user/info?access_token=你的ACCESS_TOKEN&openid=該用戶OPENID&lang=zh_CN

0x04 網頁授權

#oAuth協議:

##名詞定義:

(1)third-party application 第三方應用程序,又稱客戶端

例如:我們自己開發的網站有一個微信登錄的功能,那麼我們的網站就是第三方應用程序

(2)resource owner 資源所有者,即點擊登錄的用戶

(3)User Agent 用戶代理,即瀏覽器

(4)Authorization server 認證服務器  微信專門用來認證的服務器

(5)Resource server 資源服務器  微信的用戶頭像等圖片等資源可能在另一個服務器上

##oAuth的實現思路:

騰訊當然不可能讓 我們的網站 直接連 微信的數據庫,所以騰訊設置了一個授權層(authorization layer),我們去請求授權層去獲取相應的數據。

(A)我們的web站點 請求用戶(資源的主人)給予 授權(也就是讓用戶輸入qq密碼點確認或者掃描微信二維碼)

(B) 用戶給予我們授權

(C)我們拿着用戶給予的授權  發送給 騰訊的認證服務器

(D) 認證服務器認證成功後,返回給我們一個 令牌

(E) 我們拿着 令牌 又去 請求 騰訊的資源服務器

(F)資源服務其返回被保護的資源(用戶頭像呀用戶信息呀)

#實戰:

開發 - 接口權限 - 網頁服務 - 網頁帳號 - 網頁授權獲取用戶基本信息”的配置選項中,修改授權回調域名

微信跳轉有問題!暫定

0x05 生成帶參數的二維碼

思路:

首先向指定的url發送post請求,參數以json字符串的形式作爲post請求的參數,來獲取ticket

然後用ticket請求二維碼

代碼:

<?php
//獲取文件中保存的access_token
$access_token = file_get_contents('access_token.txt');


/**
 * 獲取臨時二維碼的ticket
 * 傳送整數型自定義參數
 * expire_seconds: 臨時二維碼有效時間
 * scene_id: 你要帶的整數型參數,自定義
 */
// $json = '{"expire_seconds": 604800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": 123}}}';

/**
 * 獲取臨時二維碼的ticket
 * 傳送字符串型自定義參數
 * scene_str:自定義字符串參數
 */
// $json = '{"expire_seconds": 604800, "action_name": "QR_STR_SCENE", "action_info": {"scene": {"scene_str": "shang"}}}';

/**
 * 獲取永久二維碼的ticket
 * 
 */
// $json ='{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": 123}}} ';
$json = '{"action_name": "QR_LIMIT_STR_SCENE", "action_info": {"scene": {"scene_str": "test"}}}';
$url='https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='.$access_token;
//發起POST請求
$msg = http_post($url,$json);

// echo $msg;
// 將json字符串解碼爲數組
$ticket = json_decode($msg,true);
$qr_url = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.$ticket['ticket'];
//用ticket請求二維碼
$res = file_get_contents($qr_url);
//必須用header說明類型,否則亂碼
header('Content-type:image/png');
echo $res;


/**
 * 發送post請求
 * url 目標網址
 * json post請求攜帶的參數,以json字符串的形式
 */
function http_post($url,$json){
    $ch  = curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_POST,1);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$json);
    $res = curl_exec($ch);
    curl_exec($ch);
    return $res;
}

?>

當用戶掃描對應的二維碼時,我們服務器上的用來監聽用戶發來消息的index.php,就會獲取到對應的post傳參(xml的形式)

其中xml中就有eventkey 和ticket ,eventkey就是我們自定義的二維碼參數。

如果用戶沒有關注公衆號,掃描參數二維碼並關注,則event將是subscribe

event_key 爲qrscene_二維碼參數

如果用戶已經關注了公衆號,掃描二維碼將進入公衆號,則event爲scan

event_key爲二維碼參數

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章