您好,欢迎访问一九零五行业门户网

PHP实现微信网页授权开发教程,php授权教程_PHP教程

php实现微信网页授权开发教程,php授权教程微信网页授权是服务号才有的高级功能,开发者可以通过授权后获取用户的基本信息;在此之前,想要获取消息信息只能在用户和公众号交互时根据openid获取用户信息;而微信网页授权可在不需要消息交互,也不需要关注的情况下获取用户的基本信息。
微信网页授权时通过oauth2.0完成的,整个过程分为三步:
用户授权,获取code; 根据code获取access_token【可通过refresh_token刷新获取较长有效期】 通过access_token和openid获取用户信息
对微信网页授权过程做了简单封装:
app_id}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_userinfo&state={$state}#wechat_redirect; } /** * 获取授权token * * @param string $code 通过get_authorize_url获取到的code */ public function get_access_token($app_id = '', $app_secret = '', $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, $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); } }
以上就是本文的全部内容,希望对大家的学习有所帮助。
您可能感兴趣的文章:php 程序授权验证开发思路新浪微博api开发简介之用户授权(php基础篇)php使用cookie控制访问授权的方法php实现paypal 授权登录weiphp微信公众平台授权设置
http://www.bkjia.com/phpjc/1093698.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/1093698.htmltecharticlephp实现微信网页授权开发教程,php授权教程 微信网页授权是服务号才有的高级功能,开发者可以通过授权后获取用户的基本信息;在此之前...
其它类似信息

推荐信息