php微信開發(fā)之二維碼生成類
來源:易賢網(wǎng) 閱讀:850 次 日期:2015-08-13 15:50:58
溫馨提示:易賢網(wǎng)小編為您整理了“php微信開發(fā)之二維碼生成類”,方便廣大網(wǎng)友查閱!

這篇文章主要介紹了php微信開發(fā)之二維碼生成類,本文使用微信接口實(shí)現(xiàn)二維碼的生成,并直接給出示例代碼,需要的朋友可以參考下

?

/**

* created by phpstorm.

* user: bin

* date: 15-1-16

* time: 上午9:48

*/

namespace home\common;

// 微信處理類

set_time_limit(30);

class weixin{

//構(gòu)造方法

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{

//生成臨時(shí)二維碼

$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);

}

//下載二維碼到服務(wù)器

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");

}

}

更多信息請(qǐng)查看IT技術(shù)專欄

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:php微信開發(fā)之二維碼生成類
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國(guó)考·省考課程試聽報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)