/** * 加载缓存数据 */ protected static function loadProCache($moduleName, $param = array(), $num = 0) { if (!$moduleName) { return false; } $paramStr = ""; if ($param) { foreach ($param as $k => $v) { $paramStr .= "&{$k}=" . urlencode($v); } } $url = "http://zcloud.xxx.com/proMongo?modName={$moduleName}{$paramStr}"; $data = API_Http::curlPage(array('url' => $url, 'timeout' => 1)); #远程请求数据 if ($data) { $data = API_JsonDecode::decode($data); } return $data; }
/** * 利用curl的形式获得页面请求 请用这个函数取代file_get_contents */ public static function curlPage($paramArr) { return API_Http::curlPage($paramArr); }
function _connect(&$fp) { if (!empty($this->proxy_host) && !empty($this->proxy_port)) { $this->_isproxy = true; $host = $this->proxy_host; $port = $this->proxy_port; } else { $host = $this->host; $port = $this->port; } $this->status = 0; if ($fp = fsockopen($host, $port, $errno, $errstr, $this->_fp_timeout)) { // socket connection succeeded return true; } else { //如果连接失败,尝试不适用代理重新连接一次 if ($this->_isproxy) { $this->proxy_host = API_Http::curlPage(array('url' => "http://118.67.120.127/cgi-bin/socket_adsl_restart_control.cgi?file=1", 'timeout' => 2)); $this->proxy_port = "8088"; $host = $this->proxy_host; $port = $this->proxy_port; if ($fp = fsockopen($host, $port, $errno, $errstr, $this->_fp_timeout)) { return true; } else { // socket connection failed $this->status = $errno; switch ($errno) { case -3: $this->error = "#socket creation failed (-3)"; case -4: $this->error = "#dns lookup failure (-4)"; case -5: $this->error = "#connection refused or timed out (-5)"; default: $this->error = "#connection failed (" . $errno . ")"; } return false; } } // socket connection failed $this->status = $errno; switch ($errno) { case -3: $this->error = "socket creation failed (-3)"; case -4: $this->error = "dns lookup failure (-4)"; case -5: $this->error = "connection refused or timed out (-5)"; default: $this->error = "connection failed (" . $errno . ")"; } return false; } }
/** * 获得某个标签下面的用户列表 * */ public static function getTagUserList($paramArr) { $options = array('tagId' => ''); if (is_array($paramArr)) { $options = array_merge($options, $paramArr); } extract($options); if (!$tagId) { return false; } $assessToken = API_Item_Open_WeixinQy::getAccessToken(); $data = API_Http::curlPage(array('url' => 'https://qyapi.weixin.qq.com/cgi-bin/tag/get?access_token=' . $assessToken . '&tagid=' . $tagId, 'timeout' => 3)); $data = api_json_decode($data); return $data; }
/** * 获得手机号信息 */ public static function getMobileArea($paramArr) { $options = array('mobile' => ''); if (is_array($paramArr)) { $options = array_merge($options, $paramArr); } extract($options); if (!$mobile || !preg_match('/^1[3458][0-9]{9}$/', $mobile)) { return false; } $outArr = array(); #首先尝试从db中查询 /** add by cui*/ $db = API_Db_Ip::instance(); $sql = "select * from mobile_data where mno = '{$mobile}' limit 1"; $re = $db->getRow($sql); if ($re) { $outArr = array('卡号归属地' => $re["area"], '卡类型' => $re["card"], '区号' => $re["areano"], '邮编' => $re["zip"]); return $outArr; } $outData = array(); #尝试接口1 $url = "http://www.ip138.com:8080/search.asp?action=mobile&mobile={$mobile}"; $dom = LJL_Api::run("Service.FetchHtml.getHtmlOrDom", array('url' => $url, 'charset' => 'gbk', 'timeout' => 3, 'getDom' => 1)); if ($dom) { $trs = $dom->find(".tdc"); if ($trs) { foreach ($trs as $tr) { $tds = $tr->find("td"); if (!isset($tds[0])) { continue; } $name = $tds[0]->innertext; $value = $tds[1]->innertext; $name = str_replace(array(" ", " "), "", strip_tags($name)); $name = iconv('gbk', 'utf-8', $name); $value = iconv('gbk', 'utf-8', $value); if (!in_array($name, array('卡类型', '区号', '邮编', '卡号归属地'))) { continue; } $value = str_replace(" ", "", strip_tags($value)); if ($name == '邮编') { $value = (int) $value; } if ($name == '卡号归属地') { $value = str_replace(" ", "|", $value); } if ($name && $value) { $outData[$name] = $value; } } } } if (!$outData) { #尝试接口2 $url = 'http://www.youdao.com/smartresult-xml/search.s?type=mobile&q=' . $mobile; $data = API_Http::curlPage(array('url' => $url, 'timeout' => 1)); if (preg_match("#<location>(.+)</location>#Ui", $data, $m)) { $outData['卡号归属地'] = trim(trim($m[1]), '|'); } } #将取得的数据记录到数据库,方便下次直接使用 /*bycui*/ if ($outData) { $sql = "insert into mobile_data(mno,area,card,areano,zip,tm)\r\n values('{$mobile}','" . $outData['卡号归属地'] . "','" . $outData['卡类型'] . "','" . $outData['区号'] . "','" . $outData['邮编'] . "','" . SYSTEM_TIME . "') "; $db->query($sql); } return $outData; }
/** * 获得一个代理IP和端口 */ public static function getProxyData($paramArr) { $options = array('retryCnt' => 5); if (is_array($paramArr)) { $options = array_merge($options, $paramArr); } extract($options); $defaultPort = 8088; $outArr = false; $tmp = 1; #如果已经取过至少3个代理,就从这三个中随机获得一个 if (self::$proxySaveArr && count(self::$proxySaveArr) >= 3) { $tmpPidx = array_rand(self::$proxySaveArr, 1); $ip = self::$proxySaveArr[$tmpPidx]; $outArr = array('ip' => $ip, 'port' => $defaultPort); } else { #尝试获得代理 while ($tmp < $retryCnt) { #获得一个有用的 $proxy = API_Http::curlPage(array('url' => "http://14.32.120.127/cgi-bin/socket_adsl_restart_control.cgi?file=1", 'timeout' => 2)); if (!$proxy) { continue; } #尝试这个代理是否可用 $fp = @fsockopen($proxy, $defaultPort, $errno, $errstr, 1); if ($fp) { self::$proxySaveArr[] = $proxy; $outArr = array('ip' => $proxy, 'port' => $defaultPort); fclose($fp); break; } else { #echo "CONNECT ERROR: ".$errno; } $tmp++; } } return $outArr; }
/** * 获取数据 */ private static function fetchData($paramArr) { $options = array('apiName' => '', 'data' => false, 'format' => 'json'); if (is_array($paramArr)) { $options = array_merge($options, $paramArr); } extract($options); //系统级参数部分 $reqData = array('app_key' => self::$_APPKEY, 'app_secret' => self::$_APPSECRET, 'api_name' => $apiName, 'format' => $format); if ($data) { $reqData = array_merge($data, $reqData); } $url = self::$_APPCALLBURL . "?" . http_build_query($reqData); # $responseData = API_Http::curlPage(array('url' => $url, 'timeout' => 30)); if ($responseData && $responseData != "null") { return api_json_decode($responseData); } else { return false; } }
<?php require '/www/8hao/html/tool/test_su/Http.php'; $url1 = 'http://pad.zol.com.cn/slide/531/5310254_1.html#p=1'; // 组图 //$url1 = 'http://ask.zol.com.cn/cell_phone/'; //$url1 = 'http://news.zol.com.cn/'; //$res1 = get_headers($url1); //$res1 = curlHttpCode(array('url'=>$url1)); //var_dump($res1);exit('58_1'); $url1 = 'http://www.zol.com.cn/'; $paramArr = array('url' => $url1, 'timeout' => 5, 'recErrLog' => 0, 'reConnect' => 0, 'keepAlive' => 0); $htmlCode = API_Http::curlPage($paramArr); $pattern = '@<a href=\\"([^#\\"\']*?)">.*?<\\/a>@'; preg_match_all($pattern, $htmlCode, $res1); if ($res1) { # 将非200的链接放到这个数组中 $badData = array(); foreach ($res1[1] as $k1 => $v1) { if ($k1 > 200) { break; } $httpCode = curlHttpCode(array('url' => $v1)); if ($httpCode != 200 || $httpCode != 0) { $badData[] = $res1[0][$k1]; } } } var_dump($badData); exit; $httpCode = curlHttpCode(array('url' => $url1));
/** * 请求天气网数据 */ private static function getCurlData($paramArr) { $options = array('url' => '', 'retry' => 3); if (is_array($paramArr)) { $options = array_merge($options, $paramArr); } extract($options); if (!$url) { return null; } #最多通信3次 $requestFlag = 0; do { $json = API_Http::curlPage(array('url' => $url, 'timeout' => 5)); $requestFlag++; } while (!$json && $requestFlag < $retry); if (!$json || $json == 'data error') { return null; } return $json; }