php版微信返回用户text输入的方法

yipeiwu_com7年前PHP代码库

本文实例讲述了php版微信返回用户text输入的方法。分享给大家供大家参考,具体如下:

获得用户输入的内容,并发回相同内容

//获取post数据
// $PostData = $HTTP_RAW_POST_DATA;
$PostData = file_get_contents("php://input");
//判断POST是否为空
if(!$PostData){
  echo "wrong input!";
  exit(0);
}
//解析XML字符串
$xmlObj = simplexml_load_string($PostData, 'SimpleXMLElement', LIBXML_NOCDATA);
if(!$xmlObj){
  echo "wrong input!";
  exit(0);
}
//获取数据
$fromUserName = $xmlObj->FromUserName;
$toUserName = $xmlObj->ToUserName;
$msgType = $xmlObj->MsgType;
//返回用户输入
 if('text' != $msgType){
   //不是文本,输出错误提示消息
   $retMsg = "只支持文本消息";
}
//用户输入文本
else{
  $content = $xmlObj->Content;
  $retMsg = $content;
}
//输出xml模板
$retTmp = "<xml>
       <ToUserName><![CDATA[%s]]></ToUserName>
       <FromUserName><![CDATA[%S]]></FromUserName>
       <CreateTime>%s</CreateTime>
       <MsgType><![CDATA[text]]></MsgType>
       <Content><![CDATA[%s]]></Content>
       <FuncFlag>0</FuncFlag>
       </xml>";
//对消息中的通配符进行替换
$resultStr = sprintf($retTmp, $fromUserName, $toUserName, time(), $retMsg);
//输出xml的消息
echo $resultStr

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP微信开发技巧汇总》、《PHP编码与转码操作技巧汇总》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。

相关文章

php实现递归抓取网页类实例

本文实例讲述了php实现递归抓取网页类的方法。分享给大家供大家参考。具体如下: <?php class crawler{ private $_depth=5; pr...

php好代码风格的阶段性总结

本文总结了php好代码的风格,分享给大家供大家参考,具体如下: 1、避免使用魔数 if($age<18){ } 这个18不太明白为什么要这样子。 可以将28定义在一个变量...

php简单截取字符串代码示例

本文实例讲述了php简单截取字符串的方法。分享给大家供大家参考,具体如下: //截取摘要 public static function mbsubstr($str){ $strl...

探讨php中header的用法详解

 header() is used to send raw HTTP headers. See the HTTP/1.1 specification for more info...

require(),include(),require_once()和include_once()区别

我把这两个语句放在一起介绍,读者可以比较学习。 1.require()语句 require()语句用于指定的文件代替语句本身,就象C语言中的include()语句一样。如果php配置文件...