首頁  >  文章  >  後端開發  >  PHP微信介面實作二維碼生成類

PHP微信介面實作二維碼生成類

墨辰丷
墨辰丷原創
2018-06-09 13:47:162461瀏覽

本篇文章主要介紹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針對上傳圖片檔案的功能詳解

php透過檔案儲存來實現快取的技巧

#php根據陣列動態建立html程式碼的方法

php根據陣列動態建立html程式碼的方法######## ########

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

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