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

然后就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代码如下

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);
if(!empty($json)){
$userinfo = $wechat->get_user_info($json['access_token'],$json['openid']);
return $userinfo;
}
} }else{
// $url = 'http://'.$_SERVER['HTTP_HOST'].'/activity/childrenday/index.php?pid=x';
$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里的这个方法好好体会下。

微信网页授权demo1的更多相关文章

  1. 微信网页授权snsapi_base、snsapi_userinfo的问题

    微信网页授权SCOPE分为snsapi_base.snsapi_userinfo,前者是用户无感知的静默授权只能拿到openid:而后者需要用户确认,能拿到更多的用户信息. 我有一个系统需要进行网页授 ...

  2. 玩玩微信公众号Java版之六:微信网页授权

    我们经常会访问一些网站,用微信登录的时候需要用到授权,那么微信网页授权是怎么一回事呢,一起来看看吧!   参考官方文档:https://mp.weixin.qq.com/wiki?t=resource ...

  3. 微信网页授权封装接口——node.js版

    Wechat 网页授权 授权url:(请在微信客户端中打开此链接体验) xxx为config.js中的WECHAT_DOMAIN 1.scope为snsapi_base xxx/?route=auth ...

  4. C# 微信网页授权多域名解决

    在做微信开发的时候,会遇到这样的场景:一个公众号,会有多个业务:官网.论坛.商城等等 微信网页授权域名 目前最多可以填写两个!!!,那么问题来了?这应该怎么办? 答案就是: 做一个中转服务! 域名1: ...

  5. php实现微信网页授权回调代理

    一个简单的php文件,实现微信网页授权回调域名的代理转发  <?php function is_HTTPS() { if (!isset($_SERVER['HTTPS'])) return F ...

  6. php 微信登录 公众号 获取用户信息 微信网页授权

    php 微信登录 公众号 获取用户信息 微信网页授权 先自己建立两个文件: index.php  和  getUserInfo.php index.php <?php //scope=snsap ...

  7. 服务号使用微信网页授权(H5应用等)

    获取授权准备 AppId 服务号已经认证且获取到响应接口权限 设置网页授权域名 公众号设置 - 功能设置 - 网页授权域名.注意事项: 回调页面域名或路径需使用字母.数字及"-"的 ...

  8. 微信网页授权,错误40163,ios正确,安卓错误?

    2017-07-29:结贴昨天研究了半天,也没解决,看到出错的http头里面有PHPSESSID,回头去修改了一下程序里的session部分的代码(这部分代码在微信网页授权之后),,也不知道是腾讯那边 ...

  9. Java微信公众平台开发(十六)--微信网页授权(OAuth2.0授权)获取用户基本信息

    转自:http://www.cuiyongzhi.com/post/78.html 好长时间没有写文章了,主要是最近的工作和生活上的事情比较多而且繁琐,其实到现在我依然还是感觉有些迷茫,最后还是决定静 ...

随机推荐

  1. Jeecg-Boot前后端分离,针对敏感数据,加密传递方案

    # 针对敏感数据,加密传递方案 第一步: 在vue页面引入aesEncrypt.js encryption方法.示例代码: import { encryption } from '@/utils/en ...

  2. google移动版针对智能手机、非智能手机的蜘蛛的User-agent

    非智能手机蜘蛛的User-agent有以下两个 SAMSUNG-SGH-E250/1.0 Profile/MIDP-2.0 Configuration/CLDC-1.1 UP.Browser/6.2. ...

  3. <每日一题>题目29:五个数字能组成多少互不重复的四位数

    #有五个数字:1.2.3.4.5,能组成多少个互不相同且无重复数字的四位数?各是多少? e =[] for a in range(1,6): for b in range(1,6): for c in ...

  4. elasticsearch 中文API(二)

    客户端 有多个地方需要使用Java client: 在存在的集群中执行标准的index, get, delete和search 在集群中执行管理任务 当你要运行嵌套在你的应用程序中的Elasticse ...

  5. 2018-8-10-docfx-做一个和微软一样的文档平台

    title author date CreateTime categories docfx 做一个和微软一样的文档平台 lindexi 2018-08-10 19:16:51 +0800 2018-2 ...

  6. 洛谷P3749 [六省联考2017]寿司餐厅

    传送门 题解 这几道都是上周llj讲的题,题解也写得十分好了,所以直接贴了几个链接和代码. //Achen #include<algorithm> #include<iostream ...

  7. 在ubuntu中安装phpstorm

    安装参考网址 https://blog.csdn.net/mrgong_/article/details/77200225 注意:不需要安装java相关 激活phpstorm 2018 参考网址:ht ...

  8. Python技巧—list与字符串互相转换

    Python技巧-list与字符串互相转换 在Python的编程中,经常会涉及到字符串与list之间的转换问题,下面就将两者之间的转换做一个梳理. 1.list转换成字符串 命令:list() 例子: ...

  9. 3377加减乘除等于24(原生js实现)

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  10. 013- unittest单元测试框架

    unittest单元测试框架 重要的概念 1. TestCase TestCase 是最小的测试单元,用于检查特定输入集合的特定返回值.unittest提供了TestCase基类,我们创建的测试类需要 ...