爱玺玺

爱玺玺的生活日记本。wx:lb87626

微信公众号开发获取二维码ticket票据

getticket.php

<?php

/**

 * @Author: yousun

 * @Date:   2018-09-29 13:48:41

 * @Last Modified by:   yousun

 * @Last Modified time: 2018-09-29 14:06:21

 */

define("appid","wx4564ada184e726cf");

define("secrect","93e822bf6bfc606c1a41a3fc96f46cd4");

define("token","libinailisha");

require "./wechat.inc.php";

$wechat=new WeChat();

echo $wechat->_getTicket(604800,"temp",8);


wechat.inc.php

<?php

/**

 * @Author: yousun

 * @Date:   2018-09-29 11:27:18

 * @Last Modified by:   yousun

 * @Last Modified time: 2018-09-29 14:07:56

 */

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()); 

}

}


将返回一个json数据:

{"ticket":"gQEz8TwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyY2JtSGtyN1FjN0QxQTRrVXhyMU4AAgSEGa9bAwSAOgkA","expire_seconds":604800,"url":"http:\/\/weixin.qq.com\/q\/02cbmHkr7Qc7D1A4kUxr1N"}

发表评论:

Powered By Z-BlogPHP 1.4 Deeplue Build 150101

Copyright Your WebSite.Some Rights Reserved.

蜀ICP备11021721号-5