/**
* wechat php test
*/
//define your token
define("TOKEN", "abcabc"); //abcabc是自己的token,随便改都可以的,只要与微信那边对应就可以了。
$wechatObj = new wechatCallbackapiTest();
$wechatObj->responseMsg();
//$wechatObj->valid();
class wechatCallbackapiTest
{
/*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);
$RX_TYPE = trim($postObj->MsgType);
switch($RX_TYPE)
{
case "text":
$resultStr = $this->handleText($postObj);
break;
case "event":
$resultStr = $this->handleEvent($postObj);
break;
default:
$resultStr = "Unknow msg type: ".$RX_TYPE;
break;
}
echo $resultStr;
}else {
echo "";
exit;
}
}
public function handleText($postObj)
{
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$time = time();
$textTpl = "
%s
0
";
if(!empty( $keyword ))
{
$msgType = "text";
$str=mb_substr($keyword,-2,2,"UTF-8");
$str_key=mb_substr($keyword,0,-2,"UTF-8");
$dbconn=oci_connect("username","password","dblink"); //一开始的时候数据库链接放到最外层,可惜不知道为何没有数据返回,后来移到这里就可以了
if($keyword == "1")
{
$contentStr="[11]班轮集港计划查询\n"."[12]在港船舶计划查询";
}elseif($keyword == "2")
{
$contentStr="[21]集装箱查询\n"."[22]订舱号查询\n"."[23]预约号查询";
}elseif($keyword=="11")
{
$sql="SELECT * FROM TABLE1";
$stmt=oci_parse($dbconn,$sql);
$contentStr="";
oci_execute($stmt);
while(($row=oci_fetch_array($stmt,OCI_ASSOC))!=false)
{
$contentStr=$contentStr.$row['v1']."(".$row['V2']."/".$row['V3']."),靠泊时间".$row['V4']."\n";
}
}elseif($keyword=="12")
{
$sql="SELECT * FROM TABLE2";
$stmt=oci_parse($dbconn,$sql);
$contentStr="";
oci_execute($stmt);
while(($row=oci_fetch_array($stmt,OCI_ASSOC))!=false)
{
$contentStr=$contentStr.$row['V1']."(".$row['V2']."/".$row['V3']."),实际靠泊时间".$row['V4']."预计离泊时间".$row['V5']."\n";
}
}elseif(mb_substr($keyword,0,2,"UTF-8")=="21")
{
$str_key=mb_substr($keyword,2,11,"UTF-8");
$sql="SELECT * FROM TABLE3 where V1='$str_key'";
$stmt=oci_parse($dbconn,$sql);
$contentStr="";
oci_execute($stmt);
while(($row=oci_fetch_array($stmt,OCI_ASSOC))!=false)
{
$contentStr=$contentStr.$row['V1']."在".$row['V2']."/".$row['V3']."\n";
}
}elseif($str=='天气' && !empty($str_key))
{
$data=$this->weather($str_key);
if(empty($data->weatherinfo))
{
$contentStr="抱歉,没有查到\"".$str_key."\"的天气情况!";
}
else
{
$contentStr = "【".$data->weatherinfo->city."天气预报】\n".$data->weatherinfo->date_y." ".$data->weatherinfo->fchh."时发布"."\n\n实时天气\n".$data->weatherinfo->weather1." ".$data->weatherinfo->temp1." ".$data->weatherinfo->wind1."\n\n温馨提示:".$data->weatherinfo->index_d."\n\n明天\n".$data->weatherinfo->weather2." ".$data->weatherinfo->temp2." ".$data->weatherinfo->wind2."\n\n后天\n".$data->weatherinfo->weather3." ".$data->weatherinfo->temp3." ".$data->weatherinfo->wind3;
}
}
else
{
$contentStr="欢迎关注志恒测试平台\n"."[1]船期查询\n"."[2]在场箱查询";
}
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}else{
echo "Input something...";
}
}
private function weather($n)
{
include("weather_cityId.php"); //需要在同一路径下加入该页面
$c_name=$weather_cityId[$n];
if(!empty($c_name)){
$json=file_get_contents("http://m.weather.com.cn/data/".$c_name.".html");
return json_decode($json);
} else {
return null;
}
}
public function handleEvent($object)
{
$contentStr = "";
switch ($object->Event)
{
case "subscribe":
$contentStr = "Thank you for your concentrate"."\n"."If you like us , pls say Hello";
break;
default :
$contentStr = "Unknow Event: ".$object->Event;
break;
}
$resultStr = $this->responseText($object, $contentStr);
return $resultStr;
}
public function responseText($object, $content, $flag=0)
{
$textTpl = "
%s
%d
";
$resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $flag);
return $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;
}
}
}
?>
weather_cityId.php
$weather_cityId = array("北京"=>"101010100","上海"=>"101020100","苏州"=>"101190401","番禺"=>"101280102","广州"=>"101280101");
?>