<?php/** * Created by Phpstorm. * User:bin * date:15-1-16 * time: Morning 9:48 */namespace home\common;//processing class set_time_limit (+); 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 (' id ' = ' = ') Hatid))->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 _tok EN; if ($type = = 1) {//Generate permanent QR code $qrcode = ' {"Action_name": "Qr_limit_scene", "Action_info": {"SCENE": {"scene_id": '. $f QId. '}} '; }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 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 "); }}