PHP WeChat development QR code generation class_PHP tutorial

WBOY
Release: 2016-07-13 09:48:48
Original
820 people have browsed it

PHP WeChat development QR code generation class

This article mainly introduces the PHP WeChat development QR code generation class. This article uses the WeChat interface to realize the generation of QR codes. , and give sample code directly, friends in need can refer to it

 ?

 /**

  * Created by PhpStorm.

  * User: bin

  * Date: 15-1-16

  * Time: 上午9:48

  */

namespace HomeCommon;

// WeChat 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 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 permanent QR code

$qrcode= '{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": '.$fqid.'}}}';

 }else{

//Generate 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 = $filestring.'.jpg';

ob_start();

readfile($url);

$img=ob_get_contents();

ob_end_clean();

 $size=strlen($img);

$fp2=fopen('./Uploads/qrcode/'.$filename,"a");

 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."rn");

 }

 }

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1022060.htmlTechArticlePHP WeChat development QR code generation class This article mainly introduces PHP WeChat development QR code generation class , This article uses the WeChat interface to generate QR codes and directly gives sample codes, which require...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!