PHP微信開發之二維碼產生類_php執行個體

來源:互聯網
上載者:User
<?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('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){      //產生永久二維碼      $qrcode= '{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": '.$fqid.'}}}';    }else{      //產生臨時二維碼      $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);  }  //下載二維碼到伺服器  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: 無法寫入圖片');      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");  }}
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.