SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Zealot phpguru's Avatar
    Join Date
    Sep 2005
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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()
    so, how can i do that?

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    if you have it what about the HTTP functions?
    http://www.php.net/manual/en/function.http-head.php
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Zealot phpguru's Avatar
    Join Date
    Sep 2005
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    omg. i'm crazy with it.
    no more easy solution? http package is too big

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    o.O

    PHP Code:
    $r http_head('http://example.com/', array('timeout' => 10), $info);
    print_r($r);
    print_r($info); 
    I find that super easy...
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Zealot phpguru's Avatar
    Join Date
    Sep 2005
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    o.O

    PHP Code:
    $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 ...

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    well its seems you don't have the HTTP extension.

    If you did you would have gotten:
    Code:
    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] => 
    )
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    SitePoint Zealot phpguru's Avatar
    Join Date
    Sep 2005
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i just put php_http.dll to extension folder. and enable it in php.ini.
    But its seems not work?

  8. #8
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    This works for me hmm...
    The HTTP extension is picky.
    http://www.php.net/manual/en/http.install.php

    PHP Code:
    $ch curl_init();

    curl_setopt($chCURLOPT_URL,            'http://example.com/');
    curl_setopt($chCURLOPT_HEADER,         true);
    curl_setopt($chCURLOPT_NOBODY,         true);
    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
    curl_setopt($chCURLOPT_TIMEOUT,        10);

    $r curl_exec($ch);

    print_r($r); 
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  9. #9
    SitePoint Zealot phpguru's Avatar
    Join Date
    Sep 2005
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    This works for me hmm...
    The HTTP extension is picky.
    http://www.php.net/manual/en/http.install.php

    PHP Code:
    $ch curl_init();

    curl_setopt($chCURLOPT_URL,            'http://example.com/');
    curl_setopt($chCURLOPT_HEADER,         true);
    curl_setopt($chCURLOPT_NOBODY,         true);
    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
    curl_setopt($chCURLOPT_TIMEOUT,        10);

    $r curl_exec($ch);

    print_r($r); 
    use this code for this URL:
    http://www2.nhac.vui.vn:8000/uploadm...0The%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 )

  10. #10
    SitePoint Zealot phpguru's Avatar
    Join Date
    Sep 2005
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    http installed.
    and here's my result

    HTTP/1.0 400 Bad Request Content-Type: text/html
    same the result with CURL . Omd

    Why get_headers has other result??!

  11. #11
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    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.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  12. #12
    SitePoint Zealot phpguru's Avatar
    Join Date
    Sep 2005
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •