PHP development of the QR code generation class
This article mainly introduces the PHP development of the two-dimensional code generation class, this article uses the interface to realize the generation of two-dimensional code, and directly to show the example code, the need for friends can refer to the next
?
/**
* Created by Phpstorm.
* User:bin
* date:15-1-16
* Time: 9:48
*/
namespace Home\common;
Processing class
Set_time_limit (30);
Class weixin{
Construction method
static $qrcode _url = "Https://api.weixin.qq.com/cgi-bin/qrcode/create?";
static $token _url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&";
static $qrcode _get_url = "Https://mp.weixin.qq.com/cgi-bin/showqrcode?";
Generate two-dimensional code
Public Function Getewm ($wechatid, $fqid, $type = 1) {
$wechat = M (' member_public ')->where (array (' id ' = = $wechatid))->find ();
$appid = $wechat [' AppID '];
$secret = $wechat [' secret '];
$ACCESS _token = $this->gettoken ($appid, $secret);
$url = $this->getqrcodeurl ($ACCESS _token, $fqid, 1);
Return DOWNLOADQR ($url, Time ());
}
protected function Getqrcodeurl ($ACCESS _token, $fqid, $type = 1) {
$url = self:: $qrcode _url. ' access_token= '. $ACCESS _token;
if ($type = = 1) {
Generate a permanent QR code
$qrcode = ' {"Action_name": "Qr_limit_scene", "Action_info": {"SCENE": {"scene_id": '. $fqid. '}} ';
}else{
Generate a temporary QR code
$qrcode = ' {"expire_seconds": 1800, "Action_name": "Qr_scene", "Action_info": {"SCENE": {"scene_id": '. $fqid. '}} ';
}
$result = $this->http_post_data ($url, $qrcode);
$oo = Json_decode ($result [1]);
if (! $oo->ticket) {
$this->errorlogger (' Getqrcodeurl falied. Error Info:getqrcodeurl get failed ');
Exit ();
}
$url = self:: $qrcode _get_url. ' ticket= '. $oo->ticket. ';
return $url;
}
protected function GetToken ($appid, $secret) {
$ACCESS _token = file_get_contents (self:: $token _url. " appid= $appid &secret= $secret ");
$ACCESS _token = Json_decode ($ACCESS _token);
$ACCESS _token = $ACCESS _token->access_token;
return $ACCESS _token;
}
protected function Http_post_data ($url, $data _string) {
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_post, 1);
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, Curlopt_postfields, $data _string);
curl_setopt ($ch, Curlopt_httpheader, Array (
' Content-type:application/json; Charset=utf-8 ',
' Content-length: '. strlen ($data _string))
);
Ob_start ();
Curl_exec ($ch);
if (Curl_errno ($ch)) {
$this->errorlogger (' Curl falied. Error Info: '. Curl_error ($ch));
}
$return _content = ob_get_contents ();
Ob_end_clean ();
$return _code = Curl_getinfo ($ch, Curlinfo_http_code);
Return Array ($return _code, $return _content);
}
Download QR code to server
protected function Downloadqr ($url, $filestring) {
if ($url = = "") {
return false;
}
$filename = $filestring. '. JPG ';
Ob_start ();
ReadFile ($url);
$img =ob_get_contents ();
Ob_end_clean ();
$size =strlen ($IMG);
$fp 2=fopen ('./uploads/qrcode/'. $filename, "a");
if (Fwrite ($fp 2, $img) = = = = False) {
$this->errorlogger (' dolwload image falied. Error Info: Unable to write picture ');
Exit ();
}
Fclose ($fp 2);
Return './uploads/qrcode/'. $filename;
}
Private Function Errorlogger ($ERRMSG) {
$logger = fopen ('./errorlog.txt ', ' A + ');
Fwrite ($logger, date (' y-m-d h:i:s '). " Error Info: ". $errMsg." \ r \ n ");
}
}
http://www.bkjia.com/PHPjc/1022060.html www.bkjia.com true http://www.bkjia.com/PHPjc/1022060.html techarticle PHP development of two-dimensional code generation class This article mainly introduces the PHP development of the two-dimensional code generation class, this article uses the interface to realize the generation of two-dimensional code, and directly to show the sample code, need ...