頁面A
getopenid.php
<?php /** * Created by PhpStorm. * User: sunjianyin * Date: 2017/10/7 * Time: 下午10:22 */ header('Location:https://open.weixin.qq.com/connect/oauth2/authorize?appid=OPENID&redirect_uri=b.php&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect');
其中的openid和回調的uri需要再自己設置,需要注意的是redirect_uri必須是在設置回調的域名下;
不需要任何其他,初始第一步這樣,跳轉到頁面b.php,其中再獲取$code=$_GET['code'],就是code了
主要還是要看 https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842
b.php
$code = $_GET['code'];//獲取code $weixin = file_get_contents("https://api.weixin.qq.com/sns/oauth2/access_token?appid=這里是你的APPID&secret=這里是你的SECRET&code=".$code."&grant_type=authorization_code");//通過code換取網頁授權access_token $jsondecode = json_decode($weixin); //對JSON格式的字符串進行編碼 $array = get_object_vars($jsondecode);//轉換成數組 $openid = $array['openid'];//輸出openid