1

I try to download a video file from this url

<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>

with this script

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


but i cant get the file, i have try the script with some urls and it has diferent result
when i download the url with internet download manager there is a notice that says the server sent the following name "file_name"
any solution ?
thanks

Try Mersianto
  • 11
  • 1
  • 3
  • 1
    You need specific settings to allow url downloads with `file_get_contents`. This is a common issue, and I suggest you browse Stack Overflow to learn how to change the settings and how to get relevant error information when `file_get_contents` fails. – GolezTrol Sep 12 '15 at 21:09

2 Answers2

0

Here's a sample script which downloads a .zip file from a URI using file_get_contents and file_put_contents

<?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();

The benefit of using PHP's file_get_contents and file_put_contents is these don't rely on any 3rd party libraries (such as cURL), and will work with different versions of PHP

While cURL will work, the PHP methods to use cURL have changed in the past, so you'll have to keep up with the changes from version of version of PHP.

If memory is an issue, then use this accepted answer Download Large Files Efficiently

Kearney Taaffe
  • 647
  • 8
  • 20
  • Do not confuse people with false claims and your subjective preferences. First. CURL API was not changed for many years and even code written in 2008 works as fine as before. It is also much more configurable and debuggable. Second. There is huge difference between downloading file via CURL and via file_getcontents+file_put_contents. file_get_contents will load entire file into RAM and file_put_contents will reserve significant part of RAM for write buffer. While curl (properly configured) will stream the file straight to disk. – Andrew Mar 26 '18 at 08:13
  • It’s not subjective. If you compile PHP from source, there was a change 2 years which broke a how to Send a request with a body. Rather than try to have 2 sesperate cURL versions I discovered ‘file_get_contents’ solves my issue. Secondly there was some breaking changes to cURL functions from PHP 5.5 to PHP7. So, if/when PHP decides to change things, the OP will have to update code. Lastly, we don’t know if OP need a low memory profile, so there’s nothing wrong with approach I suggest – Kearney Taaffe Mar 26 '18 at 20:10
  • Considering the question and OP profile I made assumption (just as you did yours) that OP is not so experienced to see nuances and differences in different approaches. So there is nothing bad to give useful advice even if guy asked about file_get_contents. For the same reason i doubt he will compile PHP. Memory consumption is an issue if you intended to download video from YT. If it worked for you means it means only that it worked for you, nothing else. What do you mean by breaking changes? Do you have an example? Does example I linked to doesn't work for you? – Andrew Mar 26 '18 at 21:38
  • @Andrew I don't have an example off hand, but a quick search on PHP.net revealed that `CURLOPT_PROGRESSFUNCTION` and `CURLOPT_NOPROGRESS` was changed from 5.5 to 5.6 and broke things. The issue I had a few years ago was an issue with cURL not sending HTTP body correctly. It was fixed, but after a few days. It's not uncommon for people to compile PHP, especially with FreeBSD ports & Crux, both of which compile source code. I updated my answer to sound less biased towards cURL – Kearney Taaffe Mar 26 '18 at 23:37
-1

First of all check this sample code and use curl instead of file_get_contents whenever possible.

And second - google doesn't like when someone download their content directly. So you might need to pretend a real browser by adding "normal" headers found in requests of regular browsers.

Community
  • 1
  • 1
Andrew
  • 1,756
  • 3
  • 18
  • 31
  • when i download the url with IDM there is notice like this the server sent the following name "file_name" – Try Mersianto Sep 12 '15 at 21:23
  • The OP asked for `file_get_contents` and `file_put_contents`. Don't suggest a 3rd party library that **may** or **may not** be supported by his/her version of PHP. Additionally, cURL requires the OP to constantly stay up to date with cURL updates, and patch his/her PHP version as updates come available. – Kearney Taaffe Mar 25 '18 at 02:58