创建二维码ticket
public getQRCode ( integer | string $scene_id, integer $type, integer $expire = 604800 ) : array('ticket'=>'qrcode字串','expire_seconds'=>604800,'url'=>'二维码图片解析后的地址') | ||
$scene_id | integer | string | 自定义追踪id,临时二维码只能用数值型 |
$type | integer | 0:临时二维码;1:数值型永久二维码(此时expire参数无效);2:字符串型永久二维码(此时expire参数无效) |
$expire | integer | 临时二维码有效期,最大为604800秒 |
return | array('ticket'=>'qrcode字串','expire_seconds'=>604800,'url'=>'二维码图片解析后的地址') |
<?php require_once '../config.php'; require_once '../db.php'; require_once '../wechat.class.php'; $options = array('token' => WX_TOKEN, 'appid' => WX_APPID, 'appsecret' => WX_SECRET, 'debug' => false, 'logcallback' => 'logdebug'); $weObj = new Wechat($options); $weObj->checkAuth(); $ret = $weObj->getQRCode($_GET['id'], 1); $url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" . urlencode($ret['ticket']); echo "<img src='{$url}' /><a href='{$url}'>View in new page</a>";
<?php /** * 微信公共接口测试 * */ include "../wechat.class.php"; function logdebug($text) { file_put_contents('../data/log.txt', $text . "\n", FILE_APPEND); } $options = array('token' => 'tokenaccesskey', 'debug' => true, 'logcallback' => 'logdebug'); $weObj = new Wechat($options); // check null $scene_id $qrcode = $weObj->getQRCode(); if ($qrcode != false) { echo "test failed.\n"; die; } // check bad $type $qrcode = $weObj->getQRCode(123, -1); if ($qrcode != false) { echo "test failed.\n"; die; } // check bad $type $qrcode = $weObj->getQRCode(123, 5); if ($qrcode != false) { echo "test failed.\n"; die; }
$error1 = array("status" => "101", "des" => "缺少@get参数Numbe stationId"); echo json_encode($error1); exit; } if (isset($_GET['expire'])) { $expire = $_GET['expire']; } else { $expire = 60; } $getStationUrl = $apiUrl . "/getStation.php?stationId=" . $stationId; $getStation = json_decode(http_get($getStationUrl)); if ($getStation->status == 0) { $w = new Wechat($options); for ($i = 0; $i < 5; $i++) { $j = $i + 1; $ticket[$i] = $w->getQRCode($stationId, 0, $expire * $j); $qrcodeUrl[$i] = $w->getQRUrl($ticket[$i]['ticket']); $stationInfo[$i] = array('stationId' => $stationId, 'name' => $getStation->info->name, 'number' => $getStation->info->number, 'qrcodeUrl' => $qrcodeUrl[$i], 'expire_seconds' => $ticket[$i]['expire_seconds']); } $result = array('status' => 0, 'info' => $stationInfo); echo json_encode($result); } else { echo json_encode($getStation); exit; } /** * GET 请求 * @param string $url */ function http_get($url) {
} if ($res['ticket'] != 'authorized') { $bc = new Browscap(); $client_type = $bc->get_type(); if (!$fromUserName && $client_type != Browscap::MOBILE) { include_once WEIXIN_PATH . '/class/wechat.class.php'; $options = array('token' => WECHAT_TOKEN, 'appid' => WECHAT_APP_ID, 'appsecret' => WECHAT_APP_SECRET); $weObj = new Wechat($options); $sql = "select `Mac_ID`, `scene_id`, `created_at`,\n `updated_at`, `ticket`\n from " . WEIXIN_TABLE . "\n where `Mac_ID` = '{$Mac_ID}'\n and `ticket` != 'authorized'\n limit 1"; $result = $mysql::query($sql, 'all'); $expire = 500; if (!is_array($result) || count($result) < 0) { $sql = "select `scene_id` from " . WEIXIN_TABLE . " order by id desc limit 1"; $scene_id = $mysql::query($sql, '1'); $scene_id = $scene_id % 9999 + 1; $qrcode = $weObj->getQRCode($scene_id, $type = 0, $expire); $ticket = $qrcode['ticket']; $sql = "insert into " . WEIXIN_TABLE . " (`Mac_ID`, `ticket`, `scene_id`, `site`)\n values ('{$Mac_ID}', '{$ticket}', '{$scene_id}', '{$site}')"; } else { $created_at = $result[0]['created_at']; $updated_at = $result[0]['updated_at']; $now = time(); if ($now - strtotime($created_at) > $expire && $now - strtotime($updated_at) > $expire) { $qrcode = $weObj->getQRCode($result[0]['scene_id'], $type = 0, $expire); $ticket = $qrcode['ticket']; $updated_at = date("Y-m-d H:i:s"); $sql = "update " . WEIXIN_TABLE . " set `ticket` = '{$ticket}', `updated_at` = '{$updated_at}' where `Mac_ID` = '{$Mac_ID}'"; } else { $ticket = $result[0]['ticket']; } }
<?php require_once '../config.php'; require_once '../db.php'; require_once '../wechat.class.php'; $options = array('token' => WX_TOKEN, 'appid' => WX_APPID, 'appsecret' => WX_SECRET, 'debug' => false, 'logcallback' => 'logdebug'); $weObj = new Wechat($options); $weObj->checkAuth(); $ret = $weObj->getQRCode(intval($_GET['id']), 1); $url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" . urlencode($ret['ticket']); echo "<img src='{$url}' /><a href='{$url}'>View in new page</a>";