insertusernamehere
insertusernamehere

Reputation: 23580

Pass header/content-type from cURL-request to current output/header

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

Upvotes: 0

Views: 2794

Answers (3)

insertusernamehere
insertusernamehere

Reputation: 23580

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

}

Upvotes: 0

Tchoupi
Tchoupi

Reputation: 14681

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");

Upvotes: 2

Edson Medina
Edson Medina

Reputation: 10269

Try this:

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

before outputting the content

Upvotes: 0

Related Questions