微信公众平台开发入门教程(图文),公众入门教程_PHP教程

WBOY
Lepaskan: 2016-07-12 08:52:26
asal
1227 orang telah melayarinya

微信公众平台开发入门教程(图文),公众入门教程

 

关键字:微信公众平台开发入门

作者:C++研究室

 

 背景知识:微信公众平台的开发需要有一定的PHP基础知识,PHP是一种HTML内嵌式的语言,广泛用于网站开发。微信服务器和开发者的服务器间有两种数据传输方式,分别是XML和JSON。其中XML主要用在接受发送普通消息和事件推送中,而用户管理、创建自定义菜单和高级群发等则需要用到JOSN格式数据。

 

在这篇微信公众平台开发教程中,你可以跟着教程一起在整体上感性了解微信公众平台的开发框架,可以帮助你更好的去入手。

我们将使用微信公众账号西安校园创客空间作为讲解的例子,二维码见底部。

这个入门教程将引导你学会完成如下任务:

 

                                                     第一节

 

申请新浪云计算并创建平台应用

 

 

 

      第二节  申请虚拟主机及为微信平台搭建环境

         这里我使用的是云邦互联的虚拟空间为大家作以演示,首先,打开网站如下图:

               

 

          然后在右上角按照提示注册即可,选择使用1G的免费空间。

    2、开通空间之后打开控制面板,就可以看到主机信息了。

         

   3、接下来我们用 8uftp 登陆虚拟空间。

       

 

 

    4、登录之后,用 8uftp   将 index.php 文件上传到虚拟空间的根目录中的web文件夹下,如下:

       

      至此,你使用虚拟空间为微信公众平台搭建服务器就成功了,后续只要将实现相应功能的代码通过8uftp上传上去即可。

 

 

 

第三节 启用开发模式与接入服务器

微信公众平台开发模式

高级功能

微信公众平台地址:https://mp.weixin.qq.com

登录微信公众平台后台,在左侧列表中最下方,找到“ 基本配置 ”,点击进入

进入服务器配置填写框。

点击“修改配置”按钮,则出现下图。

此处的URL为上篇中介绍的云应用的域名,而Token在index.php中定义为weixin。EncodingAESKey则不用填,点击“随机生成”让自动生成一个,消息加解密方式选择“明文模式”,然后点击“提交”按钮。

在弹出的提示框中,点击“确定”

配置修改后如图所示,再点击“启用”按钮

询问“是否确定开启服务器配置”,点击“确定”

如果提示“token验证失败”,可以先重次几次,微信服务器有时候不稳定。:如果使用新浪SAE可能要求进行实名认证,请先上传身份证进行实名认证并通过审核后再试!

 

 

                   

 

 

                     如果还是失败,请先用微信调试器测试一下url和token是否正确。(直接在百度搜索 会有好多免费的哦)

第四节   接口的调用与常用功能的实现

             

   示例1:API调用之百度翻译

        申请百度翻译API接口

下面介绍如何使用百度翻译 API 制作拥有翻译功能的公众账号。

    1、首先,登陆申请成为百度开发者,申请链接地址为 http://developer.baidu.com/ 。申请成为开发者之后,登陆该网站,在管理控制台的下拉菜单中选择开发者服务管理,如下图所示。

   

  1. 我们通过调用百度翻译的API可以实现英文翻译中文,中文翻译英文,中文翻译日文,日文翻译中文。目前百度AIP支持这三种语言的翻译

  2. 首先进入百度翻译的网页,网站如下箭头所示

  3. 进入后点击“使用指南”

  4. 进入使用智能页面后,网下浏览页面,在页面中间部分,即可看到百度翻译的API接口

  5. 根据上面的接口,我们可以构建接口代码:

    http://openapi.baidu.com/public/2.0/bmt/translate?client_id=uA6zT1kh5O1UXvTrUuFjFHiK&q={$keyword}&from=auto&to=auto

    “9peNkh97N6B9GGj9zBke9tGQ”这一串为自己的百度密钥,在百度的应用中心单击基本信息可以看到

  6. 关键代码为:

    case "text";

    $tranurl="http://openapi.baidu.com/public/2.0/bmt/translate?client_id=uA6zT1kh5O1UXvTrUuFjFHiK&q={$keyword}&from=auto&to=auto";//百度翻译地址

                    $transtr=file_get_contents($tranurl);//读入文件

    $transon=json_decode($transtr);//json解析

    //print_r($transon);

    $contentStr = $transon->trans_result[0]->dst;//读取翻译内容

    break;

  7. 登录手机微信,最后实现的结果如下:

     所用代码如下:
<span> 1</span> <?<span>php
</span><span> 2</span> <span>//</span><span>6.2 百度翻译  C++研究室  CopyRight 2016 </span>
<span> 3</span> <span>define</span>("TOKEN", "weixin"<span>);
</span><span> 4</span> <span>$wechatObj</span> = <span>new</span><span> wechatCallbackapiTest();
</span><span> 5</span> <span>if</span> (!<span>isset</span>(<span>$_GET</span>['echostr'<span>])) {
</span><span> 6</span>     <span>$wechatObj</span>-><span>responseMsg();
</span><span> 7</span> }<span>else</span><span>{
</span><span> 8</span>     <span>$wechatObj</span>-><span>valid();
</span><span> 9</span> <span>}
</span><span>10</span> 
<span>11</span> <span>class</span><span> wechatCallbackapiTest
</span><span>12</span> <span>{
</span><span>13</span>     <span>public</span> <span>$fromUsername</span>=''<span>;
</span><span>14</span>     <span>public</span> <span>$toUsername</span>=''<span>;
</span><span>15</span>  <span>public</span> <span>function</span><span> valid()
</span><span>16</span> <span>    {
</span><span>17</span>         <span>$echoStr</span> = <span>$_GET</span>["echostr"<span>];
</span><span>18</span>         <span>if</span>(<span>$this</span>-><span>checkSignature()){
</span><span>19</span>             <span>echo</span> <span>$echoStr</span><span>;
</span><span>20</span>             <span>exit</span><span>;
</span><span>21</span> <span>        }
</span><span>22</span> <span>    }
</span><span>23</span> 
<span>24</span>     <span>private</span> <span>function</span><span> checkSignature()
</span><span>25</span> <span>    {
</span><span>26</span>         <span>$signature</span> = <span>$_GET</span>["signature"<span>];
</span><span>27</span>         <span>$timestamp</span> = <span>$_GET</span>["timestamp"<span>];
</span><span>28</span>         <span>$nonce</span> = <span>$_GET</span>["nonce"<span>];
</span><span>29</span>         <span>$token</span> =<span> TOKEN;
</span><span>30</span>         <span>$tmpArr</span> = <span>array</span>(<span>$token</span>, <span>$timestamp</span>, <span>$nonce</span><span>);
</span><span>31</span>         <span>sort</span>(<span>$tmpArr</span>,<span> SORT_STRING);
</span><span>32</span>         <span>$tmpStr</span> = <span>implode</span>(<span>$tmpArr</span><span>);
</span><span>33</span>         <span>$tmpStr</span> = <span>sha1</span>(<span>$tmpStr</span><span>);
</span><span>34</span> 
<span>35</span>         <span>if</span>(<span>$tmpStr</span> == <span>$signature</span><span>){
</span><span>36</span>             <span>return</span> <span>true</span><span>;
</span><span>37</span>         }<span>else</span><span>{
</span><span>38</span>             <span>return</span> <span>false</span><span>;
</span><span>39</span> <span>        }
</span><span>40</span> <span>    }
</span><span>41</span> 
<span>42</span>     <span>public</span> <span>function</span><span> responseMsg(){
</span><span>43</span>         <span>$postStr</span> = <span>$GLOBALS</span>["HTTP_RAW_POST_DATA"<span>];
</span><span>44</span>             <span>$postObj</span> = <span>simplexml_load_string</span>(<span>$postStr</span>, 'SimpleXMLElement',<span> LIBXML_NOCDATA);
</span><span>45</span>                 <span>$fromUsername</span> = <span>$postObj</span>-><span>FromUserName;
</span><span>46</span>                 <span>$toUsername</span> = <span>$postObj</span>-><span>ToUserName;
</span><span>47</span>                 <span>$type</span> = <span>$postObj</span>-><span>MsgType;
</span><span>48</span>                 <span>$event</span>=<span>$postObj</span>-><span>Event;
</span><span>49</span>                 <span>$Event_Key</span>=<span>$postObj</span>-><span>EventKey;
</span><span>50</span>                 <span>$mid</span>=<span>$postObj</span>-><span>MediaId;
</span><span>51</span>                 <span>$link</span>=<span>$postObj</span>-><span>Url;
</span><span>52</span>             
<span>53</span>                 <span>$latitude</span>  = <span>$postObj</span>-><span>Location_X;
</span><span>54</span>                 <span>$longitude</span> = <span>$postObj</span>-><span>Location_Y;
</span><span>55</span>                 <span>$keyword</span> = <span>trim</span>(<span>$postObj</span>-><span>Content);
</span><span>56</span>                 <span>$time</span> = <span>time</span><span>();
</span><span>57</span>                  <span>$textTpl</span> = "<span><xml>
</span><span>58</span> <span>                            <ToUserName><![CDATA[%s]]></ToUserName>
</span><span>59</span> <span>                            <FromUserName><![CDATA[%s]]></FromUserName>
</span><span>60</span> <span>                            <CreateTime>%s</CreateTime>
</span><span>61</span> <span>                            <MsgType><![CDATA[text]]></MsgType>
</span><span>62</span> <span>                            <Content><![CDATA[%s]]></Content>
</span><span>63</span>                             </xml>"<span>; 
</span><span>64</span>                             <span>if</span>(<span>$keyword</span>!=''<span>){
</span><span>65</span>                             <span>$id</span>="R90FXoW4OPtCbLkD9Aiaihz0"<span>;
</span><span>66</span> <span>$url</span>="http://openapi.baidu.com/public/2.0/bmt/translate?client_id=<span>$id</span>&q=<span>$keyword</span>&from=auto&to=auto"<span>;
</span><span>67</span> <span>$res</span>=<span>file_get_contents</span>(<span>$url</span><span>);
</span><span>68</span> <span>$res</span>=json_decode(<span>$res</span>,<span>true</span><span>);
</span><span>69</span> <span>$contentStr</span>=<span>$res</span>['trans_result'][0]['dst'<span>];
</span><span>70</span> }                    <span>$resultStr</span> = <span>sprintf</span>(<span>$textTpl</span>, <span>$fromUsername</span>, <span>$toUsername</span>, <span>$time</span>, <span>$contentStr</span><span>);
</span><span>71</span>                     <span>echo</span> <span>$resultStr</span><span>;
</span><span>72</span> <span>  }
</span><span>73</span> <span>}
</span><span>74</span> ?>
Salin selepas log masuk

示例2:API调用之小黄鸡

1、注册simsimi 账号

URL: http://developer.simsimi.com/signUp

2、 激活账号

3、 获取API Key

4、 具体实现

调用小黄鸡API 实现

调用simsim($keyword) 函数处理,将其中的“Your API Key” 换成申请到的API Key。

  <span>//</span><span>小黄鸡   C++研究室   CopyRight 2016</span>
    <span>public</span> <span>function</span> simsim(<span>$keyword</span><span>){

        </span><span>$key</span>="41250a68-3cb5-43c8-9aa2-d7b3caf519b1"<span>;
        </span><span>$url_simsimi</span>="http://sandbox.api.simsimi.com/request.p?key=".<span>$key</span>."&lc=ch&ft=0.0&text=".<span>$keyword</span><span>;
        
        </span><span>$json</span>=<span>file_get_contents</span>(<span>$url_simsimi</span>);  <span>//</span><span> 把整个文件读入一个字符串中</span>

        <span>$result</span>=json_decode(<span>$json</span>,<span>true</span>);  <span>//</span><span> 对JSON 格式的字符串进行编码

        //$errorCode=$result['result'];  // 调试用</span>

        <span>$response</span>=<span>$result</span>['response'];  <span>//</span><span> 回复的消息</span>

        <span>if</span>(!<span>empty</span>(<span>$response</span><span>)){
            </span><span>return</span> <span>$response</span><span>;
        }</span><span>else</span><span>{
            </span><span>$ran</span>=<span>rand</span>(1,5<span>);
            </span><span>switch</span>(<span>$ran</span><span>){
                </span><span>case</span> 1:
                    <span>return</span> "小鸡鸡今天累了,明天再陪你聊天吧。"<span>;
                    </span><span>break</span><span>;
                </span><span>case</span> 2:
                    <span>return</span> "小鸡鸡睡觉喽~~"<span>;
                    </span><span>break</span><span>;
                </span><span>case</span> 3:
                    <span>return</span> "呼呼~~呼呼~~"<span>;
                    </span><span>break</span><span>;
                </span><span>case</span> 4:
                    <span>return</span> "你话好多啊,不跟你聊了"<span>;
                    </span><span>break</span><span>;
                </span><span>case</span> 5:
                    <span>return</span> "感谢您关注西安校园创客空间"."\n"."微信号:xaxymaker"."\n"<span>;
                    </span><span>break</span><span>;
                </span><span>default</span>:
                    <span>return</span> "感谢您关注西安校园创客空间"."\n"."微信号:xaxymaker"."\n"<span>;
                    </span><span>break</span><span>;
            }
        }
    }</span>
Salin selepas log masuk

 

 

       同时,微信公众号还可以实现 天气查询 火车查询 快递查询 会员卡、优惠券、大转盘、微网站、3G相册之类的。微菜单,微网站,微会员,微团购,微调研,微相册,微推送,微统计,微支付,微客服,等等功能。

 

 以上便是简单的微信公众平台基本教程,如果你用心感悟了,感觉你有所收获了,一切始于实践,请按照教程入手,开启你的微信开发之旅吧!!  如果还有什么不懂的,请扫一扫公众号二维码关注,回复“代码”、“微信教程” ...   全部视频教程 代码资料 等着你!!!

         感谢你的阅读,请用心感悟!希望可以帮到初学的您!!分享也是一种快乐!!!请接力。。。

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1126417.htmlTechArticle微信公众平台开发入门教程(图文),公众入门教程 关键字: 微信公众平台开发入门 作者:C++研究室 背景知识:微信公众平台的开发需要...
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan