- <?php
- /**
- * wechat php test
- */
- header('Content-Type:text/html;charset=utf8');
- date_default_timezone_set('RPC');
- //define your token
- define("TOKEN", "2snH21PBqF7UK");//自定义
- $wechatObj = new wechatCallbackapiTest();
- //$wechatObj->valid();//第一次验证token时使用
- $wechatObj->responseMsg();
- class wechatCallbackapiTest
- {
- private $keyword;
- 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;
- $this->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( $this->keyword ))
- {
- if($this->keyword == 999){
- $msgType = "text";
- $contentStr = '当您搜索:q北京遇上西雅图,系统返回的是迅雷磁力链接,请您使用电脑版迅雷或手机版迅雷,进行下载观看。';//'参数:1, 按下载数查询;参数:2, 按时间查询;参数:5, 按质量查询; 精确查询请添加双引号; 例如: q"北京遇上西雅图" 1';
- echo $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
- exit;
- }
- preg_match('#^q(.*)#', $this->keyword, $str);
- if($str[1]){
- $data = $this->getQueryParam($str[1]);
- $contents = $this->getQueryList($data);
- $resutl = $this->getQueryResult($contents);
- $link = str_replace('&','&',urldecode($resutl[1]));//组装磁力链接
- if($link){
- $msgType = "text";
- $contentStr = $link;
- echo $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
- }
- }else{
- $msgType = "text";
- $contentStr = '请您输入q进行查询,例如: q北京遇上西雅图 更多帮助请输入999';
- echo $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
- }
- }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;
- }
- }
- //返回q=查询
- function getQueryResult($contents){
- $result = array();
- preg_match('#<a onclick="fclck\\(this.href\\)" href="(.*)" title="Download via magnet-link">\\[magnet-link\\]</a>#iUs', $contents, $content);
- $result = $content;
- return $result;
- }
- //获取btdigg.org 的查询数据
- function getQueryList($data){
- $data['order'] = $data['order'] ? $data['order'] : 0;
- $data['p'] = 0;
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, 'https://btdigg.org/search?'.http_build_query($data));
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_USERAGENT, 'Google Bot');
- curl_setopt($ch, CURLOPT_TIMEOUT, 4);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
- curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
- $result = curl_exec($ch);
- curl_close($ch);
- return $result;
- }
- //获取查询参数
- function getQueryParam($str){
- $data = array();
- $string = explode(' ', $str);
- //是数组 and 最后一个数组是数字
- $last = array_pop($string);
- if(is_numeric($last)){
- $data['q'] = implode(' ', $string);
- $data['order'] = $last;
- }else{
- $data['q'] = $str;
- }
- return $data;
- }
- }
- ?>
- //该片段来自于http://www.codesnippet.cn/detail/301020136814.html
来源: http://www.codesnippet.cn/detail/301020136814.html