<?php
define("TOKEN", "YoonPer"); //TOKEN值
$wechatObj = new wechat();
$wechatObj->valid();
class wechat {
public function valid() {
$echoStr = $_GET["echostr"];
if($this->checkSignature()){
echo $echoStr;
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;
}
}
}
?>
<xml> <!--开发者微信号--> <ToUserName><![CDATA[toUser]]></ToUserName> <!--发送方帐号(OpenID)--> <FromUserName><![CDATA[fromUser]]></FromUserName> <!--消息创建时间 (整型)--> <CreateTime>12345678</CreateTime> <!--消息类别 (text文本消息)--> <MsgType><![CDATA1]></MsgType> <!--消息内容--> <Content><![CDATA[content]]></Content> <!--消息ID (64位整型)--> <MsgId>1234567890123456</MsgId> </xml>
<xml> <!--接收方帐号(OpenID)--> <ToUserName><![CDATA[toUser]]></ToUserName> <!--开发者微信号--> <FromUserName><![CDATA[fromUser]]></FromUserName> <!--消息创建时间 (整型)--> <CreateTime>12345678</CreateTime> <!--消息类别 (text文本消息)--> <MsgType><![CDATA1]></MsgType> <!--回复消息内容--> <Content><![CDATA[content]]></Content> <!--星标操作(位0x0001被标志时 星标刚收到的消息)--> <FuncFlag>0</FuncFlag> </xml>
<?php
/*-------------------------------------------------
| index.php [ 微信公众平台接口 ]
+--------------------------------------------------
| Author: LimYoonPer
+------------------------------------------------*/
$wechatObj = new wechat();
$wechatObj->responseMsg();
class wechat {
public function responseMsg() {
//---------- 接 收 数 据 ---------- //
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //获取POST数据
//用SimpleXML解析POST过来的XML数据
$postObj = simplexml_load_string($postStr,'SimpleXMLElement',LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName; //获取发送方帐号(OpenID)
$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>";
$msgType = "text"; //消息类型
include('simsimi.php');
$contentStr = simsimi($keyword); //返回消息内容
//格式化消息模板
$resultStr = sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);
echo $resultStr; //输出结果
}
}
?>
<?php
/*-------------------------------------------------
| simsimi.php [ 智能聊天(simsimi) ]
+--------------------------------------------------
| Author: LimYoonPer
+------------------------------------------------*/
function simsimi ($keyword)
{
$keyword = urlencode($keyword);
//----------- 获取COOKIE ----------//
$url = "http://www.simsimi.com/";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($ch);
list($header, $body) = explode("\r\n\r\n", $content);
preg_match_all("/set\-cookie:([^\r\n]*);/iU", $header, $matches);
$cookie = implode(';', $matches[1]).";simsimi_uid=1;";
curl_close($ch);
//----------- 抓 取 回 复 ----------//
$url = "http://www.simsimi.com/func/reqN?lc=ch&ft=0.0&req=$keyword&fl=http%3A%2F%2Fwww.simsimi.com%2Ftalk.htm";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
$content = json_decode(curl_exec($ch), 1);
curl_close($ch);
if ( $content['result'] == '200' ) {
return $content['sentence_resp'];
} else {
return '我还不会回答这个问题...';
}
}
?>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有