Sunday 6 March 2016

How to mock your php Curl request as if coming from a user's Web browser?

There are times when we want to make http request to some other third party website to download or access some data/url. Well, it looks strange at first look as if why would we need to worry about it, we can directly call the url from php code using CUrl.

Till now things look simple. Yes, it is easy to access url using Curl. But sometimes, we face issue while accessing the url. There is no problem when we access the same url from browser, but it fails when we try to access the same url from our php code on server.

The reason being, some third party websites whose url/data you want to access would like to provide data only to users using web browser and not to any webservers. So, in such cases the CUrl fails.

So, what is the solution?
Its very simple. If you think logically, we just need to make our http request mock as if it is coming from and users browser and not through any server.

To do this, we will set few header parameters that will mock the request as request from any web browser.

Here is the addition that you need to add in your existing php code.

 $url = 'http://www.xyz.com/';  
 $ch = curl_init($url);  
 curl_setopt_array($ch, array(  
   CURLOPT_HTTPHEADER => array('Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.83 Safari/537.1'),  
   CURLOPT_RETURNTRANSFER =>true,  
   CURLOPT_VERBOSE   => 1  
 ));  
 $out = curl_exec($ch);  
 curl_close($ch);  
 echo $out;  

Hope this will help you.
Thanks for arriving at this page.

No comments:

Post a Comment