微信 被回复信息

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

//define your token
define("TOKEN", "123456");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->responseMsgCity();

class wechatCallbackapiTest
{
   public function valid()
    {
        $echoStr = $_GET["echostr"];

        //valid signature , option
        if($this->checkSignature()){
           echo $echoStr;
           exit;
        }
    }
      //特定消息特定回复
    public function responseMsgCity()
    {

        //PHP5.6之前用这个获取原始数据
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
        // error_log(var_export($postStr,1),3,'./log.txt');exit; 
        if(!empty($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>";
            $imagesTpl = "<xml>
                        <ToUserName><![CDATA[%s]]></ToUserName>
                        <FromUserName><![CDATA[%s]]></FromUserName>
                        <CreateTime>%s</CreateTime>
                        <MsgType><![CDATA[%s]]></MsgType>
                        <Image>
                        <MediaId><![CDATA[%s]]></MediaId>
                        </Image>
                        </xml>";
            $voiceTpl = "<xml>
                        <ToUserName><![CDATA[%s]]></ToUserName>
                        <FromUserName><![CDATA[%s]]></FromUserName>
                        <CreateTime>%s</CreateTime>
                        <MsgType><![CDATA[%s]]></MsgType>
                        <Voice>
                        <MediaId><![CDATA[%s]]></MediaId>
                        </Voice>
                        </xml>";
            if(!empty($keyword))
            {
                // $url = "http://api.k780.com/?app=weather.today&weaid={$keyword}&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json";
                // $weather = file_get_contents($url);
                // //将json数据转化为数组
                // $weather_array = json_decode($weather,true);
                // if($weather_array['success'] == 1)
                // { 
                //     $msgType = "text";
                //     $contentStr = "您的位置在".$weather_array['result']['citynm']."\r\n"."目前是".$weather_array['result']['week']."\r\n".
                //     "温度为".$weather_array['result']['temperature'];
                //     $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                //     echo $resultStr;
                // }
                // else
                // {
                //     $msgType = "text";
                //     $contentStr = "您输入的方位不对!!!";
                //     $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                //     echo $resultStr;
                // } 
                //图片
                 if($keyword=='图片'){
                     $msgType = "image";
                     $contentStr = "ieoPDQ_Cty67siv0BQmvrHl59C8j8z7rj9-lieoWY69_2g_pzEHZMIwgFHeA1FDK";
                     $resultStr = sprintf($imagesTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                     echo $resultStr;
                 }
                //语音
                // if($keyword=='语音'){
                //     $msgType = "voice";
                //     $contentStr = "XvAALcAwIOusMKPJAQVCFgcoE-4yncuRplNFxBa1nVuqSSLeHLeNAlCkHnpO4sxT";
                //     $resultStr = sprintf($voiceTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                //     echo $resultStr;
                // }
            }             
        } 
    }
    
      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;
      }
   }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值