微信开发自动回复功能,消息类型判断失败,PHP语言

Director404 2016-01-28 10:38:39
编写自动回复,针对用户不同消息类型做出回复,程序怎么调都有问题额。
问题:无论我发送什么消息,文本也好,图片也好,语音也好,都只回复“我只接收图片消息”,也就是说程序似乎不执行
if(strtolower($msgType) =="image"){..............}这一判断.
求各位大神看一下~~~~
<?php
// wechat php


//define your token定义接口
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
$wechatObj ->GetImageMsg();//这个要调用自动回复消息!!
$wechatObj->valid();

class wechatCallbackapiTest
{
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>接口验证>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
public function valid()
{
$echoStr = $_GET["echostr"];

//valid signature , option
if($this->checkSignature()){
header('content-type:text');//在微信样本代码上添加了如此语句,因而接口接成功
echo $echoStr;
exit;
}
}
private function checkSignature()
{
// 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;
}
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>自动回复消息>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


public function GetImageMsg()//自动回复消息
{

$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if (!empty($postStr))
{

libxml_disable_entity_loader(true);
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$msgType=$postObj->$MsgType;
$picUrl=trim($postObj->PicUrl);
$mediaId=trim($postObj->MediaId);
$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) =="image")
{

if(!empty($picUrl)){
$msgType ="text";
$contentStr = "图片链接:".$picUrl."\n";
$contentStr =$contentStr."媒体ID:".$mediaId;
}else{
$contentStr = "请发送图片哦";
}
}else{
$msgType ="text";
$contentStr = "我只接收图片消息";
}

$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}
else
{
echo "";
exit;
}
}



}

?>
...全文
117 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Director404 2016-01-29
  • 打赏
  • 举报
回复
引用 1 楼 u013129324 的回复:
可能65行应该是$msgType=$postObj->MsgType;这个吧 多了个$???
谢谢~~~~很有用!亏我搞了半天,原来是这个呀
-0000- 2016-01-28
  • 打赏
  • 举报
回复
可能65行应该是$msgType=$postObj->MsgType;这个吧 多了个$???

20,359

社区成员

发帖
与我相关
我的任务
社区描述
“超文本预处理器”,是在服务器端执行的脚本语言,尤其适用于Web开发并可嵌入HTML中。PHP语法利用了C、Java和Perl,该语言的主要目标是允许web开发人员快速编写动态网页。
phpphpstorm 技术论坛(原bbs)
社区管理员
  • 开源资源社区
  • phpstory
  • xuzuning
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧