PHP applet generating micro-channel two-dimensional code, the two-dimensional code may be generated with parameters

Micro letter applet official opening of the three-dimensional code to create interfaces, one of which is to generate two-dimensional code, there is a sunflower-shaped applet code, I am here to generate two-dimensional code with php.

First of all to get Access_token
this request it is also very easy, there are micro-channel development documents request interface:
make your own applets to obtain APPID and APPSECRET

https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
. 1
the access_token only valid for two hours, it is best to be cached, to avoid duplication request

Construction Request parameter


You can build an array and then converted into data to a variable json

json_encode param = $ (Array ( "path" => "? Pages / index / index ID = 123", "width" => 150));
. 1
and transmits the data to the micro-channel server POST exchange two-dimensional code

完整代码
<?php
header('content-type:text/html;charset=utf-8');
//配置APPID、APPSECRET
$APPID = "APPID";
$APPSECRET = "APPSECRET";
//获取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);
// var_dump($json);
$_SESSION['access_token'] = $json['access_token'];
$_SESSION['expires_in'] = time()+7200;
$ACCESS_TOKEN = $json["access_token"];
}
else{

$ACCESS_TOKEN = $_SESSION["access_token"];
}

// request to build two-dimensional code parameter
// path is a two-dimensional code scanning to jump a small program path, can take parameters = xxx the above mentioned id?
// width is the width of the two-dimensional code
$ qcode = "https: //api.weixin. qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=$ACCESS_TOKEN ";
$ param = json_encode (Array ("? path "=>" Pages / index / index ID = 123 "," width "=> 150)) ;

//POST参数
$result = httpRequest( $qcode, $param,"POST");
//生成二维码
file_put_contents("qrcode.png", $result);
$base64_image ="data:image/jpeg;base64,".base64_encode( $result );

//把请求发送到微信服务器换取二维码
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;
}

?>

Usage:
1, the new qrcode.php
2, copy the code above into
3, and modifications APPID appsecret
. 4, access qrcode.php

Parameters to generate a two-dimensional code of the applet

 

But this interface is generated LOGO applet two-dimensional code.
We want to produce a simple, no LOGO two-dimensional code applet can do?
The official is no way, but the ordinary way we can generate two-dimensional code with third-party interfaces.

Generating a two-dimensional code without LOGO step
1, the first decoding, the applet just generated two-dimensional code decoding, to obtain URL
2, the interface generation without LOGO ordinary two-dimensional code obtained by the URL generated by conventional two-dimensional code

Decoding
decoding fact, many libraries directly with the third party JSSDK decoding of me here, free of the need to apply the interface and appid and appkey

<h2>生成无LOGO二维码</h2>
<!DOCTYPE html>
<html>
<head>
<title>PHP生成微信小程序二维码</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdn.bootcss.com/zepto/1.0rc1/zepto.min.js"></script>
<script src="hprose.min.js"></script>
<script src="md5.min.js"></script>
</head>
<body>
<!--显示二维码-->
<div id="show_test"></div>
<div id="select_img" style="display:none;">
<input type="file" id="imgTest" type="file" accept=".gif,.jpg,.jpeg,.png">
<br/>
<img src="" id="showImage" alt="">
</div>
<a href="javascript:;" id="qrdecode"> Click Generate no LOGO two-dimensional code </a>
<Script>

get_timestamp function = var () {
var timestamp = 0;
timestamp = Date.parse (new new a Date ()); // get the current time stamp (in s)
timestamp = timestamp / 1000;
return timestamp;
};

// config, application decoding interfaces: http: //www.wwei.cn/, free
var api_id = 'xxxxxxx'; // read your own
var api_key = 'xxxxxxxxx'; // read your own
var timestamp get_timestamp = ();
var = hprose.Client.create Client ( 'http://hprose.wwei.cn/qrcode.html', [ 'qrencode', 'qrdecode']);

// decode
$ ( "# qrdecode") the Click (function () {.
Var = get_timestamp timestamp ();
var imgUrl = ''; // remote image
var imgdata = '<? Php echo $ base64_image;?>'; / / local image, generates a direct call official program small two-dimensional code, for decoding
var Signature = MD5 (timestamp api_key + + + imgUrl imgdata);
client.ready (function (qrcode) {
qrcode.qrdecode (api_id, Signature, timestamp , imgUrl, imgdata)
.then (function (Result) {
IF (= result.status. 1!) {
Alert (result.msg);
return;
}

// generates a two-dimensional code without LOGO api interfaces
var qrcodeapi = "http://qr.liantu.com/api.php?text=";
// url and splicing the interface and outputs the decoded image
$ ( "# show_test") .html ( '<IMG the src = "' + qrcodeapi result.data.raw_text + + '" />');
}, function (E) {
console.error (E);
});
},
function (E) {
Console .error (E);
});
});
</ Script>
</ body>
</ HTML>

the above is achieved by js, the decoded picture is called a two-dimensional code generated address php of

<? php echo $ base64_image;?>
1
above is to call the local picture, it requires a combination of php code generation procedures small two-dimensional code decoding using this program.

LOGO no need to generate two-dimensional code is decoded, this I only need to call one interface on the line.

Interface: http: //qr.liantu.com/api.php text =?

Url is then decoded after splicing can generate a two-dimensional code of.


Then the applet code generation and code generation without LOGO combine two-dimensional code is:

<?php
header('content-type:text/html;charset=utf-8');
//配置APPID、APPSECRET
$APPID = "APPID";
$APPSECRET = "APPSECRET";
//获取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);
// var_dump($json);
$_SESSION['access_token'] = $json['access_token'];
$_SESSION['expires_in'] = time()+7200;
$ACCESS_TOKEN = $json["access_token"];
}
else{

$ACCESS_TOKEN = $_SESSION["access_token"];
}

// request to build two-dimensional code parameter
// path is a two-dimensional code scanning to jump a small program path, can take parameters = xxx the above mentioned id?
// width is the width of the two-dimensional code
$ qcode = "https: //api.weixin. qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=$ACCESS_TOKEN ";
$ param = json_encode (Array ("? path "=>" Pages / index / index ID = 123 "," width "=> 150)) ;

//POST参数
$result = httpRequest( $qcode, $param,"POST");
//生成二维码
file_put_contents("qrcode.png", $result);
$base64_image ="data:image/jpeg;base64,".base64_encode( $result );

//把请求发送到微信服务器换取二维码
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;
}

?>

<h2> official program generates a small two-dimensional code </ h2>
<- - generate applet official two-dimensional code!>
<img src = "<PHP echo $ base64_image;>??" />

<br/>
<h2>生成无LOGO二维码</h2>
<!-- 生成无LOGO二维码 -->
<!DOCTYPE html>
<html>
<head>
<title>PHP生成微信小程序二维码</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdn.bootcss.com/zepto/1.0rc1/zepto.min.js"></script>
<script src="hprose.min.js"></script>
<script src="md5.min.js"></script>
</head>
<body>
<div id="show_test"><!--显示信息--></div>
<div id="select_img" style="display:none;">
<input type="file" id="imgTest" type="file" accept=".gif,.jpg,.jpeg,.png">
<br/>
<img src="" id="showImage" alt="">
</ div>
<a href="javascript:;" id="qrdecode"> Click Generate no LOGO two-dimensional code </a>
<Script>

get_timestamp function = var () {
var timestamp = 0;
timestamp = Date.parse (new new a Date ()); // get the current time stamp (in s)
timestamp = timestamp / 1000;
return timestamp;
};

// config, application decoding interfaces: http: //www.wwei.cn/, free
var api_id = 'xxx'; // read your own
var api_key = 'xxx'; // read your own (real , it is recommended to complete the signature signatures in the background to prevent exposure api_key, or regularly change)
var timestamp = get_timestamp ();
var Client = hprose.Client.create ( 'http://hprose.wwei.cn/qrcode.html', [ 'qrencode', 'qrdecode' ]);

// decode
$ ( "# qrdecode") the Click (function () {.
Var = get_timestamp timestamp ();
var imgUrl = ''; // remote image
var imgdata = '<? Php echo $ base64_image;?>'; / / local image, generates a direct call official program small two-dimensional code, for decoding
var Signature = MD5 (timestamp api_key + + + imgUrl imgdata);
client.ready (function (qrcode) {
qrcode.qrdecode (api_id, Signature, timestamp , imgUrl, imgdata)
.then (function (Result) {
IF (= result.status. 1!) {
Alert (result.msg);
return;
}

// generates a two-dimensional code without LOGO api interfaces
var qrcodeapi = "http://qr.liantu.com/api.php?text=";
// url and splicing the interface and outputs the decoded image
$ ( "# show_test") .html ( '<IMG the src = "' + qrcodeapi result.data.raw_text + + '" />');
}, function (E) {
console.error (E);
});
},
function (E) {
Console .error (E);
});
});
</ Script>
</ body>
</ HTML>

but need to use the decoded two libraries js

Download directly here: https: //pan.baidu.com/s/1UYDj_WbmCG21VPd8XtpaXA

Guess you like

Origin www.cnblogs.com/lanshu/p/11934897.html