首頁  >  文章  >  微信小程式  >  微信如何取得openid

微信如何取得openid

小云云
小云云原創
2018-03-19 13:23:285900瀏覽

本文主要和大家分享微信如何取得openid,首先要明確一點,你要使用者授權,是需要引導使用者到你的授權頁面的。

1、你得有個連結

https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=URI&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect#wechat_redirect#wechat_redirect#wechat_redirect#wechat_redirect#wechat_redirect#wechat_redirect #wechat_redirect#wechat_redirect#wechat_redirect#wechat_redirect#wechat_redirect#wechat_redirect#wechat_redirect#wechat_redirect

說明:

APPID是你的微信公眾號ID,以wx開頭

URI為你的授權入口,一般是微信公眾號指向的主網域下,我在這裡放在了:http://www.xxoo.com/home/index/test

注意轉義,所以URI=http%3A%2F%2Fwww.xxoo.com%2Fhome%2Findex% 2Ftest

2、這個連結可以產生一個二維碼給人家掃,例如到這個網站:https://cli.im/

把上面替換好的連結填進去,產生即可。

3、取得openid的程式碼

function test(){
        $code=$_GET['code'];
        //echo htmlspecialchars($code);exit;
        $appid='wx**********';  //APPID
        $appsecret='**************';  //APP密钥
        $url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';
        $header [] = "content-type: application/json; charset=UTF-8";
        $ch = curl_init ();
        curl_setopt ( $ch, CURLOPT_URL, $url );
        //请求的方式是post
        curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, "GET" );
        // 禁用后cURL将终止从服务端进行验证
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
        //不检查证书
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, FALSE );
        //发送头部字段
        curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header );
        //告诉对方 自己的浏览器型号
        curl_setopt ( $ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)' );
        //即表示自动进行跳转抓取(如果URL发生了302重定向)继续抓取
        curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 1 );
        //自动重定向开启
        curl_setopt ( $ch, CURLOPT_AUTOREFERER, 1 );
        //发送的post参数
        //curl_setopt ( $ch, CURLOPT_POSTFIELDS, $param );
        //得到的结果不显示在屏幕上,作为变量结果储存
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
        $res = curl_exec ( $ch );
        //如果运行错误,返回一个错误号
        $flat = curl_errno ( $ch );
        if ($flat) {
            $data = curl_error ( $ch );
        }
        curl_close ( $ch );
        //拿到了返回结果后json格式转化为可以使用的数组格式
        $res = json_decode ( $res, true );
        //$access_token=$res['access_token'];
        $openid=$res['openid'];
        //跳转到抽奖页面
        header('Location: http://wx.xxoo.com/test/index/index/openid/'.$openid);
    }

這樣可以透過取得openid來辨識每一個微信用戶。

相關推薦:

微信小程式取得openid及使用者資訊的方法

微信小程式取得session_key與openid的案例(圖)

微信公眾號開發網頁中及時取得目前使用者Openid及注意事項

#

以上是微信如何取得openid的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn