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.
File Sharing SoftwareXMLPHP

Avatar of undefined
Last Comment
burnsj2

8/22/2022 - Mon
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
Oscurochu

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
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.
Oscurochu

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. :)
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
landship

Thanks for the lookout, Oscurochu.

I must say though, your solution was more thorough.
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.