微信获取ticket及生成二维码(临时或永久)

curl.php----

  1. define("APPID","");
  2. define("APPSECRET","");
  3. define("TOKEN","weixin");
  4. require("./wechat.inc.php");
  5. header("content-type:image/jpg");
  6. $wechat=new WeChat(APPID,APPSECRET,TOKEN);
  7. echo $wechat->_getQRCode();
  1. wechat.inc.php------
  1. class WeChat{
  2. private $_appid;
  3. private $_appsecret;
  4. private $_token;
  5.  
  6. public function __construct($appid,$appsecret,$token)
  7. {
  8. $this->_appid=$appid;
  9. $this->_appsecret=$appsecret;
  10. $this->_token=$token;
  11. }
  12. private function _request($curl,$https=true,$method='get',$data=null)
  13. {
  14. $ch=curl_init(); //初始化
  15. curl_setopt($ch,CURLOPT_URL,$curl);
  16. curl_setopt($ch,CURLOPT_HEADER,false);//设置不需要头信息
  17. curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);//获取页面内容,但不输出
  18. if($https)
  19. {
  20. curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);//不做服务器认证
  21. curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);//不做客户端认证
  22. }
  23.  
  24. if($method=='post')
  25. {
  26. curl_setopt($ch, CURLOPT_POST,true);//设置请求是post方式
  27. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//设置post请求数据
  28.  
  29. }
  30.  
  31. $str=curl_exec($ch);//执行访问
  32. curl_close($ch);//关闭curl,释放资源
  33. return $str;
  34. }
  35.  
  36. private function _getAccesstoken()
  37. {
  38. $file="./accesstoken";
  39. if(file_exists($file))
  40. {
  41. $content=file_get_contents($file);
  42. $content=json_decode($content);
  43. if(time()-filemtime($file)<$content->expires_in)
  44. {
  45. return $content->access_token;
  46. }
  47. }
  48. $content=_request("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->_appid."&secret=".$this->_appsecret);
  49. file_put_contents($file,$content);
  50. $content=json_decode($content);
  51. return $content->access_token;
  52. }
  53.  
  54. /*获取ticket
  55. expire_seconds:二维码有效期(秒)
  56. type:二维码类型(临时或永久)
  57. scene:场景编号
  58. */
  59. public function _getTicket($expire_seconds=604800,$type="temp",$scene=1)
  60. {
  61. if($type=="temp"){
  62. $data='{"expire_seconds": '.$expire_seconds.', "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": '.$scene.'}}}';
  63. return $this->_request("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$this->_getAccesstoken(),true,"post",$data);
  64. }else
  65. {
  66. $data='{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": '.$scene.'}}}';
  67. return $this->_request("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$this->_getAccesstoken(),true,"post",$data);
  68. }
  69. }
  70. /* 通过ticket获取二维码 */
  71. public function _getQRCode($expire_seconds=604800,$type="temp",$scene=1)
  72. {
  73. $content=json_decode($this->_getTicket($expire_seconds,$type,$scene));
  74. $ticket=$content->ticket;
  75. $image=$this->_request("https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".urlencode($ticket));
  76. //$file="./".$type.$scene.".jpg"; //设置图片名字
  77. //file_put_contents($file,$content); //二维码保存到本地
  78. return $image;
  79.  
  80. }
  81.  
  82. }

微信获取ticket及生成二维码(临时或永久)的更多相关文章

  1. 微信支付支付宝支付生成二维码的方法(php生成二维码的三种方法)

    如果图简单,可以用在线生成 http://pan.baidu.com/share/qrcode?w=150&h=150&url=http://www.xinzhenkj.com 最简单 ...

  2. 微信小程序之生成二维码

    最近项目中涉及到小程序的生成二维码,很是头疼,经过多次摸索,整理出了自己的一些思想方法,如有不足,欢迎指正. 首先完全按照小程序的结构依次填坑. pages--index.wxml <view ...

  3. 微信小程序带参数生成二维码

    wx.request({ url: 'https://api.weixin.qq.com/cgi-bin/token', header: { 'content-type': 'application/ ...

  4. 使用qrcode输入信息生成二维码包含二维码说明信息,点击转化为图片并下载

    说明:输入汉字和数字都可以识别并展示 <body> <h2 id="h2">二维码生成</h2> <br> <span id= ...

  5. web网页外部分享到微信、朋友圈、扣扣、微博等功能、自动生成二维码等

    1.这里重中之重是分享到微信:web端网页通常是没有权限分享过去的 所以用: weixin://dl/business/?ticket=ta428dhj739hg3efe6e  但是这个ticket真 ...

  6. 微信蓝牙设备开发教程之获取设备deviceid和二维码(3)

    文章转载地址  http://www.vxzsk.com/87.html  设备授权 调用 设备授权新接口 ,获取deviceid和二维码,然后利用获取到的deviceid更新设备属性(如mac地址, ...

  7. 用c#开发微信(2)扫描二维码,用户授权后获取用户基本信息 (源码下载)

    本文将介绍基于Senparc.Weixin微信开发框架来实现网页授权来获取用户基本信息.先生成包含授权及回调url信息的二维码:用户用微信扫描之后,被要求授权以获取Ta的用户基本信息:用户授权后,通过 ...

  8. (转)ZXing生成二维码和带logo的二维码,模仿微信生成二维码效果

    场景:移动支付需要对二维码的生成与部署有所了解,掌握目前主流的二维码生成技术. 1 ZXing 生成二维码 首先说下,QRCode是日本人开发的,ZXing是google开发,barcode4j也是老 ...

  9. 微信小程序条码、二维码生成模块

    代码地址如下:http://www.demodashi.com/demo/13994.html 一.前期准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.co ...

随机推荐

  1. 【译】使用 Flutter 实现跨平台移动端开发

    作者: Mike Bluestein   | 原文地址:[https://www.smashingmagazine.com/2018/06/google-flutter-mobile-developm ...

  2. L1-Day12

    1.凡是杀不死你的都会让你变得更强.(什么关系?主语是什么?)[我的翻译]There is no killing you makes you stronger.[标准答案]What doesn’t k ...

  3. [工具开发] 分享两个基于Heapster 和 Influxdb 的 Grafana 监控仪表盘模板

    Info Collector: Heapster - /heapster- --metric-resolution=30s- --sink=influxdb:http://influxdb.defau ...

  4. mac 安装和使用MongoDB

    安装 尝试一:手动命令安装尝试二:采用Homebrew尝试三:下载安装包使用安装尝试一:手动命令安装按照官网https://docs.mongodb.com/manual/tutorial/insta ...

  5. 初学python之路-day07-数据类型总结

    数据类型的各种使用方法:#1.字符串类型s='abcdef's1=s[0]s2=s[-1]print(s1,s2) #h d 索引取值,正向,反向a = 10b = "20"c = ...

  6. MongoDB数据库(二):增删查改

    MongoDB数据库的增删查改 1.插入数据 语法: db.集合名称.insert(document) db.table_name.insert({name:'gj',gender:1}) db.ta ...

  7. element-ui对话框组件Dialog在回调事件opened获取组件滚动条scrollTop的问题

    今天使用element中的Dialog组件时发现一个问题:当Dialog内容过多时会出现滚动条,而当你滚动到一定位置后关闭Dialog,然后再次打开时滚动条仍然保持在上一次关闭前的位置而没有回到顶部. ...

  8. pip命令

    安装包 pip install django   #最新版本 pip install django==1.11.7 #指定版本 pip install django>=1.11.7 #最小版本 ...

  9. 使用Nexus搭建私有Nuget仓库

    前言 Nuget是ASP .NET Gallery的一员,是免费.开源的包管理工具,专注于在.Net / .Net Core应用开发过程中第三方组件库的管理,相对于传统单纯的dll引用要方便.科学得多 ...

  10. 开发自己的react-native组件并发布到npm[转]

    原文链接:https://www.jianshu.com/p/091a68ea1ca7 写在前面 在做react-native开发的时候,我们经常会找到一些第三方组件,并且通过npm install的 ...