生成带参数的二维码”,通过这儿生成的二维码,只要通过微信扫一扫之后,会把事件自动推送到微信公众号上
用微信扫一扫推送到开发者网址那儿的数据如下:
<ToUserName>gh_eb6940eed***</ToUserName> <FromUserName>ofRsxuGNUpSuCBQVEF6YmbQ2y7dg</FromUserName> <CreateTime>1445757243</CreateTime> <MsgType>event</MsgType> <Event>SCAN</Event> <EventKey>123</EventKey> <Ticket>gQH57zoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL3JrT0FPeG5tNjdhU3c5ZWpzRzI1AAIEAH8sVgMEgDoJAA==</Ticket>
可以看出是一个xml格式的字符串,Event里SCAN表示扫描,EventKey是生成二维码时在json设置的数值,EventKey是可以自己指定的,其它都是微信平台内定的。。。通过自定义EventKey的数据,可以做到自动回复指定的数据
更多的应用场景请自由发挥,比如:扫一扫,领取红包等功能也是可以实现的,还有微信签到等等,好了不多废话,之后我会主要讲下微信扫码自动登录网站!类似58同城的微信登录
http://passport.58.com
下面说下这部分流程:
1.获取access_token 这个没有什么可说的 需要注意的是 access_token 全局缓存下 代码里面有不多说
2.创建二维码ticket
http请求方式: POST
URL: https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKEN POST数据格式:json
POST数据例子:{"expire_seconds": 604800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": 123}}}
正确的Json返回结果:
{"ticket":"gQH47joAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL2taZ2Z3TVRtNzJXV1Brb3ZhYmJJAAIEZ23sUwMEmm3sUw==","expire_seconds":60,"url":"http:\/\/weixin.qq.com\/q\/kZgfwMTm72WWPkovabbI"}
3.
获取二维码ticket后,开发者可用ticket换取二维码图片。请注意,本接口无须登录态即可调用。
请求说明
HTTP GET请求(请使用https协议) https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=TICKET 提醒:TICKET记得进行UrlEncode
全部代码两个文件 step1.php function.php :
step1.php :
<?php
include("function.php");
define("APPID","wx3f839ee5ed734***");
define("APPSECRET","1c9f66fa8d394bd410a821a91c47be3b");
//获取accesstoken
$access_token=getAccessToken(APPID,APPSECRET);
//获取二维码ticket
$url="https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=$access_token";
$json='{"expire_seconds": 604800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": 123}}}';
$return=post_json($url,$json);
$arr=json_decode($return,1);
$ticket=urlencode($arr['ticket']);
//echo $ticket;
?>
<meta charset="utf-8">
<div style="width:500px;margin:0 auto;">
<div>请使用微信扫描二维码登陆</div>
<img src="https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=<?php echo $ticket?>" style="width:200px;height:200px;" >
<div>www.wlphp.com版权所有</div>
</div>
function.php:
<?php
function getAccessToken($appId,$appSecret) {
// access_token 应该全局存储与更新,以下代码以写入到文件中做示例
$data = json_decode(file_get_contents("access_token.json"));
if ($data->expire_time < time()) {
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appId&secret=$appSecret";
//$res = json_decode(httpGet($url)); //改用file_get_contents 注意需要环境支持 谢谢
$res = json_decode(file_get_contents($url));
$access_token = $res->access_token;
if ($access_token) {
$data->expire_time = time() + 7000;
$data->access_token = $access_token;
$fp = fopen("access_token.json", "w");
fwrite($fp, json_encode($data));
fclose($fp);
}
} else {
$access_token = $data->access_token;
}
return $access_token;
}
// 工具函数 //
/* 使用curl来post一个json数据 */
// CURLOPT_SSL_VERIFYPEER,CURLOPT_SSL_VERIFYHOST - 在做https中要用到
// CURLOPT_RETURNTRANSFER - 不以文件流返回,带1
function post_json($url,$json){
$post_url = $url;
$post_data =$json;
$ch = curl_init();//初始化
curl_setopt($ch, CURLOPT_TIMEOUT, '30');//超时时间
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Keep-Alive: 300','Connection: keep-alive')) ;
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; GTB7.4; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2)');
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_URL,$post_url);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec($ch);
if(curl_errno($ch)){}
return $contents;
}