PHP WeChat QR code generation class, php generation

Source: Internet
Author: User

PHP development QR code generation class, php generation

<? Php/*** Created by PhpStorm. * User: bin * Date: 15-1-16 * Time: */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 the 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 :: $ q Rcode_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, "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. "\ r \ n ");}}

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.