使用file_get_contents和file_put_contents从Url下载文件

时间:2015-09-12 21:04:32

标签: php url download

我尝试从此网址下载视频文件

<div style="overflow:scroll;">
https://r10---sn-a5m7znee.googlevideo.com/videoplayback?upn=wWztkINP-Nk&source=youtube&key=yt5&mime=video%2F3gpp&requiressl=yes&initcwndbps=13662500&fexp=9407478%2C9408710%2C9409069%2C9415365%2C9415485%2C9416023%2C9416126%2C9417707%2C9418153%2C9418448%2C9420348&pl=36&sparams=dur%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Crequiressl%2Csource%2Cupn%2Cexpire&mv=m&mt=1442090500&ms=au&ip=2602%3Affea%3A1001%3Aa60%3A%3A8673&id=o-AEiN5gMiULrH4nyZFXgNMByrHGr26gVlBZ74tMKtCqBy&lmt=1421779843167082&expire=1442112121&dur=249.382&mn=sn-a5m7znee&mm=31&ipbits=0&sver=3&itag=17&title=Ellie+Goulding+-+Love+Me+Like+You+Do+%28Official+Video%29&keepalive=no&ratebypass=yes&signature=1255578EB41863E01DDD70B56B15956BECDB7B55.336CEDA1BACCDBB8258B2A717E7C487D459F2C98
</div>

使用此脚本

file_put_contents('video.mp4', file_get_contents($the_link));


但我不能得到该文件,我已尝试使用一些网址的脚本,它有不同的结果
当我用互联网下载管理器下载网址时,有一条通知说服务器发送了以下名称“file_name”
任何解决方案
感谢

2 个答案:

答案 0 :(得分:-1)

首先检查this sample代码并尽可能使用curl代替file_get_contents

第二个 - 谷歌不喜欢有人直接下载他们的内容。因此,您可能需要通过添加&#34; normal&#34;来假装真正的浏览器。在常规浏览器的请求中找到的标头。

答案 1 :(得分:-1)

以下是使用file_get_contentsfile_put_contents

从URI下载.zip文件的示例脚本
<?php

class DownloadZippedFile
{

    public function __construct()
    {
        ;
    }

    public function downloadZippedFile($uri, $fileName)
    {
        $opts = [
            'http' => [
                'method'  => 'GET',
                'header'  => '',
                'follow_location' => 0,
                'request_fulluri' => true,
                'protocol_version' => 1.1,
                'user-agent' => 'Paw/3.1.5 (Macintosh; OS X/10.13.2) GCDHTTPRequest',
                'content' => '',
                'connection' => 'close',
            ],
        ];

        $context = stream_context_create($opts);

        $result = file_get_contents($uri, false, $context);

        // search for a 301, 302 in the $http_response_header array
        if ( stristr($http_response_header[0], 'HTTP/1.1 301') ||
            stristr($http_response_header[0], 'HTTP/1.1 302'))
        {
            // find the location to redirect to
            foreach ($http_response_header as $intIndex => $header) {
                if (strstr($header, 'Location: ')) {
                    // get the location to redirect to by splitting on the space
                    $locationArray = explode(' ', $header);
                    $uri = $locationArray[1];
                }
            }

            # attempt the request again
            file_put_contents($fileName, file_get_contents($uri, false, $context));
        }

        file_put_contents($fileName, file_get_contents($uri, false, $context));
    }
}

function main()
{
    $documentId = '603411390';
    $uri = 'http://mis.ercot.com/misdownload/servlets/mirDownload?doclookupId='.$documentId;
    $fileName = './downloads/'.$documentId.'.zip';

    $downloader = new DownloadZippedFile();
    $downloader->downloadZippedFile($uri, $fileName);
}

main();

使用PHP file_get_contentsfile_put_contents的好处是,它们不依赖于任何第三方库(例如cURL),并且可以使用不同版本的PHP

虽然cURL会起作用,但过去使用cURL的PHP​​方法已经发生了变化,所以你必须跟上PHP版本的变化。

如果内存有问题,请使用此接受的答案Download Large Files Efficiently