QR code generation in PHP Development
This article mainly introduces the QR code generation class for PHP development. This article uses interfaces to generate QR codes and directly provides sample code. For more information, see
?
/**
* Created by PhpStorm.
* User: bin
* Date: 15-1-16
* Time: AM
*/
Namespace Home \ Common;
// Processing class
Set_time_limit (30 );
Class Weixin {
// Constructor
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 a QR 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 the QR code to the server
Protected function DownLoadQr ($ url, $ filestring ){
If ($ url = ""){
Return false;
}
$ Filename = paifilestring.'.jpg ';
Ob_start ();
Readfile ($ url );
$ Img = ob_get_contents ();
Ob_end_clean ();
$ Size = strlen ($ img );
$ Fp2 = fopen ('./Uploads/qrcode/'. $ filename, "");
If (fwrite ($ fp2, $ img) === false ){
$ This-> ErrorLogger ('dolwload image falied. Error Info: unable to write image ');
Exit ();
}
Fclose ($ fp2 );
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 ");
}
}