How to get header only with CURL

i just use CURL to get header from web page.

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $i);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);

$result = curl_exec($ch);

i used it instead of using get_headers() function, because CURL has time out option.
Howeve, when i set NOBODY is TRUE. The header return not right like get_headers() :mad:
so, how can i do that?

if you have it what about the HTTP functions?
http://www.php.net/manual/en/function.http-head.php

omg. i’m crazy with it.
no more easy solution? http package is too big

o.O


$r = http_head('http://example.com/', array('timeout' => 10), $info);
print_r($r);
print_r($info);

I find that super easy…

Fatal error: Call to undefined function http_head() in …

well its seems you don’t have the HTTP extension.

If you did you would have gotten:


print_r($r):
HTTP/1.1 200 OK
Date: Sun, 09 Sep 2007 05:45:47 GMT
Server: Apache/2.2.3 (CentOS)
Last-Modified: Tue, 15 Nov 2005 13:24:10 GMT
ETag: "280100-1b6-80bfd280"
Accept-Ranges: bytes
Content-Length: 438
Connection: close
Content-Type: text/html; charset=UTF-8

print_r($info):
Array
(
    [effective_url] => http://example.com/
    [response_code] => 200
    [total_time] => 0.042
    [namelookup_time] => 0.001
    [connect_time] => 0.019
    [pretransfer_time] => 0.019
    [size_upload] => 0
    [size_download] => 0
    [speed_download] => 0
    [speed_upload] => 0
    [header_size] => 264
    [request_size] => 96
    [ssl_verifyresult] => 0
    [filetime] => 1132061050
    [content_length_download] => 438
    [content_length_upload] => 0
    [starttransfer_time] => 0.042
    [content_type] => text/html; charset=UTF-8
    [redirect_time] => 0
    [redirect_count] => 0
    [connect_code] => 0
    [httpauth_avail] => 0
    [proxyauth_avail] => 0
    [os_errno] => 0
    [num_connects] => 1
    [ssl_engines] => Array
        (
        )

    [cookies] => Array
        (
        )

    [error] => 
)

i just put php_http.dll to extension folder. and enable it in php.ini.
But its seems not work?

This works for me hmm…
The HTTP extension is picky.
http://www.php.net/manual/en/http.install.php


$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,            'http://example.com/');
curl_setopt($ch, CURLOPT_HEADER,         true);
curl_setopt($ch, CURLOPT_NOBODY,         true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT,        10);

$r = curl_exec($ch);

print_r($r);

use this code for this URL:
http://www2.nhac.vui.vn:8000/uploadmusic/Admin/Album/Music_Dj%20songs/Dance%20Mix%20-%20Walking%20In%20The%20Sun.mp3

Here is result

HTTP/1.0 400 Bad Request Content-Type: text/html

And if i use print_r(get_headers($i));

here’s right result:

Array ( [0] => HTTP/1.0 200 OK [1] => Content-Length: 2596864 [2] => Content-Type: audio/mpeg )

http installed.
and here’s my result

HTTP/1.0 400 Bad Request Content-Type: text/html

same the result with CURL . Omd :frowning:

Why get_headers has other result??!

It seems cURL is having troubles accessing http://www2.nhac.vui.vn:8000 but it can access http://www2.nhac.vui.vn just fine. Hmmm.

so why?
get_headers() can, but cURL can’t :frowning: