Home > Backend Development > PHP Tutorial > WeChat development - Php batch generation of QR codes with parameters, php parameters_PHP tutorial

WeChat development - Php batch generation of QR codes with parameters, php parameters_PHP tutorial

WBOY
Release: 2016-07-13 09:59:35
Original
1359 people have browsed it

WeChat development - Php batch generates QR codes with parameters, php parameters

QR codes with parameters are very useful for channel marketing and promotion. Obtain multiple QR codes with different scene values. After the user scans them, the official account can receive event push. Fortunately, WeChat has opened this interface, so let’s study it below.

For specific interface description, please refer to the WeChat public platform developer documentation (http://mp.weixin.qq.com/wiki/18/28fc21e7ed87bec960651f0ce873ef8a.html). I will directly enter the code here.

Demo picture:

Since the generation of QR codes with parameters is limited, I generate them by number, and they will be accumulated directly the next time they are generated.

In addition, there are notes in the settings to facilitate future statistics.

<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>生成类型(临时、永久)</span>
<span> 7</span>             <span>$create_num</span>=<span>$this</span>->_post('create_num');     <span>//</span><span>生成数量
</span><span> 8</span> 
<span> 9</span> <span>            //数据库里查询最后生成一个编号</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>新生成在最后一个编辑上加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><=<span>$end_secne_id</span>;<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>这里代表生成成功,记录数据以便插入到数据库,方便以后统计查找</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

It’s very simple. It’s basically enough. Just take it if you like it

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/975797.htmlTechArticleWeChat development - Php batch generates QR codes with parameters, php parameters QR codes with parameters are for It is very useful for channel marketing and promotion. You can get multiple two-dimensional images with different scene values...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template