首页 > 其他分享 >微信接口开发之高级篇系列【网页授权接口】

微信接口开发之高级篇系列【网页授权接口】

时间:2023-04-05 20:33:31浏览次数:43  
标签:ch 网页 微信 接口 access token code appid curl


【1】带微信帐号的手机



 



【2】打开浏览器,这里以IE为例。



         输入:http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login




【3】用手机登录你的微信,使用微信中的“扫一扫”功能,扫描上面网页中的二维码。在手机上会出现以下界面:




【3】网页授权获取用户基本信息





注意:



这里的填写只要域名就可以里,不要http和以及域名下面的方法哦!



【4】然后在该域名下面定义一个控制器,添加方法即可



【5】方法一:跳转获取Code【需要微信登陆或者扫描的页面】



需要的参数:



(这里需要urlencode编码)



案例代码:



这里的:Scope为snsapi_base



/*
 *  定向的跳转,为了获取Code
 */
public function getcodeAction(){
    $this->view->disable();
$appid = 'wx94c43716d8a91f3f';
$redirect_uri = urlencode('http://ford4s.amailive.com/redis/getaccesstoken');

$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_base&state=1234#wechat_redirect";
    header('location:'.$url);
}



【6】方法二:这个方法也就是第一个方法要跳转的回调函数,这个方法名就在方法一的URL地址中:



需要跳转的方法:




实例代码:



public function getaccesstokenAction(){
    $this->view->disable();
    $appid = 'wx94c43716d8a91f3f';
    $appsecret = 'd4624c36b6795d1d99dcf0547af5443d';
    /*回调的时候自带的这个参数*/
    $code = $_GET['code'];

    $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
    $data = curl_exec($ch);
    curl_close($ch);
    /*这里接收到的data数据是Json格式的,我在这转换成数组了*/
    $result = json_decode($data,true);
    /*取出数组中的access_token这个值*/
    $access_token = $result['access_token'];
    $expires_in = $result['expires_in'];
    /*拿到Openid就知道是哪个用户了,例如:参加活动次数,统计量的统计,没参加一下就写一次,在这里可以写入数据库*/
    $openid = $result['openid'];
    echo $openid;
}


http://ford4s.amailive.com/redis/getcode【5】把域名和方法一生成一个二维码测试:



 




 



【6】返回信息:



{ "access_token":"ACCESS_TOKEN", "expires_in":7200, "refresh_token":"REFRESH_TOKEN", "openid":"OPENID", "scope":"SCOPE" }



【7】获取用户信息:



(1)方法一:用户登陆或者扫描的方法



Scope为snsapi_userinfo



public function getcodeAction(){
    $this->view->disable();
$appid = 'wx94c43716d8a91f3f';

    /*基本授权 方法跳转地址*/
$redirect_uri = urlencode('http://ford4s.amailive.com/redis/getuserinfo');

    /*高级授权 snsapi_userinfo*/
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_userinfo&state=1234#wechat_redirect";
    header('location:'.$url);
}



(2)方法二:获取用户详细信息,【这个方法是在第一个跳转页面的时候加载域名后面的那个方法哦!】



public function getUserInfoAction(){
    $this->view->disable();
    $appid = 'wx94c43716d8a91f3f';
    $appsecret = 'd4624c36b6795d1d99dcf0547af5443d';
    /*回调的时候自带的这个参数*/
    $code = $_GET['code'];

    $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
    $data = curl_exec($ch);
    curl_close($ch);

    $result = json_decode($data,true);
    /*取出数组中的access_token这个值*/
    $access_token = $result['access_token'];
    $openid = $result['openid'];
    $URL2 = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$URL2);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
    $info = curl_exec($ch);
    curl_close($ch);
    var_dump($info);
}



(3)打印结果:




(4)单独获取access_token方法:



参数说明:



     公众号可以使用AppID和AppSecret调用本接口来获取access_token



案列代码:



/* AppID和AppSecret调用本接口来获取access_token */
public function getaccesstokenAction(){
    $this->view->disable();
    $appid = 'wx94c43716d8a91f3f';
    $appsecret = 'd4624c36b6795d1d99dcf0547af5443d';

    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret."";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
    $data = curl_exec($ch);
    curl_close($ch);
    /*这里接收到的data数据是Json格式的,我在这转换成数组了*/
    $result = json_decode($data,true);
    /*取出数组中的access_token这个值*/
    $access_token = $result['access_token'];
    return $access_token;
}






 



 



 



 



 



 



 









标签:ch,网页,微信,接口,access,token,code,appid,curl
From: https://blog.51cto.com/tinywan/6171619

相关文章

  • 微信小程序入门教程(一)API接口数据记录
    今天测试用小程序调用API接口,发现有些数据打印都是对象,怎么全部打印详细点来小程序代码:httpsearch:function(name,offset,type,cb){wx.request({url:'https://www.tinywan.com/api/wechat/songsSearch',data:{name:name,offset:o......
  • 你真的需要一个微信公众号吗?
    阅读本文大概需要4分钟。      最近看到很多人后台问我各种开通个人公众号的问题,如怎么开通原创功能啊,怎么开通赞赏功能啊,可能读者中也有很大一部分人已经或者将要开通自己的微信公众号,这篇文章姑且我就分享下我个人运营公号的一些宝贵经验,给你们做些参考,相信看完你......
  • 接口安全处理
    一、为什么要保证接口安全在我们日常开发中,存在一些接口是敏感且重要的,比如充值接口,如果在你调用充值接口的时候被别人抓包了,然后就可以修改充值的金额,本来充值10元可以改成充值10w,产生重大生产问题,再或者说被被人抓包了,别人可以不限制的调用该充值10元的接口,调用几万次......
  • C++库封装JNI接口——实现java调用c++
    1.JNI原理概述通常为了更加灵活高效地实现计算逻辑,我们一般使用C/C++实现,编译为动态库,并为其设置C接口和C++接口。用C++实现的一个库其实是一个或多个类的简单编译链接产物。然后暴露其实现类构造方法和纯虚接口类。这样就可以通过多态调用到库内部的实现类及其成员方法。进一步......
  • OpenJDK源码研究笔记(十):枚举的高级用法,枚举实现接口,竟是别有洞天
    在研究OpenJDK,Java编译器javac源码的过程中,发现以下代码。顿时发现枚举类竟然也有如此“高端大气上档次”的用法。沙场点兵(用法源码)com.sun.tools.javac.file.JavacFileManager.SortFilesprotectedenumSortFilesimplementsComparator<File>{FORWARD{......
  • Jenkins持续集成,接口测试报告发送企业微信
    一、前置条件已经安装Jenkins(361.1)已经安装jdk(jdk17)Jdk和Jenkins版本相互兼容二、启动Jenkins(注意不要关闭dos窗口)切换到Jenkins目录,输入命令:java-jarjenkins.war        2.dos窗口出现“Jenkinsisfullyupandrunning”表示启动成功   ......
  • 接口收藏哦
    http://c.m.163.com/nc/article/headline/T1348647853363/0-40.html头条http://c.3g.163.com/nc/article/list/T1467284926140/0-20.html精选http://c.3g.163.com/nc/article/list/T1348648517839/0-20.html娱乐http://c.m.163.com/nc/auto/list/5bmz6aG25bGx/0-20.html汽车......
  • 知乎微信接口
    微信精选段子http://v.juhe.cn/weixin/query?key=d046cd1f569ed13d951f0258902ef9b2&ps=10知乎最新日报列表http://news-at.zhihu.com/api/4/news/latest知乎详情http://news-at.zhihu.com/api/4//news/{KaTeXparseerror:Expected'EOF',got'}'atposition3:......
  • 微信支付——微信退款+提现
    未封装的退款<?php//+----------------------------------------------------------------------//|Tplay[WEONLYDOWHATISNECESSARY]//+----------------------------------------------------------------------//|Copyright(c)2017http://tplay.pengyiche......
  • 微信证书
    windows版本:https://wx.gtimg.com/mch/files/WXCertUtil.exemac版本:https://wx.gtimg.com/mch/files/WXCertUtil.dmghttps://kf.qq.com/faq/161222NneAJf161222U7fARv.html......