0

I don't know how to write a better title. Feel free to edit. Somehow I didn't find anything on this:

I have a cURL request from PHP which returns a quicktime file. This works fine if I want to output the stream in the browser's window. But I want to send it as it were a real file. How can I pass the headers and set it to the script's output, without the need of storing everything in a variable.

The script looks like this:

if (preg_match('/^[\w\d-]{36}$/',$key)) {

    // create url
    $url        = $remote . $key;

    // init cURL request
    $ch         = curl_init($url);

    // set options
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
    curl_setopt($ch, CURLOPT_NOBODY, false);
    curl_setopt($ch, CURLOPT_BUFFERSIZE, 256);
    if (null !== $username) {
        curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
    }

    // execute request
    curl_exec($ch);

    // close
    curl_close($ch);
}

I can see the header and content like this, so the request itself is working fine:

HTTP/1.1 200 OK X-Powered-By: Servlet/3.0 JSP/2.2 (GlassFish Server Open Source Edition 3.1.2 Java/Oracle Corporation/1.7) Server: GlassFish Server Open Source Edition 3.1.2 Content-Type: video/quicktime Transfer-Encoding: chunked

2
  • Just to make sure I understood. Your script act as a proxy. It downloads a file and send it back to a user's browser. The user sees the video/music playing, but you'd like to force download? Commented Aug 7, 2012 at 11:50
  • Right it acts like a proxy. But the browser just outputs the content as a string. So when calling the script you see the binary content. Bbasically the question should maybe be more like How to pass the mime-type. Commented Aug 7, 2012 at 11:52

3 Answers 3

2

Get the Content-Type from your curl query:

$info = curl_getinfo($ch);
$contentType = $info['content_type'];

And send it to the client:

header("Content-Type: $contentType");
Sign up to request clarification or add additional context in comments.

1 Comment

Many thanks, was the right direction. But worked only when getting the whole content and with the need to store everything in a variable before flushing it.
0

Try this:

header ('Content-Type: video/quicktime');

before outputting the content

1 Comment

The idea for sure is the right. :) Unfortunately the content-type can differ from request to request. So I had to get it dynamically. Many thanks for your answer.
0

So with the help of the previous answers I got it to work. Still it has one request to much in my opinion, but maybe someone has a better approach.

The problems that occurred where:

1.) When using cURL like this:

curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);

the header didn't return the content-type, but only *\*.

2.) Using curl_setopt($ch, CURLOPT_NOBODY, false); got the right content-type but also the whole content itself. So I could store everything in a variable, read the header, send the content. Not really an option somehow.

So I had to request the header once using get_headers($url, 1); before getting the content.

3.) Finally, there was the problem that the HTML5-video-tag and the jwPlayer both didn't want to play 'index.php'. So with mod_rewrite and setting 'name.mov' to 'index.php' it worked:

RewriteRule ^(.*).mov index.php?_route=$1 [QSA]

This is the result:

if (preg_match('/^[\w\d-]{36}$/',$key)) {

    // create url
    $url        = $remote . $key;

    // get header
    $header     = get_headers($url, 1);

    if ( 200 == intval(substr($header[0], 9, 3)) ) {
        // create url
        $url        = $remote . $key;

        // init cURL request
        $ch         = curl_init($url);

        // set options
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
        curl_setopt($ch, CURLOPT_NOBODY, false);
        curl_setopt($ch, CURLOPT_BUFFERSIZE, 256);
        if (null !== $username) {
            curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
        }

        // set header
        header('Content-Type: ' . $header['Content-Type']);

        // execute request
        curl_exec($ch);

        // close
        curl_close($ch);

        exit();
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.