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
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:
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 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.
<?php
print $_SERVER['HTTP_USER_AGENT'
$_SERVER['HTTP_USER_AGENT'
print "<br />";
print $_SERVER['HTTP_USER_AGENT'
?>