[SOLVED] Why is curl() returning different results from get_headers()?

I am trying to validate URLs and return the url http_code.

Problem:

Sometimes different http_codes are returned

  1. curl(…); ==> 400 // INCORRECT
  2. get_header(…) ==> 301 // CORRECT

Unfortunately I cannot use the second method because some URLs result in the page hanging without any reesults!

Curl has an option: CURLOPT_TIMEOUT = 10 (seconds) BUT it is giving different results.

Try this:
$url = ‘https://ikea.com’;
$aHdr = @get_headers($url);

// THIS LINE IS NEVER EVER ACTIVATED! - expect it to change at anytime

print_r($aHdr);


//=================================================

INCORRECT http_code = 400

Curl Script:

  $url  = 'http://ikea.com'; 

  $opts = [
    CURLOPT_URL             => $url,
    CURLOPT_HEADER          => true,
    CURLOPT_NOBODY          => true,
    CURLOPT_RETURNTRANSFER  => true,
    CURLOPT_FOLLOWLOCATION  => true, 
    CURLOPT_SSL_VERIFYHOST  => false,
    CURLOPT_SSL_VERIFYPEER  => false,
    CURLOPT_TIMEOUT         => 15,
  ];
  $ch = curl_init($url);
    curl_reset($ch);
    curl_setopt_array($ch, $opts);
    curl_reset($ch);
    curl_setopt_array($ch, $opts);
  $sOK  = curl_exec($ch); 

# ESSENTIAL TO CALL BEFORE CLOSING
  prettifyResults( curl_getinfo($ch) );
  curl_close($ch);

Curl() Prettified Results:

url
    http://ikea.com/ 
content_type
    text/html 
http_code
    400 
header_size
    98 
request_size
    48 
filetime
    -1 
total_time
    0.635201 
namelookup_time
    0.004272 
connect_time
    0.127172 
pretransfer_time
    0.127289 
download_content_length
    246 
upload_content_length
    -1 
starttransfer_time
    0.635158 
primary_ip
    216.87.148.114 
certinfo
    Array ( ) 
primary_port
    80 
local_ip
    192.168.1.105 
local_port
    41724 

//======================================================

CORRECT http_code = 301

get_headers() Script

 $aHdr = @get_headers($url);
 prettifyResults( $aHdr );

get_headers($url) Prettified Results:

http://ikea.com
    HTTP/1.0 301 Moved Permanently
Location
    http://www.ikea.com
Content-Length
    224
Connection
    close
Location Response
    HTTP/1.0 301 Moved Permanently
Server
    AkamaiGHost
Content-Length
    0
Location
    https://www.ikea.com/
Cache-Control
    private, max-age=300
Expires
    Sun, 26 Aug 2018 06:24:57 GMT
Date
    Sun, 26 Aug 2018 06:19:57 GMT
Connection
    close
Set-Cookie
    eaesssn=5778e7313d1f00008d46825bef010000b0560000; path=/; domain=.ikea.com
Set-Cookie
    MyLocation=TH; expires=Sun, 26-Aug-2018 06:24:57 GMT
X-Content-Type-Options
    nosniff
X-UA-Compatible
    IE=edge
Location Response
    HTTP/1.0 200 OK
ETag
    "52ee03de84b1b44964304ce117395227:1533631913"
Last-Modified
    Tue, 07 Aug 2018 08:51:53 GMT
Accept-Ranges
    bytes
Content-Length
    52140
Content-Type
    text/html
X-UA-Compatible
    IE=edge
Cache-Control
    private, max-age=300
Expires
    Sun, 26 Aug 2018 06:24:57 GMT
Date
    Sun, 26 Aug 2018 06:19:57 GMT
Connection
    close
Set-Cookie
    eaesssn=5c78e731a06c00008d46825b06030000495e0000; path=/; domain=.ikea.com
Set-Cookie
    MyLocation=TH; expires=Sun, 26-Aug-2018 06:24:57 GMT
X-Content-Type-Options
    nosniff
Server
    IITP Server

Curl is also getting a 301, but it’s following it, i.e., making a new request to the provided location to redirect to, because you have CURLOPT_FOLLOWLOCATION set to true. If you set CURLOPT_FOLLOWLOCATION to false, curl will return 301 too.

1 Like

Also, you can set a timeout with get_headers as well if you want to:

$context = stream_context_create(['http' => ['timeout' => 15]]);

$headers = get_headers($url, 0, $context);
1 Like

Many thanks for the prompt reply.

Before adding the follow location 400 was returned. After adding incorrectly it was still 400.

I look forward to trying your suggestion when I am next on the desktop.

Many thanks for the prompt reply.

I look forward to trying your suggestion when I am on the desktop.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.