Two-dimension code generation class for PHP micro-credit development

Source: Internet
Author: User

This article mainly introduced the PHP micro-credit Development two-dimensional code generation class, this article uses the micro-letter interface to realize the two-dimensional code generation, and directly to the production example code, needs the friend to be possible to refer to under

?

/**

* Created by Phpstorm.

* User:bin

* date:15-1-16

* Time: 9:48

*/

namespace Homecommon;

Micro-Letter 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 a 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 two-dimensional code

$qrcode = ' {' action_name ': ' Qr_limit_scene ', ' action_info ': {' SCENE ': {' scene_id ': '. $fqid. '}} ';

}else{

Generate a temporary two-dimensional 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 two-dimensional code to the 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." RN ");

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.