getQRCode.php
<?php
/**
* @Author: yousun
* @Date: 2018-09-29 13:48:41
* @Last Modified by: yousun
* @Last Modified time: 2018-09-29 14:52:36
*/
header("content-type:image/jpeg");
define("appid","wx4564ada184e726cf");
define("secrect","93e822bf6bfc606c1a41a3fc96f46cd4");
define("token","libinailisha");
require "./wechat.inc.php";
$wechat=new WeChat();
echo $wechat->_getQRCode();
wechat.inc.php
<?php
/**
* @Author: yousun
* @Date: 2018-09-29 11:27:18
* @Last Modified by: yousun
* @Last Modified time: 2018-09-29 14:46:48
*/
class WeChat{
private $_appid;
private $_secrect;
private $_token;
function _construnct($appid,$secrect,$token){
$this->_appid=$appid;
$this->_secrect=$secrect;
$this->_token=$token;
}
/**
* 连接请求微信服务器
* @var $curl 连接的地址
* @var $https=true,是否支持https协议,默认支持
* @var $method="get" 默认请求方式 get
* @var $data 请求方发来的数据
*/
private function _request($curl,$https=true,$method='get',$data=null){
$ch=curl_init();//初始化curl
curl_setopt($ch,CURLOPT_URL,$curl);//设置访问url,这里用https还不能访问
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);
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=$this->_request("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->_appid."&secret=".$this->_secrect);
file_put_contents($file, $content);
$content=json_decode($content);
return $content->access_token;
}
/**
* 生成二维码ticket
* @param integer $expires_seconds 二维有效期
* @param string $type 二维码类型临时还是永久
* @param integer $scene 场景,不同的服务不同的编号
* @return [type] json
*/
public function _getTicket($expires_seconds=604800,$type="temp",$scene=1){
if ($type=="temp") {//临时二维码
$data='{"expire_seconds": '.$expires_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());
}
}
public function _getQRCode($expires_seconds,$type,$scene){
//获取票据json
$content=$this->_getTicket($expires_seconds,$type,$scene);
//json解码
$content=json_decode($content);
//获取ticket票据
$ticket=$content->ticket;
//获取二维码
return $this->_request("https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".$ticket);
}
}
链接:https://pan.baidu.com/s/12KmIgPNOVDjaTJ-fWtyeVQ
提取码:g9pv