【问题标题】:php download file from remote server, from download url with headers setphp 从远程服务器下载文件,从带有标题集的下载 url
【发布时间】:2010-08-29 07:10:01
【问题描述】:

我正在尝试使用位于远程服务器上的 php 下载文件,但没有运气。我尝试使用 fopen、get_file_contents,但没有任何效果。

我传入了一个下载 URL,它不是确切的文件位置,它是文件的“下载 url”,然后强制浏览器下载。

所以我认为这就是文件 fopen 和 file_get_contents 失败的原因,有人可以告诉我我必须做什么才能从设置了标题以强制文件下载的 url 下载文件。

非常感谢任何帮助!

【问题讨论】:

  • 请在任何情况下都不要告诉我们“没有运气”和“不起作用”的确切含义。它会带走我们猜测的乐趣!

标签: php file


【解决方案1】:

虽然从技术上讲不是重复的,但之前已经在 SO 上提出过这个问题:How to get redirecting url link with php from bit.ly

您的问题是 file_get_contents 不遵循重定向。请参阅链接的答案以获取解决方案。

【讨论】:

  • 谢谢你,你可能为我节省了几个小时的困惑和沮丧!现在很有意义。
【解决方案2】:

虽然你没有清楚地提出你的问题,但我想我知道你的意思。 试试这个取自 php.net cmets 的函数

没有对其进行测试,但它看起来不错,并且似乎遵循 html 标头重定向以及元和 javascript 重定向到文件。

<?php

/*==================================
Get url content and response headers (given a url, follows all redirections on it and returned content and response headers of final url)

@return    array[0]    content
        array[1]    array of response headers
==================================*/
function get_url( $url,  $javascript_loop = 0, $timeout = 5 )
{
    $url = str_replace( "&amp;", "&", urldecode(trim($url)) );

    $cookie = tempnam ("/tmp", "CURLCOOKIE");
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt( $ch, CURLOPT_COOKIEJAR, $cookie );
    curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
    curl_setopt( $ch, CURLOPT_ENCODING, "" );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
    curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );    # required for https urls
    curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
    curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout );
    curl_setopt( $ch, CURLOPT_MAXREDIRS, 10 );
    $content = curl_exec( $ch );
    $response = curl_getinfo( $ch );
    curl_close ( $ch );

    if ($response['http_code'] == 301 || $response['http_code'] == 302)
    {
        ini_set("user_agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1");

        if ( $headers = get_headers($response['url']) )
        {
            foreach( $headers as $value )
            {
                if ( substr( strtolower($value), 0, 9 ) == "location:" )
                    return get_url( trim( substr( $value, 9, strlen($value) ) ) );
            }
        }
    }

    if (    ( preg_match("/>[[:space:]]+window\.location\.replace\('(.*)'\)/i", $content, $value) || preg_match("/>[[:space:]]+window\.location\=\"(.*)\"/i", $content, $value) ) &&
            $javascript_loop < 5
    )
    {
        return get_url( $value[1], $javascript_loop+1 );
    }
    else
    {
        return array( $content, $response );
    }
}

?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-24
    • 2016-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-25
    • 2012-06-15
    相关资源
    最近更新 更多