微信获取ticket及生成二维码(临时或永久)
微信获取ticket及生成二维码(临时或永久)
curl.php----
- define("APPID","");
- define("APPSECRET","");
- define("TOKEN","weixin");
- require("./wechat.inc.php");
- header("content-type:image/jpg");
- $wechat=new WeChat(APPID,APPSECRET,TOKEN);
- echo $wechat->_getQRCode();
- wechat.inc.php------
- class WeChat{
- private $_appid;
- private $_appsecret;
- private $_token;
- public function __construct($appid,$appsecret,$token)
- {
- $this->_appid=$appid;
- $this->_appsecret=$appsecret;
- $this->_token=$token;
- }
- private function _request($curl,$https=true,$method='get',$data=null)
- {
- $ch=curl_init(); //初始化
- curl_setopt($ch,CURLOPT_URL,$curl);
- curl_setopt($ch,CURLOPT_HEADER,false);//设置不需要头信息
- curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);//获取页面内容,但不输出
- if($https)
- {
- curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);//不做服务器认证
- curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);//不做客户端认证
- }
- if($method=='post')
- {
- curl_setopt($ch, CURLOPT_POST,true);//设置请求是post方式
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//设置post请求数据
- }
- $str=curl_exec($ch);//执行访问
- curl_close($ch);//关闭curl,释放资源
- return $str;
- }
- private function _getAccesstoken()
- {
- $file="./accesstoken";
- if(file_exists($file))
- {
- $content=file_get_contents($file);
- $content=json_decode($content);
- if(time()-filemtime($file)<$content->expires_in)
- {
- return $content->access_token;
- }
- }
- $content=_request("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->_appid."&secret=".$this->_appsecret);
- file_put_contents($file,$content);
- $content=json_decode($content);
- return $content->access_token;
- }
- /*获取ticket
- expire_seconds:二维码有效期(秒)
- type:二维码类型(临时或永久)
- scene:场景编号
- */
- public function _getTicket($expire_seconds=604800,$type="temp",$scene=1)
- {
- if($type=="temp"){
- $data='{"expire_seconds": '.$expire_seconds.', "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": '.$scene.'}}}';
- return $this->_request("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$this->_getAccesstoken(),true,"post",$data);
- }else
- {
- $data='{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": '.$scene.'}}}';
- return $this->_request("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$this->_getAccesstoken(),true,"post",$data);
- }
- }
- /* 通过ticket获取二维码 */
- public function _getQRCode($expire_seconds=604800,$type="temp",$scene=1)
- {
- $content=json_decode($this->_getTicket($expire_seconds,$type,$scene));
- $ticket=$content->ticket;
- $image=$this->_request("https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".urlencode($ticket));
- //$file="./".$type.$scene.".jpg"; //设置图片名字
- //file_put_contents($file,$content); //二维码保存到本地
- return $image;
- }
- }
微信获取ticket及生成二维码(临时或永久)的更多相关文章
- 微信支付支付宝支付生成二维码的方法(php生成二维码的三种方法)
如果图简单,可以用在线生成 http://pan.baidu.com/share/qrcode?w=150&h=150&url=http://www.xinzhenkj.com 最简单 ...
- 微信小程序之生成二维码
最近项目中涉及到小程序的生成二维码,很是头疼,经过多次摸索,整理出了自己的一些思想方法,如有不足,欢迎指正. 首先完全按照小程序的结构依次填坑. pages--index.wxml <view ...
- 微信小程序带参数生成二维码
wx.request({ url: 'https://api.weixin.qq.com/cgi-bin/token', header: { 'content-type': 'application/ ...
- 使用qrcode输入信息生成二维码包含二维码说明信息,点击转化为图片并下载
说明:输入汉字和数字都可以识别并展示 <body> <h2 id="h2">二维码生成</h2> <br> <span id= ...
- web网页外部分享到微信、朋友圈、扣扣、微博等功能、自动生成二维码等
1.这里重中之重是分享到微信:web端网页通常是没有权限分享过去的 所以用: weixin://dl/business/?ticket=ta428dhj739hg3efe6e 但是这个ticket真 ...
- 微信蓝牙设备开发教程之获取设备deviceid和二维码(3)
文章转载地址 http://www.vxzsk.com/87.html 设备授权 调用 设备授权新接口 ,获取deviceid和二维码,然后利用获取到的deviceid更新设备属性(如mac地址, ...
- 用c#开发微信(2)扫描二维码,用户授权后获取用户基本信息 (源码下载)
本文将介绍基于Senparc.Weixin微信开发框架来实现网页授权来获取用户基本信息.先生成包含授权及回调url信息的二维码:用户用微信扫描之后,被要求授权以获取Ta的用户基本信息:用户授权后,通过 ...
- (转)ZXing生成二维码和带logo的二维码,模仿微信生成二维码效果
场景:移动支付需要对二维码的生成与部署有所了解,掌握目前主流的二维码生成技术. 1 ZXing 生成二维码 首先说下,QRCode是日本人开发的,ZXing是google开发,barcode4j也是老 ...
- 微信小程序条码、二维码生成模块
代码地址如下:http://www.demodashi.com/demo/13994.html 一.前期准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.co ...
随机推荐
- 【译】使用 Flutter 实现跨平台移动端开发
作者: Mike Bluestein | 原文地址:[https://www.smashingmagazine.com/2018/06/google-flutter-mobile-developm ...
- L1-Day12
1.凡是杀不死你的都会让你变得更强.(什么关系?主语是什么?)[我的翻译]There is no killing you makes you stronger.[标准答案]What doesn’t k ...
- [工具开发] 分享两个基于Heapster 和 Influxdb 的 Grafana 监控仪表盘模板
Info Collector: Heapster - /heapster- --metric-resolution=30s- --sink=influxdb:http://influxdb.defau ...
- mac 安装和使用MongoDB
安装 尝试一:手动命令安装尝试二:采用Homebrew尝试三:下载安装包使用安装尝试一:手动命令安装按照官网https://docs.mongodb.com/manual/tutorial/insta ...
- 初学python之路-day07-数据类型总结
数据类型的各种使用方法:#1.字符串类型s='abcdef's1=s[0]s2=s[-1]print(s1,s2) #h d 索引取值,正向,反向a = 10b = "20"c = ...
- MongoDB数据库(二):增删查改
MongoDB数据库的增删查改 1.插入数据 语法: db.集合名称.insert(document) db.table_name.insert({name:'gj',gender:1}) db.ta ...
- element-ui对话框组件Dialog在回调事件opened获取组件滚动条scrollTop的问题
今天使用element中的Dialog组件时发现一个问题:当Dialog内容过多时会出现滚动条,而当你滚动到一定位置后关闭Dialog,然后再次打开时滚动条仍然保持在上一次关闭前的位置而没有回到顶部. ...
- pip命令
安装包 pip install django #最新版本 pip install django==1.11.7 #指定版本 pip install django>=1.11.7 #最小版本 ...
- 使用Nexus搭建私有Nuget仓库
前言 Nuget是ASP .NET Gallery的一员,是免费.开源的包管理工具,专注于在.Net / .Net Core应用开发过程中第三方组件库的管理,相对于传统单纯的dll引用要方便.科学得多 ...
- 开发自己的react-native组件并发布到npm[转]
原文链接:https://www.jianshu.com/p/091a68ea1ca7 写在前面 在做react-native开发的时候,我们经常会找到一些第三方组件,并且通过npm install的 ...