这个依然是文字版的,而且不支持语音,是通过抓包分析小i机器人wap版的获取。
可以通过扫描下面的二维码或者直接添加微信号xiaoibot进行关注思密达
但是最近小i wap版的demo可能发现有人盗用了,所以把“机器人”封了,如果用之前的方法,会出现403 forbidden的错误,所以我又重新弄了一下,加上了一些浏览器信息,这样就不会封了~
之前不想公布来着,但是没有但是……共享才是王道,大家共同进步。
上 代码:
<div style="background:#fdfdfd;color:black;"><u>JavaScript+PHP语言</u>: </div>
<div class="source" style="font-family: '[object HTMLOptionElement]', Consolas, 'Lucida Console', 'Courier New'; color: rgb(246, 243, 232); background-color: rgb(36, 36, 36);"><span style="color: rgb(229, 120, 109);"><?php</span>
/
作者:Kaedeen
来源:http://www.kaedeen.com/
日期:2013.1.7
修改:2013.1.15 14号被封掉,错误是403,特此修改
/
//define your token
define("TOKEN", "kaede");
$wxObj = new weixinCallbackApi();
//$wxObj->valid(); //第一次验证时使用
$wxObj->responseMsg();
class weixinCallbackApi
{
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 ))
{
$msgType = "text";
$contentStr = "";
$picnews = "";
//新关注我的用户
if($keyword=='Hello2BizUser')
{
$contentStr=$this->welcome($toUsername);
}
$contentStr = $this->get_wap_xiaoi($keyword);
$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;
}
}
//调用小i机器人wap版
public function get_wap_xiaoi($key){
$post_data =
array(
'requestContent='.$key,
);
$post_data = implode('&',$post_data);
/**old method
$url='http://nlp.xiaoi.com/robot/demo/wap/wap-demo.action';
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_USERAGENT,"Opera/9.60");
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
ob_start();
curl_exec($ch);
$result = ob_get_contents() ;
ob_end_clean();
/
$url="http://nlp.xiaoi.com/robot/demo/wap/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$content = curl_exec($ch);
curl_close($ch);
//echo $content;
list($header, $body) = explode("rnrn", $content);
preg_match("/set-cookie:([^rn]*)/i", $header, $matches);
$cookie = $matches[1];
$ch = curl_init( );
curl_setopt( $ch, CURLOPT_REFERER, "http://nlp.xiaoi.com/robot/demo/wap/" );
curl_setopt( $ch, CURLOPT_HEADER, true );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)" );
curl_setopt( $ch, CURLOPT_URL, "http://nlp.xiaoi.com/robot/demo/wap/wap-demo.action" );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_data );
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
$content = curl_exec($ch);
curl_close($ch);
sae_log($content);
$preg = '/</span>(.*)</p>/iUs';
preg_match_all($preg,$content,$match);
$response_msg=$match[0][0];
$preg = "/</?[^>]+>/i";
$response_msg=preg_replace($preg,'',$response_msg);
if("hello,how are you"==$response_msg||"how do you do"==$response_msg)
{
$response_msg="小i机器人欢迎您,作者主页地址:www.kaedeen.com。小i机器人不断学习中,欢迎各种调戏…/:,@-D";//欢迎语
}
$response_msg=trim($response_msg);
return $response_msg;
}
//问候语
public function welcome($toUsername) {
if($toUsername=="gh_48776be7ef17"){//微信原始id
return "小i机器人欢迎您,作者主页地址:www.kaedeen.com。小i机器人不断学习中,欢迎各种调戏…/:,@-D";//欢迎语
}
}
}
//sae打日志封装函数
function sae_log($msg)
{
sae_set_display_errors(false);//关闭信息输出
if (is_array($msg))
{
$msg = implode(",", $msg);
}
sae_debug("[BEGIN]".$msg."[END]");//记录日志
sae_set_display_errors(true);//记录日志后再打开信息输出,否则会阻止正常的错误信息的显示
}
?>