Home > php教程 > php手册 > 微信开发之Php批量生成带参数的二维码

微信开发之Php批量生成带参数的二维码

WBOY
Release: 2016-06-06 19:36:21
Original
1473 people have browsed it

带 参数 的二维码对于渠道营销推广来说是很有用的,可以获得多个带不同场景值的二维码,用户扫描后,公众号可以接收到事件推送,可喜的是微信开通了这个接口,那下面就来研究一下吧。 具体接口说明请参见,微信公众平台 开发 者文档(http://mp.weixin.qq.co

参数的二维码对于渠道营销推广来说是很有用的,可以获得多个带不同场景值的二维码,用户扫描后,公众号可以接收到事件推送,可喜的是微信开通了这个接口,那下面就来研究一下吧。

具体接口说明请参见,微信公众平台开发者文档(http://mp.weixin.qq.com/wiki/18/28fc21e7ed87bec960651f0ce873ef8a.html),我这里就直接上代码。

演示图:

微信开发之Php批量生成带参数的二维码

由于带参数二维码生成是有限的,所有我是按编号生成的,下次生成的时候直接累加。

另外带设置有备注,方便以后统计。

<span> 1</span> <span>public</span> <span>function</span><span> createewm(){
</span><span> 2</span>         <span>if</span><span>(IS_POST){
</span><span> 3</span>             <span>$access_token</span>=checkAccessToken(<span>$this</span>->token);  <span>//</span><span>获取access_token</span>
<span> 4</span>             <span>$json_url</span>='https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='.<span>$access_token</span><span>;
</span><span> 5</span>              
<span> 6</span>             <span>$action_name</span>=<span>$this</span>->_post('action_name');   <span>//</span><span><strong>生成</strong>类型(临时、永久)</span>
<span> 7</span>             <span>$create_num</span>=<span>$this</span>->_post('create_num');     <span>//</span><span><strong>生成</strong>数量
</span><span> 8</span> 
<span> 9</span> <span>            //数据库里查询最后<strong>生成</strong>一个编号</span>
<span>10</span>             <span>$now_secne_id</span>=M('erweima')->where(<span>array</span>('token'=><span>$this</span>->token))->order('scene_id desc')->getField('scene_id'<span>);
</span><span>11</span> 
<span>12</span>             <span>//</span><span>新<strong>生成</strong>在最后一个编辑上加1</span>
<span>13</span>             <span>$start_secne_id</span>=<span>intval</span>(<span>$now_secne_id</span>)+1<span>;
</span><span>14</span>             <span>$end_secne_id</span>=<span>intval</span>(<span>$now_secne_id</span>)+<span>intval</span>(<span>$create_num</span><span>);
</span><span>15</span>             <span>$n</span>=0<span>;
</span><span>16</span>             <span>for</span>(<span>$i</span>=<span>$start_secne_id</span>;<span>$i</span>$end_secne_id;<span>$i</span>++<span>){
</span><span>17</span>                 <span>$curl_data</span>=''<span>;
</span><span>18</span>                 <span>if</span>(<span>$action_name</span>=='QR_SCENE'<span>){
</span><span>19</span>                     <span>//</span><span>临时 post的json数据</span>
<span>20</span>                     <span>$curl_data</span>='{"expire_seconds": 1800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": '.<span>$i</span>.'}}}'<span>;
</span><span>21</span> <span>                }
</span><span>22</span>                 
<span>23</span>                 <span>if</span>(<span>$action_name</span>=='QR_LIMIT_SCENE'<span>){
</span><span>24</span>                     <span>//</span><span>永久 post的json数据</span>
<span>25</span>                     <span>$curl_data</span>='{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": '.<span>$i</span>.'}}}'<span>;
</span><span>26</span> <span>                } 
</span><span>27</span>                 <span>$json_info</span>=json_decode(<span>$this</span>->api_notice_increment(<span>$json_url</span>,<span>$curl_data</span>),<span>true</span><span>); 
</span><span>28</span> 
<span>29</span>                 <span>//</span><span>这里代表<strong>生成</strong>成功,记录数据以便插入到数据库,方便以后统计查找</span>
<span>30</span>                 <span>if</span>(<span>$json_info</span>['errcode']!=40013<span>){ 
</span><span>31</span>                     <span>$data</span>[<span>$n</span>]['token']=<span>$this</span>-><span>token;
</span><span>32</span>                     <span>$data</span>[<span>$n</span>]['tiket']=<span>$json_info</span>['ticket'<span>];
</span><span>33</span>                     <span>$data</span>[<span>$n</span>]['url']=<span>$json_info</span>['url'<span>];
</span><span>34</span>                     <span>$data</span>[<span>$n</span>]['scene_id']=<span>$i</span><span>;
</span><span>35</span>                     <span>$data</span>[<span>$n</span>]['expire_seconds']=<span>$json_info</span>['expire_seconds'<span>];
</span><span>36</span>                     <span>$data</span>[<span>$n</span>]['action_name']=<span>$action_name</span><span>;
</span><span>37</span>                     <span>$data</span>[<span>$n</span>]['remark']=''<span>;
</span><span>38</span>                     <span>$data</span>[<span>$n</span>]['createtime']=<span>time</span><span>();
</span><span>39</span>                     <span>$n</span>++<span>;
</span><span>40</span>                 }<span>else</span><span>{
</span><span>41</span>                     <span>$this</span>->error('操作失败'<span>);
</span><span>42</span> <span>                }
</span><span>43</span> <span>            }
</span><span>44</span> 
<span>45</span>             <span>if</span>(<span>count</span>(<span>$data</span>)>0<span>){
</span><span>46</span>                 <span>$res</span>= M('erweima')->addAll(<span>$data</span>);<span>//</span><span>插入数据</span>
<span>47</span>                 <span>if</span>(<span>$res</span><span>){
</span><span>48</span>                     <span>$this</span>->success('添加成功'<span>);
</span><span>49</span>                 }<span>else</span><span>{
</span><span>50</span>                     <span>$this</span>->error('操作失败'<span>);
</span><span>51</span> <span>                }
</span><span>52</span>             }<span>else</span><span>{
</span><span>53</span>                 <span>$this</span>->error('操作失败'<span>);
</span><span>54</span> <span>            } 
</span><span>55</span> <span>        } 
</span><span>56</span>     }
Copy after login

<span>function</span> api_notice_increment(<span>$url</span>, <span>$data</span><span>){
        </span><span>$ch</span> =<span> curl_init();
        </span><span>$header</span> = "Accept-Charset: utf-8"<span>;
        curl_setopt(</span><span>$ch</span>, CURLOPT_URL, <span>$url</span><span>);
        curl_setopt(</span><span>$ch</span>, CURLOPT_CUSTOMREQUEST, "POST"<span>);
        curl_setopt(</span><span>$ch</span>, CURLOPT_SSL_VERIFYPEER, <span>FALSE</span><span>);
        curl_setopt(</span><span>$ch</span>, CURLOPT_SSL_VERIFYHOST, <span>FALSE</span><span>);
        curl_setopt(</span><span>$curl</span>, CURLOPT_HTTPHEADER, <span>$header</span><span>);
        curl_setopt(</span><span>$ch</span>, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'<span>);
        curl_setopt(</span><span>$ch</span>, CURLOPT_FOLLOWLOCATION, 1<span>);
        curl_setopt(</span><span>$ch</span>, CURLOPT_AUTOREFERER, 1<span>);
        curl_setopt(</span><span>$ch</span>, CURLOPT_POSTFIELDS, <span>$data</span><span>);
        curl_setopt(</span><span>$ch</span>, CURLOPT_RETURNTRANSFER, <span>true</span><span>);
        </span><span>$tmpInfo</span> = curl_exec(<span>$ch</span><span>);
        </span><span>if</span> (curl_errno(<span>$ch</span><span>)) {
            </span><span>//</span><span>curl_close( $ch )</span>
            <span>return</span> <span>$ch</span><span>;
        }</span><span>else</span><span>{
            </span><span>//</span><span>curl_close( $ch ) </span>
            <span>return</span> <span>$tmpInfo</span><span>;
        }
        curl_close( </span><span>$ch</span><span> ) ;
    }</span>
Copy after login

 

 很简单,基本够用了,喜欢就拿走

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template