因为Sham有计划在各区域放置二维码,用于同事们快速查询相关信息和提交反馈建议,就需要每个二维码都能单独对应该区域。
如果通过官方网站生成的话太麻烦,于是参照官方接口文档,用PHP做了一个,以便快速生成,代码如下:
<?php
header('content-type:text/html;charset=utf-8');
//配置APPID、APPSECRET
$APPID = "你的id";
$APPSECRET = "你的secret";
//获取access_token
$access_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$APPID&secret=$APPSECRET";
//缓存access_token,这个是为了避免重复多次提交导致超限后无法再次获取
session_start();
$_SESSION['access_token'] = "";
$_SESSION['expires_in'] = 0;
$ACCESS_TOKEN = "";
if(!isset($_SESSION['access_token']) || (isset($_SESSION['expires_in']) && time() > $_SESSION['expires_in']))
{
$json = httpRequest( $access_token );
$json = json_decode($json,true);
$_SESSION['access_token'] = $json['access_token'];
$_SESSION['expires_in'] = time()+7200;
$ACCESS_TOKEN = $json["access_token"];
}
else{
$ACCESS_TOKEN = $_SESSION["access_token"];
}
//构建请求二维码参数
$qcode ="https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=$ACCESS_TOKEN";
//path是扫描二维码跳转的小程序路径,可以带参数?id=xxx
//width是二维码宽度
$param = json_encode(array("path"=>$_GET['page'],"width"=> 500));
//通过curl,POST参数,获取通过官方接口传回的图片数据
$result = httpRequest( $qcode, $param,"POST");
//生成二维码
$base64_image ="data:image/jpeg;base64,".base64_encode( $result );
//输出图片
echo '<image src="'.$base64_image.'" style="width:500px;height:565px;"></image>';
//通过curl把请求发送到微信服务器换取二维码
function httpRequest($url, $data='', $method='GET'){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
if($method=='POST')
{
curl_setopt($curl, CURLOPT_POST, 1);
if ($data != '')
{
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
}
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
?>
然后,你就可以通过这个PHP接口,将page信息传入后就能获取到对应的二维码啦!
当然,你也可以把appid和appsecret设成变量,做成一个开放接口供其他人使用,方便你我他,快乐千万家,^-^
评论前必须登录!
注册