微信公众平台开发——helloworld

摘要:
威信公众平台有两种模式:编辑模式和开发模式。开发模式具有更多的功能。让我们来使用开发模式开发helloword吧步骤如下:1.先注册一个公众号2.注册sae,作为你的服务器。开启“服务者配置”。

威信公众平台有两种模式:编辑模式 和 开发模式。

普通的功能可以通过编辑模式来搞定。开发模式具有更多的功能。让我们来使用开发模式开发helloword吧

步骤如下:

1.先注册一个公众号(https://mp.weixin.qq.com)

2.注册sae(http://sae.sina.com.cn/),作为你的服务器。

3.登录微信公众平台(https://mp.weixin.qq.com)查看开发文档并下载官方提供的demo。做适当修改。

4.将代码压缩成zip格式,上传到sae平台。

5.登录微信公众平台,进入开发者中心。开启“服务者配置”。

6.成功了。

开始吧:

1.先注册一个公众号(https://mp.weixin.qq.com);个人可以注册订阅号(公众号的一种)。

2.注册sae(http://sae.sina.com.cn/)

注册完以后要记得进行实名认证,不然绑定到公众平台的时候,会有永远的“无法获取token”的提示。(实名认证需要3个工作日才能成功)

然后可以点击创建应用。创建后可以在下面看到。

微信公众平台开发——helloworld第1张

进入自己创建的应用。然后点击代码管理。

微信公众平台开发——helloworld第2张

微信公众平台开发——helloworld第3张

3.登录微信公众平台(https://mp.weixin.qq.com)

查看开发文档并下载官方提供的demo。

微信公众平台开发——helloworld第4张

微信公众平台开发——helloworld第5张

微信公众平台开发——helloworld第6张  

打开后是如下的代码:

<?php
/**
  * wechat php test
  */

//define your token
define("TOKEN", "weixin");
$wechatObj = newwechatCallbackapiTest();
$wechatObj->valid();

classwechatCallbackapiTest
{
    public functionvalid()
    {
        $echoStr = $_GET["echostr"];

        //valid signature , option
        if($this->checkSignature()){
            echo $echoStr;
            exit;
        }
    }

    public functionresponseMsg()
    {
        //get post data, May be due to the different environments
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

          //extract post data
        if (!empty($postStr)){
                /*libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
                   the best way is to check the validity of xml by yourself */libxml_disable_entity_loader(true);
                  $postObj = simplexml_load_string($postStr, 'SimpleXMLElement',LIBXML_NOCDATA);
                $fromUsername = $postObj->FromUserName;
                $toUsername = $postObj->ToUserName;
                $keyword = trim($postObj->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(!empty( $keyword))
                {
                      $msgType = "text";
                    $contentStr = "Welcome to wechat world!";
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                    echo $resultStr;
                }else{
                    echo "Input something...";
                }

        }else{
            echo "";
            exit;
        }
    }
        
    private functioncheckSignature()
    {
        //you must define TOKEN by yourself
        if (!defined("TOKEN")) {
            throw new Exception('TOKEN is not defined!');
        }
        
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
                
        $token =TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        //use SORT_STRING rule
        sort($tmpArr,SORT_STRING);
        $tmpStr = implode( $tmpArr);
        $tmpStr = sha1( $tmpStr);
        
        if( $tmpStr == $signature){
            return true;
        }else{
            return false;
        }
    }
}

?>

我试过,如上代码,似乎无法执行到response那一块。所以做了更改

<?php
/**
  * wechat php test
  */

//define your token
define("TOKEN", "weixin");
$wechatObj = newwechatCallbackapiTest();

//这里做了更改
if($_GET["echostr"]){
    $wechatObj->valid();
}else{
    $wechatObj->responseMsg();
}

classwechatCallbackapiTest
{
    public functionvalid()
    {
        $echoStr = $_GET["echostr"];

        //valid signature , option
        if($this->checkSignature()){
            echo $echoStr;
            exit;
        }
    }

    public functionresponseMsg()
    {
        //get post data, May be due to the different environments
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

          //extract post data
        if (!empty($postStr)){
                /*libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
                   the best way is to check the validity of xml by yourself */libxml_disable_entity_loader(true);
                  $postObj = simplexml_load_string($postStr, 'SimpleXMLElement',LIBXML_NOCDATA);
                $fromUsername = $postObj->FromUserName;
                $toUsername = $postObj->ToUserName;
                $keyword = trim($postObj->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(!empty( $keyword))
                {
                      $msgType = "text";
                    $contentStr = "Welcome to wechat world!";
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                    echo $resultStr;
                }else{
                    echo "Input something...";
                }

        }else{
            echo "";
            exit;
        }
    }
        
    private functioncheckSignature()
    {
        //you must define TOKEN by yourself
        if (!defined("TOKEN")) {
            throw new Exception('TOKEN is not defined!');
        }
        
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
                
        $token =TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        //use SORT_STRING rule
        sort($tmpArr,SORT_STRING);
        $tmpStr = implode( $tmpArr);
        $tmpStr = sha1( $tmpStr);
        
        if( $tmpStr == $signature){
            return true;
        }else{
            return false;
        }
    }
}

?>

你可以将Welcome to wechat world!更改为Hello World!

4.将代码压缩成zip格式,上传到sae平台。

微信公众平台开发——helloworld第7张

点击“编辑代码”,可以看到你上传的php文件。然后右击,url查看。复制url(http://1.carlzhang.sinaapp.com/wx_carlzhang819.php)。在这里要记住在此php文件中定义的token。此处为“weixin”,可以在如下图中看到。

微信公众平台开发——helloworld第8张

5.登录微信公众平台,进入开发者中心。开启“服务者配置”。url填写上一步复制的url(这里我删除了前面的1.因为我的sae默认第一版。你可以试试,删除1,若是url访问,不报404,那就是没问题)。token填写的是代码中的token(上面是“weixin”)。

微信公众平台开发——helloworld第9张

微信公众平台开发——helloworld第10张

如果启用成功,就可以关注你的微信平台,输入内容,看看提示是不是Welcome to wechat world!或者Hello World!

是的?那祝贺你,成功了。

免责声明:文章转载自《微信公众平台开发——helloworld》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇磁盘 blk_update_request: I/O errorMPI编程简单介绍下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

vue后台管理系统项目

项目介绍1.项目根目录文件 2.源码子目录结构 3.api目录 4.assets目录 5.components目录 6.mixins目录 7.permission目录 8.router目录 9.store目录 10.styles目录 11.utils目录 项目文件介绍1.安装element-ui组件实现按需加载 // 1.1.npm...

深入浅出接口幂等性的实现方式

阅读目录 一、数据库去重表 二、状态机 三、TOKEN机制 1.代码实现 2.演示 阅读目录 接口幂等性如何实现? 回到目录 接口幂等性如何实现?  导读 转载自幂等性如何实现?深入了解一波!!! 现在这个时代大家可能最关心的就是钱了,那么有没有想过你银行转账给你没有一次是转多的,要么失败,要么成功,为什么不能失误一下多转一笔...

shell 通过 data命令实现 时间戳 和 时间 相互转换

#!/bin/bash starttime=$(date "+%Y-%m-%d 00:00:00" -d "-1 day") endtime=$(date "+%Y-%m-%d 23:59:59" -d "-1 day") echo ${time1} starttime1=`date -d "$starttime" +%s` endtime1=`da...

保障接口安全的5种常见方式

一般有五种方式:1、Token授权认证,防止未授权用户获取数据;2、时间戳超时机制;3、URL签名,防止请求参数被篡改;4、防重放,防止接口被第二次请求,防采集;5、采用HTTPS通信协议,防止数据明文传输; 所有的安全措施都用上的话有时候难免太过复杂,在实际项目中需要根据自身情况作出取舍,比如可以只使用签名机制就可以保证信息不会被篡改,或者定向提供服务的...

HBase 伪分布式环境搭建及基础命令使用

一.前提条件: (1)文件存储在HDFS文件系统之上。因此必须启动hadoop服务。(namenode,datanode,resourcemanager,nodemanager,historyserver)(2)源文件依赖于zookeeper。因此需要启动zookeeper服务。(./zkServer ./zkCli.sh) 二,HBase的安装(版本:5...

ffmpeg文档21-分离器

分离器 分离器是使得ffmpeg能从特定类型文件中读取多媒体流的组件元素。 当编译ffmepg时,所有支持的分离器都默认被包含,你可以通过编译配置脚本中的--list-demuxers列出所有支持的分离器。 你也可以通过配置--disable-demuxers禁用所有的分离器,如果要在此基础上允许单独的分离器可以选用--enable-demuxer=DE...