2013年5月29日星期三

洛洛带你在SAE上零基础玩转微信公众平台消息接口


前不久微信公众平台开放了自定义消息接口,这给广大微友们带来了一个新的机会,自定义消息接口说简单了就是在原有200条规则上的无限扩增,说复杂了他就是一个万能接口,没有做不到只有想不到。Ps:微信消息发送到微博上去~

要使用此接口开发,就需要有个域名和空间,SAE刚好有二级域名和空间使用,于是申请了个http://zero.sinaapp.com/

对于完全没有接触过SAE的同学,下面我来告诉大家如何去快速的使用SAE~
1.打开新浪云SAE网址
http://sae.sina.com.cn/

2.点击右上角的登录
3.使用新浪微博进行登录

4.返回我的首页

5.点右侧创建新应用


6.填写应用信息

7.完成应用创建后,我们就有了一个二级域名和一个空间。

接下来,我们下载官方的提供的demo,修改demo中的token值,然后传到SAE上,传完后,我们到公众平台填写接口配置信息,进行网址接入的验证。
WX官方demo如下,已经修改了那个符号错误。
PHP代码
<?php
/**
  * wechat php test
  */
//define your token
define("TOKEN", "wxapi");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();
class wechatCallbackapiTest
{
    public function valid()
  {
    $echoStr = $_GET["echostr"];
    //valid signature , option
    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 );
     
      if( $tmpStr == $signature ){
        return true;
      }else{
        return false;
      }
    }
}
?>
通过验证后我们就可以开始进行消息接口的开发了,消息接口我们要做的就是获取粉丝的信息然后按照获取到的信息进行匹配,匹配出信息再发给粉丝。
通过三个例子从不会到会~对应了我三周每周的学习成果。

第一个最简单的例子,就是实现了,粉丝发送消息到公众平台返回粉丝的的消息。
微信请添加演示号码:weijiekou

代码如下:
PHP代码
<?php
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if (!emptyempty($postStr)){
    $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(!emptyempty( $keyword )){
      $msgType = "text";
      $resultStr = sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$keyword);
      echo $resultStr;
    }else{
      echo "Input something...";
    }
}else {
    echo "";
    exit;
}
?>

第二个是实现了微友查天气预报,可发送地区名称、电话区号、邮政编码、城市拼音到公众平台就可返回天气预报。如:北京,返回对应的天气预报。(由于是演示,天气预报数据使用了weather.com.cn的数据方便大家测试,测试后请删除。)

微信请添加演示号码:tianqi114



代码如下:
PHP代码
<?php
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if (!emptyempty($postStr)){
    $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(!emptyempty( $keyword )){
      $msgType = "text";
      $post_data = array();
      $post_data['city'] = $keyword;
      $post_data['submit'] = "submit";
      $url='http://search.weather.com.cn/wap/search.php';
      $o="";
      foreach ($post_data as $k=>$v){
        $o.= "$k=".urlencode($v)."&";
      }
      $post_data=substr($o,0,-1);
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_POST, 1);
      curl_setopt($ch, CURLOPT_HEADER, 0);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_URL,$url);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
      $result = curl_exec($ch);
      curl_close($ch);
      $result=explode('/',$result);
      $result=explode('.',$result['5']);
      $citynum = $result['0'];
      $weatherurl = "http://m.weather.com.cn/data/".$citynum.".html";
      $weatherjson = file_get_contents($weatherurl);
      $weatherarray = json_decode($weatherjson,true);
      $weatherinfo = $weatherarray['weatherinfo'];
      $contentTpl = "#这里是%s#(%s)
%s%s
%s时发布的天气预报:
今天天气:%s
%s,%s
穿衣指数:%s
紫外线指数:%s
洗车指数:%s
明天天气:%s
%s,%s
后天天气:%s
%s,%s";
      $contentStr = sprintf($contentTpl,$weatherinfo['city'],$weatherinfo['city_en'],$weatherinfo['date_y'],$weatherinfo['week'],$weatherinfo['fchh'],$weatherinfo['temp1'],$weatherinfo['weather1'],$weatherinfo['wind1'],$weatherinfo['index_d'],$weatherinfo['index_uv'],$weatherinfo['index_xc'],$weatherinfo['temp2'],$weatherinfo['weather2'],$weatherinfo['wind2'],$weatherinfo['temp3'],$weatherinfo['weather3'],$weatherinfo['wind3']);
      $resultStr = sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);
      echo $resultStr;
    }else{
      echo "Input something...";
    }
}else {
    echo "";
    exit;
}
?>
第三个就是实现了无限消息回复

请添加演示微信:xianhouyu

没有评论:

发表评论