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

php获取微信用户信息(没测试过)

时间:2017-08-09 13:07:43      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:wechat   exp   inf   time()   用户信息   使用   type   订阅   pen   

<?php   
/** 
 * 通过$appid、$appsecret获得基础支持的接口唯一凭证access_token,返回值为array类型 
 */  
function get_access_token_base($appid , $appsecret ) {  
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";  
        $output = https_request( $url );  
        $jsoninfo = json_decode($output, true);  
        return $jsoninfo;  
}  
  
//=======================================================================================================================================  
  
/** 
 * 通过网页授权access_token和用户openid获取用户信息 
 * 返回值为array类型 
 */  
public function get_userinfo($access_token , $openid ) {  
    $url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";  
    $output = https_request( $url );  
    $jsoninfo = json_decode($output, true);  
    return $jsoninfo;  
}  
  
//=======================================================================================================================================  
  
/** 
 * 获取JS接口中的签名signature 
 */  
function jssdk( $appid , $appsecret ,$access_token){  
    //获取缓存的access_token  
    $access_token = S(access_token);  
    if( $access_token =="" ) {  
        $jsoninfo = $this->get_access_token( $appid, $appsecret );  
        $access_token = $jsoninfo[access_token];  
        S(access_token , $access_token , 3600);  
    }  
          
    $jsapi = file_get_contents("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$access_token."&type=jsapi");  
    $jsapi = json_decode($jsapi);  
    $j = get_object_vars($jsapi);  
    $jsapi = $j[ticket];//get JSAPI  
          
    $time = 14999923234;  
    $noncestr= $time;  
    $jsapi_ticket= $jsapi;  
    $timestamp=$time;  
    $url=http://.$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];  
    $and = "jsapi_ticket=".$jsapi_ticket."&noncestr=".$noncestr."×tamp=".$timestamp."&url=".$url."";  
    $signature = sha1($and);  
    return $signature;  
}  
  
//=======================================================================================================================================  
  
/** 
 * 若在TP框架中,可用此方法将微信JS接口所需要的参数传递至模板中 
 */  
function js_param_to_temp( $appid , $appsecret, $access_token ) {  
    //获取缓存的access_token  
    $access_token = S(access_token);  
    if( $access_token =="" ) {  
        $jsoninfo = $this->get_access_token( $appid, $appsecret );  
        $access_token = $jsoninfo[access_token];  
        S(access_token , $access_token , 3600);  
    }  
    $this->assign(appid , $appid);  
    $jsapi = file_get_contents("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$access_token."&type=jsapi");  
    $jsapi = json_decode($jsapi);  
    //将对象转为数组  
    $j = get_object_vars($jsapi);  
    $jsapi = $j[ticket];//get JSAPI  
       
    $time = time();  
    $noncestr= $time;  
    $this->assign(noncestr , $noncestr);  
    $jsapi_ticket= $jsapi;  
    $timestamp=$time;  
    $this->assign(timestamp , $timestamp);  
    $url=http://.$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];  
    $and = "jsapi_ticket=".$jsapi_ticket."&noncestr=".$noncestr."×tamp=".$timestamp."&url=".$url."";  
    $signature = sha1($and);  
    $this->assign(signatrue , $signature);  
      
}  
  
//=======================================================================================================================================  
  
/** 
 * 构造获取用户信息所需要的code,的链接 
 * $jump_url:为用户授权后的跳转地址,可用get方式获取code,且只能使用一次 
 * $scope:为应用授权作用域,snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid), 
 * snsapi_userinfo: (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且,即使在未关注的情况下,只要用户授权,也能获取其信息) 
 */  
function construct_getcode_url( $appid, $jump_url, $scope="snsapi_userinfo" ) {  
    $jump_u = urlencode( $jump_url );  
    $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $appid .   
    "&redirect_uri=" . $jump_url . "&response_type=code&scope=" . $scope . "&state=1#wechat_redirect";  
    return $url;  
}  
  
//=======================================================================================================================================  
  
/** 
 * 获取网页授权access_token 
 * 所需要的参数必须有用户授权后返回的code值 
 * 返回值为array类型 
 * 包括:access_token、expires_in、refresh_token、scope、unionid 
 */  
function get_access_token_web( $appid, $appsecret, $code ) {  
    $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=". $appid ."&secret=". $appsecret ."&code=". $code ."CODE&grant_type=authorization_code";  
    $output = https_request( $url );  
    $jsoninfo = json_decode($output, true);  
    return $jsoninfo;  
}  
  
//=======================================================================================================================================  
  
/** 
 * 检验access_token是否有效 
 * access_token为网页授权接口凭证 
 * openid为用户ID 
 */  
function check_access_token( $access_token , $openid ) {  
    $url = "https://api.weixin.qq.com/sns/auth?access_token=". $access_token ."&openid=". $openid ;  
    $output = https_request( $url );  
    $jsoninfo = json_decode($output, true);  
    if( $jsoninfo[errcode] == 0 ) {  
        return true;  
    } else {  
        return false;  
    }  
}  
  
//=======================================================================================================================================  
  
/** 
 * 数据请求 
 * @return string $output 返回的数据,未解码 
 */  
function https_request($url, $data = null){  
    $curl = curl_init();  
    curl_setopt($curl, CURLOPT_URL, $url);  
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);  
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);  
    if (!empty($data)){  
        curl_setopt($curl, CURLOPT_POST, 1);  
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);  
    }  
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  
    $output = curl_exec($curl);  
    curl_close($curl);  
    return $output;  
}  
  
//=======================================================================================================================================  
  
/** 
 *  此接口唯一凭证access_token为基础凭证 
 *  返回的信息全面 
 */  
function get_user_info( $access_token, $openid ) {  
    $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=". $access_token ."&openid=". $openid ."&lang=zh_CN";  
    $output = https_request( $url );  
    $jsoninfo = json_decode($output, true);  
    return jsoninfo;  
}  
  
//========================================================================================================================================  
  
/** 
 *  此接口为用公众号接口唯一凭证access_token和用户openid获取用户信息,包括订阅、头像、昵称等详细信息 
 *  @param string $access_token :公众号接口唯一凭证 
 *  @param string $openid :用户openid 
 *  @return array :用户详细信息 
 */  
function get_user_if( $access_token, $openid ) {  
    $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=". $access_token ."&openid=" . $openid . "&lang=zh_CN";  
    $output = https_request( $url );  
    $jsoninfo = json_decode($output, true);  
    return $jsoninfo;   
}  
  
//========================================================================================================================================  
  
/** 
 * 菜单类处理 (***未完善***) 
 * @param access_token:公众号基础接口 
 * @param action:操作:0:创建;1:查询;2:删除;3:获取菜单配置接口 
 * @param data:菜单处理json数据 
 * @return info:处理结果 
 */  
function menu_handle( $access_token, $action, $data ) {  
    switch ($action) {  
        case 0:  
            $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" . $access_token;  
            //post方式提交  
            $ch = curl_init();  
            curl_setopt($ch, CURLOPT_URL, $url);  
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");  
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);  
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);  
            curl_setopt($ch, CURLOPT_USERAGENT, Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0));  
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  
            curl_setopt($ch, CURLOPT_AUTOREFERER, 1);  
            //带上数据  
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
            //反馈数据  
            $info = curl_exec($ch);  
            if (curl_errno($ch)) {  
                echo Errno.curl_error($ch);  
            }  
            curl_close($ch);  
            break;  
        case 1:  
            $url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=" . $access_token;  
            $info = https_request( $url );  
            break;  
        case 2:  
            $url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" . $access_token;  
            $info = https_request( $url );  
            break;  
        case 3:  
            $url = "https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token=" . $access_token;  
            $info = https_request( $url );  
            break;  
    }  
    return $info;  
      
}  

 

php获取微信用户信息(没测试过)

标签:wechat   exp   inf   time()   用户信息   使用   type   订阅   pen   

原文地址:http://www.cnblogs.com/weiyiyong/p/7324480.html

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