Prashant Pugalia
Prashant Pugalia

Reputation: 1101

https headers with file_get_contents

This does not get gzipped content, but plain content. How to make file_get_contents send headers with https ?

$url = 'https://www.google.co.in/';

///Try to fetch compressed content using the file_get_contents function
$opts = array(
'http'=>array(
    'method'=>"GET",
    'header'=>"Accept-language: en-US,en;q=0.8\r\n" .
                "Accept-Encoding: gzip,deflate,sdch\r\n" .
                "Accept-Charset:UTF-8,*;q=0.5\r\n"
)
);

$context = stream_context_create($opts);
$zipped_content = file_get_contents($url ,false,$context);

echo $zipped_content;

print_r($http_response_header);

If the url is http://www.yahoo.co.in then the gzipped content is served (and to confirm, it appears like rubbish).

But when using "https://" it seems that file_get_contents does not send the headers specified.

Upvotes: 0

Views: 2358

Answers (2)

sectus
sectus

Reputation: 15464

Header are no OK... Add User-agent and it will be fine.

"User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:19.0) Gecko/20100101 Firefox/19.0 FirePHP/0.4\r\n".

Why? Google decides.

Upvotes: 1

Sandeep Solanki
Sandeep Solanki

Reputation: 702

Try this

 $url = "https://www.google.co.in/";   
  $ch = curl_init();    
  curl_setopt($ch,CURLOPT_URL,$url);   
  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);   
  curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);    
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept-Encoding: gzip'));   
  $contents = curl_exec($ch);   
  curl_close($ch);

Upvotes: 0

Related Questions