protected function sendAndReceive($request) { $f = new SaeFetchurl(); $cookie = $this->getCookie(); if ($cookie != '') { $f->setHeader("Cookie", $cookie); } if ($this->keepAlive) { $f->setHeader("Connection", "keep-alive"); $f->setHeader("Keep-Alive", $this->keepAliveTimeout); } else { $f->setHeader("Connection", "close"); } foreach ($this->header as $name => $value) { $f->setHeader($name, $value); } $f->setMethod("post"); $f->setPostData($request); $f->setConnectTimeout($this->timeout); $f->setSendTimeout($this->timeout); $f->setReadTimeout($this->timeout); $response = $f->fetch($this->url); if ($f->errno()) { throw new Exception($f->errno() . ": " . $f->errmsg()); } $http_response_header = $f->responseHeaders(false); $this->setCookie($http_response_header); return $response; }
function getName($stuID, $pwd) { $f = new SaeFetchurl(); $f->setMethod('post'); $logindata['IPT_LOGINUSERNAME'] = $stuID; $logindata['IPT_LOGINPASSWORD'] = $pwd; $f->setPostData($logindata); $f->setAllowRedirect(false); $f->fetch('http://222.30.60.9/meol/homepage/common/login.jsp'); if ($f->errno()) { $cookies = $f->responseCookies(false); $f->setCookies($cookies); $content = $f->fetch('http://222.30.60.9/meol/welcomepage/student/index.jsp'); $name = substr($content, strlen('<li>' . ' ') + strpos($content, '<li>') + 3, (strlen($content) - strpos($content, '</li>')) * -1); return iconv('GBK', 'UTF-8//IGNORE', $name); } else { return false; } }
public function invoke($functionName, &$arguments = array(), $byRef = false, $resultMode = HproseResultMode::Normal) { $stream = new HproseStringStream(HproseTags::TagCall); $hproseWriter = new HproseWriter($stream); $hproseWriter->writeString($functionName, false); if (0 < count($arguments) || $byRef) { $hproseWriter->reset(); $hproseWriter->writeList($arguments, false); } if ($byRef) { $hproseWriter->writeBoolean(true); } $stream->write(HproseTags::TagEnd); $request = $stream->toString(); if ($this->filter) { $request = $this->filter->outputFilter($request); } $stream->close(); $f = new SaeFetchurl(); $cookie = $this->getCookie(); if ($cookie != "") { $f->setHeader("Cookie", $cookie); } if ($this->keepAlive) { $f->setHeader("Connection", "keep-alive"); $f->setHeader("Keep-Alive", $this->keepAliveTimeout); } else { $f->setHeader("Connection", "close"); } foreach ($this->header as $name => $value) { $f->setHeader($name, $value); } $f->setMethod("post"); $f->setPostData($request); $f->setConnectTimeout($this->timeout); $f->setSendTimeout($this->timeout); $f->setReadTimeout($this->timeout); $response = $f->fetch($this->url); if ($f->errno()) { throw new HproseException($f->errno() . ": " . $f->errmsg()); } $http_response_header = $f->responseHeaders(false); $this->setCookie($http_response_header); if ($this->filter) { $response = $this->filter->inputFilter($response); } if ($resultMode == HproseResultMode::RawWithEndTag) { return $response; } if ($resultMode == HproseResultMode::Raw) { return substr($response, 0, -1); } $stream = new HproseStringStream($response); $hproseReader = new HproseReader($stream); $result = NULL; $error = NULL; while (($tag = $hproseReader->checkTags(array(HproseTags::TagResult, HproseTags::TagArgument, HproseTags::TagError, HproseTags::TagEnd))) !== HproseTags::TagEnd) { switch ($tag) { case HproseTags: if ($resultMode == HproseResultMode::Serialized) { $result = $hproseReader->readRaw()->toString(); } else { $hproseReader->reset(); $result =& $hproseReader->unserialize(); } break; case HproseTags: $hproseReader->reset(); $args =& $hproseReader->readList(); for ($i = 0; $i < count($arguments); $i++) { $arguments[$i] =& $args[$i]; } break; case HproseTags: $hproseReader->reset(); $error = new HproseException($hproseReader->readString()); break; } } if (!is_null($error)) { throw $error; } return $result; }
//建立抓取对象 $fetch = new SaeFetchurl(); //通过新浪天气接口查询天气链接 //example http://php.weather.sina.com.cn/xml.php?city=%B1%B1%BE%A9&password=DJOYnieT8234jlsK&day=0 $weather_api_url = "http://php.weather.sina.com.cn/xml.php?"; //改变编码 $city = $_POST['city']; //echo $city; $city = urlencode(iconv("UTF-8", "GBK", $city)); $city = "city=" . $city; //echo $city; //抓取当天天气 $weather = $fetch->fetch($weather_api_url . $city . "&password=DJOYnieT8234jlsK&day=0"); //echo $weather_api_url.$city."&password=DJOYnieT8234jlsK&day=0"; //抓取到天气 if ($fetch->errno() == 0 && strstr($weather, "Weather")) { //正则表达式获取数据 preg_match_all("/\\<city\\>(.*?)\\<\\/city\\>/", $weather, $w_city); preg_match_all("/\\<status2\\>(.*?)\\<\\/status2\\>/", $weather, $w_status2); preg_match_all("/\\<status1\\>(.*?)\\<\\/status1\\>/", $weather, $w_status1); preg_match_all("/\\<temperature2\\>(.*?)\\<\\/temperature2\\>/", $weather, $w_temperature2); preg_match_all("/\\<temperature1\\>(.*?)\\<\\/temperature1\\>/", $weather, $w_temperature1); preg_match_all("/\\<direction1\\>(.*?)\\<\\/direction1\\>/", $weather, $w_direction1); preg_match_all("/\\<direction2\\>(.*?)\\<\\/direction2\\>/", $weather, $w_direction2); preg_match_all("/\\<power1\\>(.*?)\\<\\/power1\\>/", $weather, $w_power1); preg_match_all("/\\<power2\\>(.*?)\\<\\/power2\\>/", $weather, $w_power2); //如果天气变化一致 if ($w_status2 == $w_status1) { $w_status = $w_status2[1][0]; } else { $w_status = $w_status2[1][0] . "转" . $w_status1[1][0];
function sign($cookie, $tiebaname, $fid, $urlname) { $f = new SaeFetchurl(); $f->setMethod("post"); $f->setCookie("BDUSS", $cookie); $tbs = tbs($cookie); curl_get2("http://tieba.baidu.com/f/user/json_userinfo", $cookie, $rescookie); $cookieT = "TIEBA_USERTYPE=" . $rescookie['TIEBA_USERTYPE'] . ";TIEBAUID=" . $rescookie['TIEBAUID'] . ";BAIDUID=" . $rescookie['BAIDUID'] . "=1;BDUSS=" . $cookie; $poststr = $cookieT . "fid=" . $fid . "from=tiebakw=" . $tiebaname . "net_type=1tbs=" . $tbs; $sign = md5($poststr . "tiebaclient!!!"); $poststr = $cookieT . "&fid=" . $fid . "&from=tieba&kw=" . $urlname . "&net_type=1&tbs=" . $tbs . "&sign=" . $sign; $f->setPostData($poststr); $text = $f->fetch("http://c.tieba.baidu.com/c/c/forum/sign"); if ($f->errno() == 0) { return $text; } else { return false; } }
if ($res[$i]) { array_push($data, array('title' => $res[$i]['title'], 'note' => $res[$i]['description'], 'link' => 'http://xiaouri.sinaapp.com/api/ghoststory_read.php?url=' . $res[$i]['link'])); } } } elseif (preg_match('#^张震\\s?(\\d)?#i', $content, $matches)) { if (isset($matches[1])) { $data = zhangzhen((int) $matches[1]); } else { $data = zhangzhen(rand(0, 49)); } } } } elseif ($type == 'image') { $picurl = $xml->PicUrl; $img_data = $f->fetch($picurl); if ($f->errno() == 0) { $img_name = $openid . date(YmdHis) . '.jpg'; $s->write('wephoto', $img_name, $img_data); $picUrl = $s->getUrl('wephoto', $img_name); $mysql->runSql("UPDATE moments SET photo='{$picUrl}' WHERE FromUserName='******'"); $data = '你的图片已上传成功!'; $data .= "\n" . picRec($picurl); } else { $data = 'failed'; } } elseif ($type == 'voice') { $data = '小u听不懂人话的说。。'; } elseif ($type == 'event') { $event = strtolower($xml->Event); if ($event == 'subscribe') { $data = $xiaoU;
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)) { /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection, the best way is to check the validity of xml by yourself */ libxml_disable_entity_loader(true); $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $keyword = trim($postObj->Content); $time = time(); //消息类型 $form_MsgType = $postObj->MsgType; //文字消息模板 $textTpl = "<xml>\n\t\t\t\t\t\t\t<ToUserName><![CDATA[%s]]></ToUserName>\n\t\t\t\t\t\t\t<FromUserName><![CDATA[%s]]></FromUserName>\n\t\t\t\t\t\t\t<CreateTime>%s</CreateTime>\n\t\t\t\t\t\t\t<MsgType><![CDATA[%s]]></MsgType>\n\t\t\t\t\t\t\t<Content><![CDATA[%s]]></Content>\n\t\t\t\t\t\t\t<FuncFlag>0</FuncFlag>\n\t\t\t\t\t\t\t</xml>"; //事件消息 if ($form_MsgType == "event") { //获取某一特定事件,这里是订阅事件 $form_event = $postObj->Event; //如果是订阅事件 if ($form_event == "subscribe") { //回复欢迎文字消息 $msgType = "text"; $contentStr = "感谢您关注江大计算机考研![愉快]\n回复数据结构可以获得数据结构有关资料\n回复复试可以获得复试资料\n回复导师可以查看导师信息\n发送位置信息可以获取天气预报\n还可以给我发图片\n有意见可以回复我哦\n平台建设中请见谅![玫瑰]\n by XiJiaxiang"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, $contentStr); echo $resultStr; exit; } } //非文字消息,这里是图片消息 if ($form_MsgType == "image") { //获取图片url $from_PicUrl = $postObj->PicUrl; //创建图片名称 $filename = $fromUsername . date("YmdHis") . ".jpg"; //抓取的图片类, 新浪云平台提供 $saefetch = new SaeFetchurl(); //抓取图片 $PicResource = $saefetch->fetch($from_PicUrl); //没有错误 if ($saefetch->errno() == 0) { //use sinacloud\sae\Storage as Storage; //使用全局变量global 新浪Storage提供。 global $storage; $storage = new Storage(); //写入图片,第一个参数是文件,第二个是bucket(新浪方面)。第三个存储路径 $storage->putObject($PicResource, "********", "wexinPic/" . $filename); //提示上传成功,返回消息给用户 $msgType = "text"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, "收到图片了哦"); echo $resultStr; exit; } else { //提示失败 $msgType = "text"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, "图片上传失败"); echo $resultStr; exit; } } //非文字消息,这里是地理位置信息 if ($form_MsgType == "location") { $from_Location_X = $postObj->Location_X; //经度 echo $from_Location_X; $from_Location_Y = $postObj->Location_Y; //纬度 echo $from_Location_Y; $from_Location_Scale = $postObj->Scale; //地图缩放信息 $from_Location_Label = $postObj->Label; //地址信息 //使用百度地图api解析地址 //example //http://api.map.baidu.com/geocoder/v2/?ak=E4805d16520de693a3fe707cdc962045&callback=renderReverse&location=39.983424,1//16.322987&output=json&pois=1 $map_api_url = "http://api.map.baidu.com/geocoder/v2/?"; //坐标类型 $map_coord_type = "coordtype=wgs84ll"; $ak = "ak=****************************"; //建立抓取对象 $fetch = new SaeFetchurl(); //抓取返回的xml文件,也可以利用json $geocoder = $fetch->fetch($map_api_url . $ak . "&callback=renderReverse&location=" . $from_Location_X . "," . $from_Location_Y . "&output=xml&pois=1"); //抓取成功 if ($fetch->errno() == 0) { //正则表达式匹配城市 preg_match_all("/\\<city\\>(.*?)\\<\\/city\\>/", $geocoder, $city); //将市区县替换为空 例如北京市=》北京 $city = str_replace(array("市", "区", "县"), array("", "", ""), $city[1][0]); echo $city; //通过新浪天气接口查询天气链接 //example http://php.weather.sina.com.cn/xml.php?city=%B1%B1%BE%A9&password=DJOYnieT8234jlsK&day=0 $weather_api_url = "http://php.weather.sina.com.cn/xml.php?"; //改变编码 $city = "city=" . urlencode(iconv("UTF-8", "GBK", $city)); echo $city; //抓取当天天气 $weather = $fetch->fetch($weather_api_url . $city . "&password=DJOYnieT8234jlsK&day=0"); //抓取到天气 if ($fetch->errno() == 0 && strstr($weather, "Weather")) { //正则表达式获取数据 preg_match_all("/\\<city\\>(.*?)\\<\\/city\\>/", $weather, $w_city); preg_match_all("/\\<status2\\>(.*?)\\<\\/status2\\>/", $weather, $w_status2); preg_match_all("/\\<status1\\>(.*?)\\<\\/status1\\>/", $weather, $w_status1); preg_match_all("/\\<temperature2\\>(.*?)\\<\\/temperature2\\>/", $weather, $w_temperature2); preg_match_all("/\\<temperature1\\>(.*?)\\<\\/temperature1\\>/", $weather, $w_temperature1); preg_match_all("/\\<direction1\\>(.*?)\\<\\/direction1\\>/", $weather, $w_direction1); preg_match_all("/\\<direction2\\>(.*?)\\<\\/direction2\\>/", $weather, $w_direction2); preg_match_all("/\\<power1\\>(.*?)\\<\\/power1\\>/", $weather, $w_power1); preg_match_all("/\\<power2\\>(.*?)\\<\\/power2\\>/", $weather, $w_power2); //如果天气变化一致 if ($w_status2[1][0] == $w_status1[1][0]) { $w_status = $w_status2[1][0]; } else { $w_status = $w_status2[1][0] . "转" . $w_status1[1][0]; } $weather_res = array($w_city[1][0] . "天气预报", "天气:" . $w_status, "气温:" . $w_temperature2[1][0] . "℃-" . $w_temperature1[1][0] . "℃", "风向:" . $w_direction1[1][0], "风力:" . $w_power1[1][0] . "级"); $weather_res = implode("\n", $weather_res); //消息回复 $msgType = "text"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $weather_res); echo $resultStr; exit; } else { echo "抓取天气信息失败"; exit; } } } //回复关键字 if (!empty($keyword)) { switch ($keyword) { case "数据结构": $msgType = "text"; $contentStr = "http://pan.baidu.com/s/1kUr9NRD"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; break; case "复试": $msgType = "text"; $contentStr = "http://pan.baidu.com/s/1c1sT2Q8"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; break; case "author": case "作者": $msgType = "text"; $contentStr = "qq:1163451848"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; break; //考研时间 //考研时间 case "考研时间": $resultStr = "<xml>\n\t\t\t\t\t\t\t\t\t<ToUserName><![CDATA[" . $fromUsername . "]]></ToUserName>\n\t\t\t\t\t\t\t\t\t<FromUserName><![CDATA[" . $toUsername . "]]></FromUserName>\n\t\t\t\t\t\t\t\t\t<CreateTime>" . time() . "</CreateTime>\n\t\t\t\t\t\t\t\t\t<MsgType><![CDATA[news]]></MsgType>\n\t\t\t\t\t\t\t\t\t<ArticleCount>1</ArticleCount>\n\t\t\t\t\t\t\t\t\t<Articles>\n\t\t\t\t\t\t\t\t\t<item>\n\t\t\t\t\t\t\t\t\t\t<Title><![CDATA[考研时间]]></Title> \n\t\t\t\t\t\t\t\t\t\t<Description><![CDATA[test only1]]></Description>\n\t\t\t\t\t\t\t\t\t\t<PicUrl><![CDATA[http://xijiaxiangwexin-xijiaxiangwexin.stor.sinaapp.com/pic%2Ftime.jpg]]></PicUrl>\n\t\t\t\t\t\t\t\t\t\t<Url><![CDATA[http://xijiaxiangwexin.sinaapp.com/wexin/kaoyantime.php]]></Url>\n\t\t\t\t\t\t\t\t\t</item>\n\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t</Articles>\n\t\t\t\t\t\t\t\t\t<FuncFlag>0</FuncFlag>\n\t\t\t\t\t\t\t\t\t</xml> "; echo $resultStr; break; case "天气": $resultStr = "<xml>\n\t\t\t\t\t\t\t\t\t<ToUserName><![CDATA[" . $fromUsername . "]]></ToUserName>\n\t\t\t\t\t\t\t\t\t<FromUserName><![CDATA[" . $toUsername . "]]></FromUserName>\n\t\t\t\t\t\t\t\t\t<CreateTime>" . time() . "</CreateTime>\n\t\t\t\t\t\t\t\t\t<MsgType><![CDATA[news]]></MsgType>\n\t\t\t\t\t\t\t\t\t<ArticleCount>1</ArticleCount>\n\t\t\t\t\t\t\t\t\t<Articles>\n\t\t\t\t\t\t\t\t\t<item>\n\t\t\t\t\t\t\t\t\t\t<Title><![CDATA[天气预报]]></Title> \n\t\t\t\t\t\t\t\t\t\t<Description><![CDATA[test only]]></Description>\n\t\t\t\t\t\t\t\t\t\t<PicUrl><![CDATA[http://xijiaxiangwexin-xijiaxiangwexin.stor.sinaapp.com/pic%2Fweather.jpg]]></PicUrl>\n\t\t\t\t\t\t\t\t\t\t<Url><![CDATA[http://xijiaxiangwexin.sinaapp.com/wexin/weather.php]]></Url>\n\t\t\t\t\t\t\t\t\t</item>\n\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t</Articles>\n\t\t\t\t\t\t\t\t\t<FuncFlag>0</FuncFlag>\n\t\t\t\t\t\t\t\t\t</xml> "; echo $resultStr; break; case "导师": $msgType = "text"; $contentStr = "1. https://github.com/TutorWikiUJS/TutorWiki" . "\n" . "2. http://pan.baidu.com/s/1i3PrVPN"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; break; default: //引入turling机器人 $msgType = "text"; //显示一样的消息 $contentStr = $keyword; $ch = curl_init(); $url = 'http://www.tuling123.com/openapi/api?key=*****************&info=' . $keyword . '&userid=' . $fromUsername; curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 执行HTTP请求 curl_setopt($ch, CURLOPT_URL, $url); $res = curl_exec($ch); $resarr = json_decode($res, true); switch ($resarr["code"]) { case "100000": echo "text"; $string = str_replace("<br>", "\n", $resarr["text"]); break; case "200000": echo "url"; $string = $resarr["text"] . "\n" . "详细请点击:" . $resarr["url"]; break; case "302000": echo "news"; echo $resarr["text"] . "\n"; for ($i = 0; $i < count($resarr["list"]); $i++) { $string = "-----------------------" . "\n"; $string .= $resarr["list"][$i]["article"] . "\n"; $string .= $resarr["list"][$i]["source"] . "\n"; $string .= $resarr["list"][$i]["detailurl"] . "\n"; } break; //case "305000":echo "trains";break; //case "306000":echo "flights";break; //case "305000":echo "trains";break; //case "306000":echo "flights";break; case "308000": echo "cai pu"; $string = $resarr["text"] . "\n"; $string .= "-----------------------" . "\n"; $string .= "<" . $resarr["list"]["0"]["name"] . ">\n"; $string .= "配料:" . $resarr["list"]["0"]["info"] . "\n"; $string .= "详细请点击:" . $resarr["list"]["0"]["detailurl"] . "\n"; break; case "40001": $string = "sorry,sth wrong"; echo "1 turling_err"; break; case "40002": $string = "sorry,sth wrong"; echo "2 turling_err null"; break; case "40004": $string = "sorry,sth wrong"; echo "3 turling_err full"; break; case "40007": $string = "sorry,sth wrong"; echo "4 turling_err "; break; default: $string = "sorry,sth wrong"; echo "turling_err"; break; } $contentStr = $string; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; } } else { echo "something i didn't know :)"; } } else { echo ""; exit; } }
if ($form_MsgType == "location") { // 获取地理消息信息,经纬度,地图缩放比例,地址 $from_Location_X = $postObj->Location_X; $from_Location_Y = $postObj->Location_Y; $from_Location_Scale = $postObj->Scale; $from_Location_Label = $postObj->Label; // 地址解析使用百度地图API的链接 $map_api_url = "http://api.map.baidu.com/geocoder?"; // 坐标类型 $map_coord_type = "&coord_type=wgs84"; // 建立抓取对象 $f = new SaeFetchurl(); // 抓取百度地址解析 $geocoder = $f->fetch($map_api_url . $map_coord_type . "&location=" . $from_Location_X . "," . $from_Location_Y); // 如果抓取地址解析成功 if ($f->errno() == 0) { // 匹配出城市 preg_match_all("/\\<city\\>(.*?)\\<\\/city\\>/", $geocoder, $city); $city = str_replace(array("市", "县", "区"), array("", "", ""), $city[1][0]); // 通过新浪天气接口查询天气的链接 $weather_api_url = "http://php.weather.sina.com.cn/xml.php?password=DJOYnieT8234jlsK"; // 城市名转字符编码 $city = "&city=" . urlencode(iconv("UTF-8", "GBK", $city)); // 查询当天 $day = "&day=0"; // 抓取天气 $weather = $f->fetch($weather_api_url . $city . $day); // 如果抓取到天气 if ($f->errno() == 0 && strstr($weather, "Weather")) { // 用正则表达式获取数据 preg_match_all("/\\<city\\>(.*?)\\<\\/city\\>/", $weather, $w_city);