Search code examples
phpcurlhttpsgetheader

Curl GET response empty result


Hi there, I am trying to you use CURL to get page from a website below is my code: my PHP file with CURL settings

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300); 
curl_setopt($ch, CURLOPT_TIMEOUT, 300); //timeout in seconds
$resultcurl = curl_exec($ch);
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
curl_close($ch);

The after going through CURL through Stackoverflow I got a post which suggested the following

$header = get_headers('https://www.websitedomain.com/somepage');
echo '<pre>';
print_r($header);
echo '</pre>';

Then I got :

Array
(
    [0] => HTTP/1.1 301 Moved Permanently
    [1] => Cache-Control: no-cache, must-revalidate
    [2] => Content-Type: text/html; charset=UTF-8
    [3] => Date: Thu, 21 Jun 2018 06:50:00 GMT
    [4] => Expires: Sun, 19 Nov 1978 05:00:00 GMT
    [5] => Location: https://www.websitedomain.com/somepage
    [6] => Server: Apache/2.4.33 (Amazon) OpenSSL/1.0.2k-fips PHP/5.6.36
    [7] => X-Content-Type-Options: nosniff
    [8] => X-Content-Type-Options: nosniff
    [9] => X-Drupal-Cache: MISS
    [10] => X-Powered-By: PHP/5.6.36
    [11] => Content-Length: 10
    [12] => Connection: Close
    [13] => HTTP/1.1 200 OK
    [14] => Cache-Control: no-cache, must-revalidate
    [15] => Content-Language: en
    [16] => Content-Type: text/html; charset=utf-8
    [17] => Date: Thu, 21 Jun 2018 06:50:00 GMT
    [18] => Expires: Sun, 19 Nov 1978 05:00:00 GMT
    [19] => Link: ,,,,,
    [20] => Server: Apache/2.4.33 (Amazon) OpenSSL/1.0.2k-fips PHP/5.6.36
    [21] => Vary: Accept-Encoding
    [22] => X-Content-Type-Options: nosniff
    [23] => X-Content-Type-Options: nosniff
    [24] => X-Drupal-Cache: MISS
    [25] => X-Generator: Drupal 7 (http://drupal.org)
    [26] => X-Powered-By: PHP/5.6.36
    [27] => X-UA-Compatible: IE=edge,chrome=1
    [28] => Connection: Close
)

Content Length always shows "10" and when I access the page directly through browser it works ! Can someone please help !!?


Solution

  • HTTP/1.1 301 Moved Permanently

    You're getting a redirect. You need to follow it, which browsers do automatically, but which PHP's cURL library won't by default. You can change that with:

    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);