微信物联网云服务

2017-04-02  本文已影响0人  木木口丁

微信物联网开发原理图:

一、微信公众号与用户端的交互

1.你需要的东西

**我自己的公众号**

2.自定义服务器上的部署

3.开发接口验证

微信公众平台技术文档

一个不错的PHP在线执行工具

<?php
define("TOKEN","weixin");       // 定义token
$wechatObj = new wechat_php();  // 生成类实例
$wechatObj->valid();        // 调用类的检验方法

// 定义一个操作微信公众帐号的类
class wechat_php
{
    // 定义公用校验方法
    public function valid()
    {
        $echoStr = $_GET["echostr"];    // 获取GET请求的参数echostr
        
        // 校验signature
        if($this->checkSignature ()) {  // 调用校验方法
            echo $echoStr;
            exit;
        }
    }
    
    // 校验方法
    private function checkSignature ()
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);    // 将三个参数保存到数组中
        sort($tmpArr);      // 对数组中三个数据进行排序
        $tmpStr = implode( $tmpArr );       // 将数组中三个数据组成一个字符串
        $tmpStr = sha1( $tmpStr );      // 对字符串进行SHA-1散列运算
        
        if( $tmpStr == $signature ) {       // 计算结果与$signature相等
            return true;        // 通过验证
        } else {
            return false;       // 未通过验证
        }
    }
}
?>

4.开始编写代码进行开发

例1:文本消息自动被动回复

<?php
$wechatObj = new wechat_php();
$wechatObj->GetTextMsg();

class wechat_php
{
    public function GetTextMsg()
    {
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
        
        if (!empty($postStr))
        {
            $postStr = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            $fromUesrname = $postStr->FromUserName;
            $toUsername = $postStr->ToUserName;
            $msgType = $postStr->MsgType;
            $keyword = trim($postStr->Content);
            $time = time();
            
            $textTpl = "<xml>
                        <ToUserName><![CDATA[%s]]></ToUserName>
                        <FromUserName><![CDATA[%s]]></FromUserName>
                        <CreateTime>%s</CreateTime>
                        <MsgType><![CDATA[%s]]></MsgType>
                        <Content><![CDATA[%s]]></Content>
                        <FuncFlag>0</FuncFlag>
                        </xml>";
            if (strtolower($msgType) != "text")
            {
                $msgType = "text";
                $contentStr = "我只接收文本信息!";
            }else{
                if(!empty( $keyword ))
                {
                    $msgType = "text";
                    $contentStr = "消息内容:" . $keyword . "\n";
                    $contentStr = $contentStr . "ToUserName:" . $toUsername . "\n";
                    $contentStr = $contentStr . "FromUserName:" . $fromUesrname;
                }else{
                    $contentStr = "请输入关键字...";
                }
            }
            $resultStr = sprintf($textTpl, $fromUesrname, $toUsername, $time, $msgType, $contentStr);
            ob_clean();
            echo $resultStr;
        }else{
            echo "";
            exit;
        }
    }
}
?>
上一篇 下一篇

猜你喜欢

热点阅读