PHP SERVER superglobal HTTP_USER_AGENT key undefined with file_get_contents request

StackOverflow https://stackoverflow.com/questions/21163789

  •  28-09-2022
  •  | 
  •  

Pergunta

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.

Foi útil?

Solução 2

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

I turned that off and it worked.

Outras dicas

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)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top