I am trying to scrape a site that actually block Bots.
I have this code in PHP cURL to get away with blockage.
$headers = array(
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding: zip, deflate, sdch'
, 'Accept-Language:en-US,en;q=0.8'
, 'Cache-Control:max-age=0',
'User-Agent:' . $user_agents[array_rand($user_agents)]
);
curl_setopt($curl_init, CURLOPT_URL, $url);
curl_setopt($curl_init, CURLOPT_HTTPHEADER, $headers);
$output = curl_exec($curl_init);
It works well.
But I am using PHP Goutte, I want to generate same request using this library
$headers2 = array(
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding' => 'zip, deflate, sdch'
, 'Accept-Language' => 'en-US,en;q=0.8'
, 'Cache-Control' => 'max-age=0',
'User-Agent' => $user_agents[array_rand($user_agents)]
);
$client = new Client();
foreach ($headers2 as $key => $v) {
$client->setHeader($key, $v);
}
$resp = $client->request('GET', $url);
echo $resp->html();
But using this code I get blocked from the site I am scraping.
I want to know how can I use Gouttee to properly use Headers?