CURL Request like browser in PHP

CURL is powerful tool and extension for of the PHP. It uses to get the response from the other external URL. Actually, we can get many tutorial from internet for this topic. Our main aim to write this blog in this topic because of no one written about the user agent in CURL.

We are telling our recent experience about the issue for specific URL which given the result of 403 via CURL. But that URL is properly opened in the any browser. So we checked lot more about this thing and try to get response from this type of URL.

Finally, we got the solution using “CURLOPT_USERAGENT” inside the CURL request. You can check it from this link http://docs.php.net/function.curl-setopt

Below is the full example to do this thing.

$url="https://www.google.com";

$agent= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)'; 

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);

var_dump($result);

We have also used CURLOPT_SSL_VERIFYPEER because some time request uses https there might also be error in verifying the certificate.

Leave a Comment

Your email address will not be published. Required fields are marked *