夺命雷公狗—微信开发38—-临时二维码

我们要做一个临时的二维码,就类似超市的最后抢购30秒。

临时二维码手册的查找方式几乎差不了什么

技术分享

 

 

第一步都是需要先申请到一个ticket值,然后通过ticket值去换取二维码,temp_qrcode.php代码如下:

 

<?php
    header("Content-Type:text/html;charset=utf-8");
    require_once "get_token.php";
    require_once "common.php";
    //首先我们申请一个二维码的ticket
    //比如我们规定$scene_id值5000表示我们临时生成的二维码,有效时间为50秒
    //临时二维码,是有过期时间的,最长可以设置为在二维码生成后的30天(即2592000秒)后过期
    $scene_id = ‘500‘;
    //action_name 二维码类型,QR_SCENE为临时,QR_LIMIT_SCENE为永久,QR_LIMIT_STR_SCENE为永久的字符串参数值
    //action_info 二维码详细信息
    //scene_id 场景值ID,临时二维码时为32位非0整型,永久二维码时最大值为100000(目前参数只支持1--100000)
    //scene_str 场景值ID(字符串形式的ID),字符串类型,长度限制为1到64,仅永久二维码支持此字段
    //这下面的50代表50秒
    $post = ‘{"expire_seconds": 50, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": ‘.$scene_id.‘}}}‘;
    $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token={$access_token}";
    //发出http请求,申请ticket
    $res = http_request($url,$post);
    $str_json = json_decode($res);
    $ticket = $str_json->ticket;
    //通过ticket来换取二维码
    $ticket = urlencode($ticket);
    $url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket={$ticket}";
    $filee = file_get_contents($url);
    //把该二维码文件保存到空间里的rqcode文件夹内,如果是linux系统要要修改下文件权限 chown -R www:www ./rqcode/
    $file_name = "qrcode{$scene_id}.jpg";
    file_put_contents("./qrcode/".$file_name,$filee);
    $rqcode_url = "http://weixin.showtp.com/qrcode/{$file_name}";
?>
<!DOCTYPE html>
<html>
    <head>
        <title>生成临时性二维码</title>
    </head>
    <body>
        <h1>生成临时性二维码 50秒内扫描该二维码即可赠送美国充气娃娃</h1>
        <img src="<?php echo $rqcode_url; ?>" />
    </body>
</html>

 

 

index.php代码如下:

<?php
/**
  * wechat php test
  */

//define your token
require_once "common.php";
//这里是引入curl发送函数的类
require_once ‘WeChat.class.php‘;
define("TOKEN", "twgdh");

//这里让这个类继承了curl发送参数的类
class wechatCallbackapiTest extends WeChat
{
    public function valid()
    {
        $echoStr = $_GET["echostr"];

        //valid signature , option
        if($this->checkSignature()){
            echo $echoStr;
            exit;
        }
    }

    public function responseMsg()
    {
        //get post data, May be due to the different environments
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
        
          //extract post data
        if (!empty($postStr)){
                /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
                   the best way is to check the validity of xml by yourself */
                // 使用simplexml技术对xml进行解析 
                // libxml_disable_entity_loader(true), 是从安全性考虑,为了防止xml外部注入,
                //只对xml内部实体内容进行解析
                libxml_disable_entity_loader(true);
                //加载 postStr 字符串
                $postObj = simplexml_load_string($postStr, ‘SimpleXMLElement‘, LIBXML_NOCDATA);
                $fromUsername = $postObj->FromUserName;
                $toUsername = $postObj->ToUserName;
                $keyword = trim($postObj->Content);
                $time = time();
                global $tmp_arr;
                
                //当用户公众号有互动时,我们将会动hd_time字段更新
                $connect = mysql_connect(‘localhost‘,‘root‘,‘root‘) or die(‘数据库链接失败‘);
                mysql_select_db(‘wxdb‘,$connect);
                mysql_query(‘set names utf8‘);
                $sql = "update qf_users set hd_time=‘{$time}‘ where openid=‘{$fromUsername}‘";
                mysql_query($sql, $connect);
                
                //根据接收到的消息类型,来进行分支处理(switch)
                switch($postObj->MsgType)
                {
case ‘event‘:
    //用户未关注时,进行关注后的事件推送
    if($postObj->Event == ‘subscribe‘)
    {
        
        $contentStr = "欢迎关注leigood微信测试号噢";
        $resultStr = sprintf($tmp_arr[‘text‘], $fromUsername, $toUsername, $time, $contentStr);
        echo $resultStr; 
        //当用户订阅时,根据用户的扫描情况来保存该用户
        $connect = mysql_connect(‘localhost‘,‘root‘,‘root‘) or die(‘连接数据库失败‘);
        mysql_select_db(‘wxdb‘,$connect);
        mysql_query(‘set names utf8‘);
        //这里的EventKey是手册上告诉我们的,EventKey事件KEY值,qrscene_为前缀,后面为二维码的参数值
        //EventKey也就是我们的场景值
        $EventKey = $postObj->EventKey;
        $sql = "insert into qrcode_users (id,openid,scene_id) values(NULL,‘{$fromUsername}‘,‘{$EventKey}‘)";
        mysql_query($sql);
    //这下面的EventKey是来自temp_qrcode.php文件里面自己定义的$scene_id的值
    }else if($postObj->Event == ‘SCAN‘ && $postObj->EventKey == ‘500‘)
    {
        $contentStr = ‘恭喜您,您已经成功抢到我们美国菲尔兰州充气娃娃一份‘;
        $resultStr = sprintf($tmp_arr[‘text‘], $fromUsername, $toUsername, $time, $contentStr);
        echo $resultStr;
    }
    
    break;
case ‘text‘: //回复文本模块
    //这里开始测试客服消息接口了
    if(!empty($keyword)){
       //将用户提交的内容入库
       $connect = mysql_connect(‘localhost‘,‘root‘,‘root‘) or die(‘连接数据库失败‘);
       mysql_select_db(‘wxdb‘,$connect);
       mysql_query(‘set names utf8‘);
       $sql = "insert into zx_info (id,openid,zx_info) values(NULL,‘{$fromUsername}‘,‘{$keyword}‘)";
       mysql_query($sql);
       
       $contentStr = ‘您的咨询消息我们已经收到,稍后将会有我们的客服代表进行回复‘;
       $resultStr = sprintf($tmp_arr[‘text‘], $fromUsername, $toUsername, $time, $contentStr);
       echo $resultStr;
    }else{
        $contentStr = ‘您输入的格式有误‘;
        $resultStr = sprintf($tmp_arr[‘text‘], $fromUsername, $toUsername, $time, $contentStr);
        echo $resultStr;
    }
    break;
case ‘image‘: //处理用户上传图片
    $media_id = $postObj -> MediaId; //获取到用户上传的图片的mediaid
    $resultStr = sprintf($tmp_arr[‘image‘], $fromUsername, $toUsername, $time, $media_id);
    echo $resultStr;
    
    //将图片保存到本地服务器的文件系统
    //1.先给图片创建一个名字
    $image_file_name = time().‘.jpg‘;
    //2.获取该图片的内容
    $image_file = file_get_contents($postObj->PicUrl);
    //3.保存到本地服务器的文件系统
    //提醒:一定要保证您创建的文件夹是www用户可读可写,否则无法保存该图片到文件夹下
    file_put_contents("./uploadimage/".$image_file_name,$image_file);
    
    //将图片的路径和相关信息入库
    //1.创建一张表
    //2.链接mysql数据库,并且添加图片信息
    $connect = mysql_connect(‘localhost‘,‘root‘,‘root‘);
    mysql_select_db(‘wxdb‘,$connect);
    mysql_query(‘set names utf8‘);
    $media_path = "./uploadimage/".$image_file_name; //路径
    $sql = "insert into keep_image_uploads (id,openid,media_id,media_path) values(NULL,‘{$fromUsername}‘,‘{$media_id}‘,‘{$media_path}‘)";
    mysql_query($sql,$connect);
    break;
case ‘voice‘: //处理用户上传语言的业务逻辑
    $media_id = $postObj -> MediaId; //获取media_id的id号
    $resultStr = sprintf($tmp_arr[‘voice‘], $fromUsername, $toUsername, $time, $media_id);
    echo $resultStr;
    break;
case ‘location‘: //处理用户上传的地理位置信息
    $Location_X = $postObj -> Location_X; //获取上传地理位置的纬度
    $Location_Y = $postObj -> Location_Y; //获取上传地地理位置经度
    $contentStr = "您上报的地理位置是:n经度是:{$Location_Y} n纬度是: {$Location_X}";
    $resultStr = sprintf($tmp_arr[‘text‘], $fromUsername, $toUsername, $time, $contentStr);
    echo $resultStr;
    break;
case ‘link‘: //接收并回复链接信息
    //获取到用户上传的链接信息
    $Title = $postObj -> Title;
    $Url = $postObj -> Url;
    $contentStr = "<a href=‘{$Url}‘>{$Title}</a>";
    $resultStr = sprintf($tmp_arr[‘text‘], $fromUsername, $toUsername, $time, $contentStr);
    echo $resultStr;
    break;
    
                }
        }else {
            echo "";
            exit;
        }
    }
        
    private function checkSignature()
    {
        // you must define TOKEN by yourself
        if (!defined("TOKEN")) {
            throw new Exception(‘TOKEN is not defined!‘);
        }
        
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
                
        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        // use SORT_STRING rule
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );
        
        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }
}

//如果这段代码放在上面,那程序将会报错,因为继承的问题,会显示类没有找到
$wechatObj = new wechatCallbackapiTest();
//当接入成功后,请注销这句话,否则,会反复验证。
//$wechatObj->valid();
//添加响应请求的语句
$wechatObj->responseMsg();

?>

 

 

index.php核心代码如下:

技术分享

 

 

临时二维码重点提示以下,如果是分两次进行扫描,那么最后的效果就是他会在永久二维码的数据库里面增加一条数据,但重点还是看永久二维码里面的业务逻辑是如何写的了

ticket

 

夺命雷公狗---微信开发38----临时二维码

原文:http://www.cnblogs.com/leigood/p/5240767.html

以上是夺命雷公狗—微信开发38—-临时二维码的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>