TomSawyer
TomSawyer

Reputation: 3820

CURL get header return different with get_headers

i want to get header from file. but CURL return value different with get_headers.

don't know why. here's result:

with get_headers

Array
(
    [0] => HTTP/1.0 200 OK
    [1] => Content-Type: image/jpeg
    [2] => Last-Modified: Wed, 14 Nov 2012 11:06:07 GMT
    [3] => X-Cache-Status: HIT
    [4] => Expires: Tue, 19 Jan 2038 03:14:07 GMT
    [5] => Cache-Control: max-age=2592000
    [6] => X-Cache: HIT
    [7] => Content-Length: 120185
    [8] => Connection: close
    [9] => Date: Fri, 16 Nov 2012 08:01:15 GMT
    [10] => Server: lighttpd/1.4.26
)

and CURL no Content-Type ~> thats what i want

Array
(
    [0] => HTTP/1.1 200 OK
    [1] => Content-Length: 120185
    [2] => Connection: close
    [3] => Date: Fri, 16 Nov 2012 08:01:42 GMT
    [4] => Server: lighttpd/1.4.26
    [5] => 
    [6] => 
)

here's my get header by curl function

function get_headers_curl($url) 
{ 
    $ch = curl_init(); 

    curl_setopt($ch, CURLOPT_URL,            $url); 
    curl_setopt($ch, CURLOPT_HEADER,         true); 
    curl_setopt($ch, CURLOPT_NOBODY,         true); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_TIMEOUT,        15); 

    $r = curl_exec($ch); 
    $r = split("\n", $r); 
    return $r; 
} 

CURL with return Content-Type if i set

curl_setopt($ch,CURLOPT_HTTPGET,true);

but it will return also file content (body), how to get exactly file CONTENT TYPE ONLY with CURL and without file content?

Upvotes: 1

Views: 1490

Answers (2)

TuanNguyen
TuanNguyen

Reputation: 1046

It's easy to get CONTENT_TYPE or other cURL infomation:

<?php
// Create a curl handle
$ch = curl_init('link_to_file');

curl_setopt($ch, CURLOPT_NOBODY, TRUE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, FALSE);

// Execute
curl_exec($ch);

// Check if any error occured
if (!curl_errno($ch)) {
    var_dump(curl_getinfo($ch, CURLINFO_CONTENT_TYPE));
}

// Close handle
curl_close($ch);

Output:

string(10) "image/jpeg"

Upvotes: 1

laurens
laurens

Reputation: 11

get_headers is receiving info for an image, However the other isn't. Guess you could check the url. because both have to be the same.

Upvotes: 1

Related Questions