靜默獲取微信用戶openid如此簡單,只需要一句話


頁面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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM