Link to home
Start Free TrialLog in
Avatar of burnsj2
burnsj2

asked on

PHP set User Agent String

I'm working on a project where the API requires that I "must set the HTTP user agent to a valid web browser string" like Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)

My questions is, how do I actually "set" the user agent in PHP.  Can it be done with header()?
The actual call to the API looks like $xml = simplexml_load_file($url);

Thanks.
Avatar of landship
landship

You can write directly to the user agent global, here's an example just copy and paste to see:

<?php
print $_SERVER['HTTP_USER_AGENT'];
$_SERVER['HTTP_USER_AGENT'] = "something else";
print "<br />";
print $_SERVER['HTTP_USER_AGENT'];
?>
ASKER CERTIFIED SOLUTION
Avatar of Oscurochu
Oscurochu
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of burnsj2

ASKER

It seems the trick is to use:
ini_set("user_agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
in your PHP document.  The "user_agent" is a setting in php.ini and is set to "PHP" by default.
You should be more specific of what you needed it for next time. you have three different reponses, and they're all relavant. Personally, I think you should give us all points because you didn't say what you needed to change the user-agent for.

I'm just looking out for every one else. :)
Thanks for the lookout, Oscurochu.

I must say though, your solution was more thorough.
Avatar of burnsj2

ASKER

Just as a note, neither solution worked in my case, but if using sockets Oscurochu's would pass a user agent to another server, the first solution would change the user agent of the client calling the script.