1

我需要一个脚本来处理 HTTP 302 响应,并返回给定 URL 的真实链接。例如http://185.20.36.231:8080/stream/?channel=nettvinfo&stream=1mb&sp=pass1@&b=3&u=user1它应该返回http://185.20.36.233:8080/stream/?fw=true&route=at1_1&rule=null&sp=nettvplus&channel=nettvinfoint&stream=1mb&u=user1&b=3&player=http

我尝试了以下脚本,但由于某种原因,它不适用于这个特定的 URL。请帮忙。

    <?php
function getOriginalURL($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $result = curl_exec($ch);
    $httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    // if it's not a redirection (3XX), move along
    if ($httpStatus < 300 || $httpStatus >= 400)
        return $url;

    // look for a location: header to find the target URL
    if(preg_match('/location: (.*)/i', $result, $r)) {
        $location = trim($r[1]);

        // if the location is a relative URL, attempt to make it absolute
        if (preg_match('/^\/(.*)/', $location)) {
            $urlParts = parse_url($url);
            if ($urlParts['scheme'])
                $baseURL = $urlParts['scheme'].'://';

            if ($urlParts['host'])
                $baseURL .= $urlParts['host'];

            if ($urlParts['port'])
                $baseURL .= ':'.$urlParts['port'];

            return $baseURL.$location;
        }

        return $location;
    }
    return $url;
}
    $newurl=getOriginalURL("http://185.20.36.231:8080/stream/?channel=nettvinfo&stream=1mb&sp=pass1@&b=3&u=user1");
    print($newurl);
?>

编辑>

@farmer1992 curl_setopt($ch, CURLOPT_VERBOSE, true); curl_setopt($ch, CURLOPT_STDERR, fopen('log2.txt', 'a+'));

日志文件

* About to connect() to 185.20.36.231 port 8080 (#4) * Trying 185.20.36.231... * Adding handle: conn: 0x29a98d0 * Adding handle: send: 0 * Adding handle: recv: 0 * Curl_addHandleToPipeline: length: 1 * - Conn 4 (0x29a98d0) send_pipe: 1, recv_pipe: 0 * Connection timed out * Failed connect to 185.20.36.231:8080; Connection timed out * Closing connection 4

4

0 回答 0