搜尋
首頁微信小程式微信開發微信開發之二維碼生成類

微信開發之二維碼生成類

Mar 11, 2017 pm 04:22 PM
php微信開發

這篇文章主要介紹了PHP微信開發之二維碼生成類,本文使用微信接口實現二維碼的生成,並直接給出示例代碼,需要的朋友可以參考下


#
<?php
/**
 * Created by PhpStorm.
 * User: bin
 * Date: 15-1-16
 * Time: 上午9:48
 */
namespace Home\Common;

// 微信处理类
set_time_limit(30);
class Weixin{
  //构造方法
  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?";

  //生成二维码
  public function getEwm($wechatid,$fqid,$type = 1){
    $wechat = M(&#39;Member_public&#39;)->where(array(&#39;id&#39;=> $wechatid))->find();
    $appid = $wechat[&#39;appid&#39;];
    $secret = $wechat[&#39;secret&#39;];
    $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.&#39;access_token=&#39;.$ACCESS_TOKEN;
    if($type == 1){
      //生成永久二维码
      $qrcode= &#39;{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": &#39;.$fqid.&#39;}}}&#39;;
    }else{
      //生成临时二维码
      $qrcode = &#39;{"expire_seconds": 1800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": &#39;.$fqid.&#39;}}}&#39;;
    }
    $result = $this->http_post_data($url,$qrcode);
    $oo = json_decode($result[1]);
    if(!$oo->ticket){
      $this->ErrorLogger(&#39;getQrcodeurl falied. Error Info: getQrcodeurl get failed&#39;);
      exit();
    }
    $url = self::$qrcode_get_url.&#39;ticket=&#39;.$oo->ticket.&#39;&#39;;
    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(
        &#39;Content-Type: application/json; charset=utf-8&#39;,
        &#39;Content-Length: &#39; . strlen($data_string))
    );
    ob_start();
    curl_exec($ch);
    if (curl_errno($ch)) {
      $this->ErrorLogger(&#39;curl falied. Error Info: &#39;.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);
  }
  //下载二维码到服务器
  protected function DownLoadQr($url,$filestring){
    if($url == ""){
      return false;
    }
    $filename = $filestring.&#39;.jpg&#39;;
    ob_start();
    readfile($url);
    $img=ob_get_contents();
    ob_end_clean();
    $size=strlen($img);
    $fp2=fopen(&#39;./Uploads/qrcode/&#39;.$filename,"a");
    if(fwrite($fp2,$img) === false){
      $this->ErrorLogger(&#39;dolwload image falied. Error Info: 无法写入图片&#39;);
      exit();
    }
    fclose($fp2);
    return &#39;./Uploads/qrcode/&#39;.$filename;
  }

  private function ErrorLogger($errMsg){
    $logger = fopen(&#39;./ErrorLog.txt&#39;, &#39;a+&#39;);
    fwrite($logger, date(&#39;Y-m-d H:i:s&#39;)." Error Info : ".$errMsg."\r\n");
  }

}

以上是微信開發之二維碼生成類的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。