Exemple #1
0
function get_contents($file)
{
    // Leerer Inhalt als Default
    $content = '';
    // Erster Versuch: Datei "normal" oeffnen
    $fh = fopen($file, 'rb');
    if ($fh) {
        // Funktioniert: Datei einlesen
        if (!is_url($file)) {
            // Lokal ...
            $content = fread($fh, filesize($file));
        } else {
            // ... bzw. uebers Netz
            while (!feof($fh)) {
                $content .= fread($fh, 2048);
            }
        }
        fclose($fh);
    } elseif (is_url($file, 'http')) {
        // Funktioniert nicht und Datei ist eine HTTP-Resource (externer Zugriff wurde ggf. verhindert)
        // User & Passwort extrahieren
        $userpass = get_url_userpass($file);
        if ($userpass) {
            $file = str_replace($userpass . '@', '', $file);
        }
        if (function_exists('curl_init')) {
            // Alternativversuch mittels cURL-Lib (PHP >= 4.0.2)
            $ch = curl_init($file);
            if ($ch) {
                curl_setopt($ch, CURLOPT_HEADER, FALSE);
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
                curl_setopt($ch, CURLOPT_FORBID_REUSE, TRUE);
                curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
                curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
                curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);
                if ($userpass) {
                    curl_setopt($ch, CURLOPT_USERPWD, $userpass);
                    if (substr($file, 0, 4) == 'http') {
                        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
                    }
                }
                $content = curl_exec($ch);
                curl_close($ch);
            }
        } else {
            // Alternativversuch mittels socket connection (PHP < 4.0.2)
            $host = get_url_hostname($file);
            $port = get_url_port($file);
            $sh = fsockopen($host, $port);
            if ($sh) {
                $request = 'GET ' . get_url_filename($file, TRUE) . ' HTTP/1.0' . "\r\n";
                $request .= 'Accept: */*' . "\r\n";
                $request .= 'Host: ' . $host . ':' . $port . "\r\n";
                if ($userpass) {
                    $request .= 'Authorization: Basic ' . base64_encode($userpass) . "\r\n";
                }
                $request .= 'Connection: Close' . "\r\n";
                $request .= "\r\n";
                fputs($sh, $request);
                while (!feof($sh)) {
                    $content .= fread($sh, 2048);
                }
                fclose($sh);
                $header = substr($content, 0, strpos($content, "\r\n\r\n"));
                $redirect = strpos($header, 'Location:');
                if ($redirect !== FALSE) {
                    // Seitenweiterleitung, also neuer URL
                    $redirect = trim(substr($header, $redirect + 9, strpos($header, "\r\n", $redirect) + 2 - ($redirect + 9)));
                    $content = is_url($redirect) ? get_contents($redirect) : '';
                } else {
                    // Header abschneiden
                    $content = substr($content, strpos($content, "\r\n\r\n") + 4);
                }
            }
        }
    }
    return $content;
}
Exemple #2
0
function http_get_contents(&$url, $size = 0)
{
    file_put_contents(get_url_filename($url), 'DefaultIcon');
    include_once dirname(dirname(__FILE__)) . '/hyp_common_func.php';
    $ht = new Hyp_HTTP_Request();
    $ht->init();
    $ht->url = $url;
    if ($size) {
        $ht->getSize = $size;
    }
    $ht->ua = 'Mozilla/5.0';
    $ht->connect_timeout = 2;
    $ht->read_timeout = 5;
    $ht->get();
    if ($size) {
        $url = $ht->url;
    }
    return $ht->rc == 404 || $ht->rc == 410 || $ht->rc > 600 || $ht->rc < 100 ? null : $ht->data;
}