It is easy to set the User-Agent header in PHP. You can either edit your PHP installation's php.ini file or add the following line of code to your PHP script. The cURL library also supports setting HTTP headers, but this library is not included in the standard PHP installation.
//tell it what value to use for the User-Agent header ini_set('user_agent', 'My Cool Screen-Scraper (+http://www.mangst.com)'); //includes the above User-Agent header in this request and all subsequent requests $page = file_get_contents('http://en.wikipedia.org/wiki/Pumpkin');
Note that this is different from the header() function. The header() function is used to set the headers of the HTTP response that the PHP script itself is generating. This has nothing to do with any HTTP requests that the script makes in the process of generating its response.
6 comments:
Bless you! I searched long and hard to solve this problem. Thank you for sharing this valuable piece of know-how!
Thanks birchy! I'm glad that you found it helpful. :)
I wasted my 1 hour to figure out that Wikipedia requires that HTTP request header be included or else it will return a 403 Forbidden error.Your blog really saved me from spending a full day.Good work .Keep it up.
Glad to help.
Greetings from Norway
I see that the other person above used an hour to find this soulution and that he was happy that he didnt spend the whole day.
Guess what. I have spent a full saturday to get this...
If it was not for you and this info I would propably spend tomorrow as well.
Glad I could help you, Sem!
Post a Comment