Undermine2k
Undermine2k

Reputation: 1491

browser headers for curl request

We have an assignment to filter authentic curl requests from robots. I am sending a curl request to the site, but it's returning to me an invalid image file(i know because when i view it with my browser it works). It somehow knows my request is not authentic. Is there a field I'm overlooking here, I'm trying to mimic a browser request exactly.

 $header_arr = array(
            '0' =>'Host: www.myittest.com',
            '1' =>'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0',
             '2' =>'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*\/*;q=0.8',
             '3' =>'Accept-Language: en-US,en;q=0.5', 
             '4' =>'Accept-Encoding: gzip, deflate',
             '5' =>'Connection: keep-alive',
          );





    $ch = curl_init($url);         

    curl_setopt($ch, CURLOPT_HTTPHEADER, $header_arr);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);  
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);         
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);    
    curl_setopt($ch, CURLOPT_MAXREDIRS, 6);



    $raw=curl_exec($ch);

Upvotes: 1

Views: 1238

Answers (1)

toster-cx
toster-cx

Reputation: 2387

You have requested gzip/deflate encoding but haven't made curl aware of it so it doesn't decode the image. Adding this should fix it:

curl_setopt($ch, CURLOPT_ENCODING, '');

Upvotes: 1

Related Questions