代码如下,在输入的$contentStr =$keyword . ":" . $this->con_temp;只获取到了$keyword的值,而$this->con_temp的值并获得。
比如发送 "天空之城" 返回 "天空之城:"
望指点。
<?php
/**
* wechat php test
*/
//define your token
include('mysql.class.php');
$weixinconn = new mysql_con($cfg_dbhost, $cfg_dbname, $cfg_dbuser, $cfg_dbpwd, $cfg_db_language);
define("TOKEN", "jita");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->responseMsg();
class wechatCallbackapiTest{
public $con_temp = "";
public function valid()
{
$echoStr = $_GET["echostr"];
//valid signature , option
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
public function responseMsg()
{
//get post data, May be due to the different environments
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
//extract post data
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>";
if(!empty( $keyword ))
{
$sql = "select * from dede_uploads where title = '$keyword' "; //如果此处$keyword换成 天空之城 童话 等关键词就可以返回正确结果
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)){
$this->con_temp = $this->con_temp . " ".$row['url'];
}
$msgType = "text";
$contentStr =$keyword . ":" . $this->con_temp;
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}else{
echo "Input something...";
}
}else {
echo "";
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;
}
}
}
?>
比如发送 "天空之城" 返回 "天空之城:"
望指点。
<?php
/**
* wechat php test
*/
//define your token
include('mysql.class.php');
$weixinconn = new mysql_con($cfg_dbhost, $cfg_dbname, $cfg_dbuser, $cfg_dbpwd, $cfg_db_language);
define("TOKEN", "jita");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->responseMsg();
class wechatCallbackapiTest{
public $con_temp = "";
public function valid()
{
$echoStr = $_GET["echostr"];
//valid signature , option
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
public function responseMsg()
{
//get post data, May be due to the different environments
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
//extract post data
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>";
if(!empty( $keyword ))
{
$sql = "select * from dede_uploads where title = '$keyword' "; //如果此处$keyword换成 天空之城 童话 等关键词就可以返回正确结果
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)){
$this->con_temp = $this->con_temp . " ".$row['url'];
}
$msgType = "text";
$contentStr =$keyword . ":" . $this->con_temp;
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}else{
echo "Input something...";
}
}else {
echo "";
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;
}
}
}
?>