php微信開發(fā)之批量生成帶參數(shù)的二維碼
帶參數(shù)的二維碼對于渠道營銷推廣來說是很有用的,可以獲得多個帶不同場景值的二維碼,用戶掃描后,公眾號可以接收到事件推送,可喜的是微信開通了這個接口,那下面就來研究一下吧。
具體接口說明請參見,微信公眾平臺開發(fā)者文檔(http://mp.weixin.qq.com/wiki/18/28fc21e7ed87bec960651f0ce873ef8a.html) ,我這里就直接上代碼。
演示圖:

由于帶參數(shù)二維碼生成是有限的,所有我是按編號生成的,下次生成的時候直接累加。
另外帶設置有備注,方便以后統(tǒng)計。
public function createewm(){
if(IS_POST){
$access_token=checkAccessToken($this->token); //獲取access_token
$json_url='https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='.$access_token;
$action_name=$this->_post('action_name'); //生成類型(臨時、永久)
$create_num=$this->_post('create_num'); //生成數(shù)量
//數(shù)據(jù)庫里查詢最后生成一個編號
$now_secne_id=M('erweima')->where(array('token'=>$this->token))->order('scene_id desc')->getField('scene_id');
//新生成在最后一個編輯上加1
$start_secne_id=intval($now_secne_id)+1;
$end_secne_id=intval($now_secne_id)+intval($create_num);
$n=0;
for($i=$start_secne_id;$i<=$end_secne_id;$i++){
$curl_data='';
if($action_name=='QR_SCENE'){
//臨時 post的json數(shù)據(jù)
$curl_data='{"expire_seconds": 1800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": '.$i.'}}}';
}
if($action_name=='QR_LIMIT_SCENE'){
//永久 post的json數(shù)據(jù)
$curl_data='{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": '.$i.'}}}';
}
$json_info=json_decode($this->api_notice_increment($json_url,$curl_data),true);
//這里代表生成成功,記錄數(shù)據(jù)以便插入到數(shù)據(jù)庫,方便以后統(tǒng)計查找
if($json_info['errcode']!=40013){
$data[$n]['token']=$this->token;
$data[$n]['tiket']=$json_info['ticket'];
$data[$n]['url']=$json_info['url'];
$data[$n]['scene_id']=$i;
$data[$n]['expire_seconds']=$json_info['expire_seconds'];
$data[$n]['action_name']=$action_name;
$data[$n]['remark']='';
$data[$n]['createtime']=time();
$n++;
}else{
$this->error('操作失敗');
}
}
if(count($data)>0){
$res= M('erweima')->addAll($data);//插入數(shù)據(jù)
if($res){
$this->success('添加成功');
}else{
$this->error('操作失敗');
}
}else{
$this->error('操作失敗');
}
}
}
function api_notice_increment($url, $data){
$ch = curl_init();
$header = "Accept-Charset: utf-8";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$tmpInfo = curl_exec($ch);
if (curl_errno($ch)) {
//curl_close( $ch )
return $ch;
}else{
//curl_close( $ch )
return $tmpInfo;
}
curl_close( $ch ) ;
}
很簡單,基本夠用了,喜歡就拿走
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
php-fpm.conf配置文件中文說明詳解及重要參數(shù)說明
本文主要介紹了php-fpm.conf配置文件的中文說明詳解以及php-fpm.conf重要參數(shù)配置說明,最后有一個監(jiān)控php-fpm進程運行狀態(tài)的頁面代碼實例2018-10-10
PHP實現(xiàn)的統(tǒng)計數(shù)據(jù)功能詳解
這篇文章主要介紹了PHP實現(xiàn)的統(tǒng)計數(shù)據(jù)功能,結合實例形式分析了php數(shù)據(jù)查詢與顯示處理的相關操作技巧,需要的朋友可以參考下2016-12-12
PHP通過內(nèi)置函數(shù)memory_get_usage()獲取內(nèi)存使用情況
這篇文章主要介紹了PHP通過內(nèi)置函數(shù)memory_get_usage()獲取內(nèi)存使用情況,需要的朋友可以參考下2014-11-11
PHP傳輸base64數(shù)據(jù)不完整的解決方法
最近在做OCR增值稅務處理時,接口是通過圖片轉base64提交處理然后返回數(shù)據(jù)的,通過在線工具進行測試,發(fā)現(xiàn)傳遞過去的數(shù)據(jù)可以使用,接收到的數(shù)據(jù)卻提示損壞,所以本文給大家介紹了PHP傳輸base64數(shù)據(jù)不完整的解決方法,需要的朋友可以參考下2024-05-05

