Exemplo n.º 1
0
/**
 *  远程获取数据函数
 *  例如:DownHost($url, '', 'GET', NULL, NULL, $user, $pass);
 *
 * @access    public
 * @param     string   站点地址
 * @param     string   附加数据
 * @param     string   请求方法,GET POST方式
 * @param     string   显示agent
 * @param     int      端口号
 * @param     string   用户名,便于Authorization: Basic请求
 * @param     string   密码
 * @param     int      超限时间
 * @return    array
 */
function DownHost($host, $data = '', $method = 'GET', $showagent = null, $port = null, $user = '', $pwd = '', $timeout = 30)
{
    $reval = array();
    $parse = @parse_url($host);
    if (empty($parse)) {
        return false;
    }
    if ((int) $port > 0) {
        $parse['port'] = $port;
    } elseif (!isset($parse['port'])) {
        $parse['port'] = '80';
    }
    if (!empty($user)) {
        $parse['user'] = $user;
    }
    if (!empty($pwd)) {
        $parse['pass'] = $pwd;
    }
    $parse['host'] = str_replace(array('http://', 'https://'), array('', 'ssl://'), "{$parse['scheme']}://") . $parse['host'];
    if (!($fp = @fsockopen($parse['host'], $parse['port'], $errnum, $errstr, $timeout))) {
        return false;
    }
    $method = strtoupper($method);
    $wlength = $wdata = $responseText = '';
    $parse['path'] = str_replace(array('\\', '//'), '/', $parse['path']) . "?{$parse['query']}";
    $headers = '';
    $agent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2)';
    $headers .= "User-Agent: " . $agent . "\r\n";
    $accept = 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*';
    $headers .= "Accept: " . $accept . "\r\n";
    if (!empty($parse['user']) || !empty($parse['pass'])) {
        $headers .= "Authorization: Basic " . base64_encode($parse['user'] . ":" . $parse['pass']) . "\r\n";
    }
    $content_type = '';
    if (!empty($content_type)) {
        $headers .= "Content-type: {$content_type}";
        if ($content_type == "multipart/form-data") {
            $headers .= "; boundary=dede" . md5(uniqid(microtime()));
        }
        $headers .= "\r\n";
    }
    if ($method == 'GET') {
        $separator = $parse['query'] ? '&' : '';
        substr($data, 0, 1) == '&' && ($data = substr($data, 1));
        $parse['path'] .= $separator . $data;
    } elseif ($method == 'POST') {
        $wlength = "Content-length: " . strlen($data) . "\r\n";
        $wdata = $data;
    }
    $write = "{$method} {$parse['path']} HTTP/1.0\r\nHost: {$parse['host']}\r\n{$wlength}{$headers}\r\n{$wdata}";
    // dump($write);
    @fwrite($fp, $write, strlen($write));
    while ($currentHeader = fgets($fp, 4096)) {
        if ($currentHeader == "\r\n") {
            break;
        }
        // 根据返回信息判断是否跳转
        if (preg_match("/^(Location:|URI:)/i", $currentHeader)) {
            preg_match("/^(Location:|URI:)[ ]+(.*)/i", chop($currentHeader), $matches);
            if (!preg_match("|\\:\\/\\/|", $matches[2])) {
                $_redirectaddr = $parse["scheme"] . "://" . $parse['host'] . ":" . $parse['port'];
                if (!preg_match("|^/|", $matches[2])) {
                    $_redirectaddr .= "/" . $matches[2];
                } else {
                    $_redirectaddr .= $matches[2];
                }
            } else {
                $_redirectaddr = $matches[2];
            }
            return DownHost($_redirectaddr, $data, $method, $showagent, $port, $user, $pwd, $timeout);
        }
        $reval['status'] = '';
        if (preg_match("|^HTTP/|", $currentHeader)) {
            if (preg_match("|^HTTP/[^\\s]*\\s(.*?)\\s|", $currentHeader, $status)) {
                $reval['status'] = $status[1];
            }
        }
    }
    $reval['results'] = '';
    do {
        $_data = fread($fp, 500000);
        if (strlen($_data) == 0) {
            break;
        }
        $reval['results'] .= $_data;
    } while (true);
    @fclose($fp);
    return $reval;
}
Exemplo n.º 2
0
/**
 *   获取远程接口数据
 *
 * @access    public
 * @param     string
 * @return    string
 */
function GetAnalyticsDate($url)
{
    global $cfg_bshare, $json, $cfg_soft_lang;
    $user = $cfg_bshare['user'];
    $pass = $cfg_bshare['pwd'];
    $results = DownHost($url, '', 'GET', NULL, NULL, $user, $pass);
    try {
        $result = $json->decode($results['results']);
    } catch (Exception $e) {
        trigger_error("Server Error:" . $result);
    }
    //if ($cfg_soft_lang == 'gb2312')
    //{
    $result = AutoCharset($result, 'utf-8', 'gbk');
    //}
    return $result;
}