crmpicco
crmpicco

Reputation: 17181

PHP SERVER superglobal HTTP_USER_AGENT key undefined with file_get_contents request

I am making a call from my local development environment to a URL on one of my test systems with the following code:

$vnumber = file_get_contents($url);

On one of the environments I get this error below, however on all others I do not experience this.

Notice: Undefined index: HTTP_USER_AGENT in /var/www/vhosts/test/crmpicco.co.uk/web/config.php on line 127

Line 127 is this:

define ("USER_AGENT", $_SERVER['HTTP_USER_AGENT']);

Other than the $_SERVER superglobal being accidentally trashed (which I cannot find) what would be a potential cause of this?

I am using PHP 5.3.27.

Upvotes: 0

Views: 689

Answers (2)

crmpicco
crmpicco

Reputation: 17181

It turns out I had error_reporting(E_ALL); switched on with my broken environment.

I turned that off and it worked.

Upvotes: 0

Alma Do
Alma Do

Reputation: 37365

And how can it be set? You're doing direct file_get_contents() call, while HTTP_USER_AGENT is set by browser which is absent it this scenario. To emulate HTTP request in full way, use cURL in PHP (so curl_setopt() with CURLOPT_USERAGENT)

Upvotes: 1

Related Questions