码迷,mamicode.com
首页 > 微信 > 详细

微信网页授权demo,

时间:2016-07-07 11:02:14      阅读:511      评论:0      收藏:0      [点我收藏+]

标签:

要授权首先要网页域名授权

技术分享

然后就index.php代码如下

<?php
require_once("./function.php");
$url = ‘http://‘.$_SERVER[‘HTTP_HOST‘].$_SERVER[‘REQUEST_URI‘].‘?pid=x‘;
$getuserinfo = getuserinfo($url);
print_r($getuserinfo);//可以看到获取的用户信息
?>

function.php代码如下

<?php
function getuserinfo($url){
		require_once("./oauth.php");
		$wechat = new Wechat();
		
		if(isset($_GET[‘param‘]) && isset($_GET[‘code‘])){

			$get = $_GET[‘param‘];
			$code = $_GET[‘code‘];
			if($get==‘access_token‘ && !empty($code)){
				$json = $wechat->get_access_token($code);
				return $json;
			}
				
		}else{
			$wechat->get_authorize_url($url.‘&param=access_token‘,‘STATE‘);
		}
		
}
?>

  类方法封装在oauth.php里,代码如下

<?php
class Wechat {
    
    //高级功能-》开发者模式-》获取
    private $app_id = ‘wx123456789‘;//服务号appid
    private $app_secret = ‘833134#$%^&*($%^&*$%^&*‘;//服务号密匙
 
 
    /**
     * 获取微信授权链接
     * 
     * @param string $redirect_uri 跳转地址
     * @param mixed $state 参数
     */
    public function get_authorize_url($redirect_uri = ‘‘, $state = ‘‘)
    {
        $redirect_uri = urlencode($redirect_uri);
        $url =  "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->app_id}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_userinfo&state={$state}#wechat_redirect";
		echo "<script language=‘javascript‘ type=‘text/javascript‘>";  
        echo "window.location.href=‘$url‘";  
        echo "</script>";  
    }
    
    /**
     * 获取授权token
     * 
     * @param string $code 通过get_authorize_url获取到的code
     */
    public function get_access_token($code)
    {
        $token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->app_id}&secret={$this->app_secret}&code={$code}&grant_type=authorization_code";
        $token_data = $this->http($token_url);
        if($token_data[0] == 200)
        {
            return json_decode($token_data[1], TRUE);
        }
        
        return FALSE;
    }
    
    /**
     * 获取授权后的微信用户信息
     * 
     * @param string $access_token
     * @param string $open_id
     */
    public function get_user_info($access_token = ‘‘, $open_id = ‘‘)
    {
        if($access_token && $open_id)
        {
            $info_url = "https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$open_id}&lang=zh_CN";
            $info_data = $this->http($info_url);
            
            if($info_data[0] == 200)
            {
                return json_decode($info_data[1], TRUE);
            }
        }
        
        return FALSE;
    }
    
    public function http($url, $method=‘post‘, $postfields = null, $headers = array(), $debug = false)
    {
        $ci = curl_init();
        /* Curl settings */
        curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ci, CURLOPT_TIMEOUT, 30);
        curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
 
        switch ($method) {
            case ‘POST‘:
                curl_setopt($ci, CURLOPT_POST, true);
                if (!empty($postfields)) {
                    curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
                    $this->postdata = $postfields;
                }
                break;
        }
        curl_setopt($ci, CURLOPT_URL, $url);
        curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ci, CURLINFO_HEADER_OUT, true);
 
        $response = curl_exec($ci);
        $http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
 
        if ($debug) {
            echo "=====post data======\r\n";
            var_dump($postfields);
 
            echo ‘=====info=====‘ . "\r\n";
            print_r(curl_getinfo($ci));
 
            echo ‘=====$response=====‘ . "\r\n";
            print_r($response);
        }
        curl_close($ci);
        return array($http_code, $response);
    }
 
}

?>

  搞清楚微信授权流程很重要,看function.php里的这个方法好好体会下。

微信网页授权demo,

标签:

原文地址:http://www.cnblogs.com/phpjinggege/p/5649266.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!