SanketR
SanketR

Reputation: 1192

Why does ini_get('user_agent') doesn't work with file_get_contents() in php?

I have a situation like this.

  1. A crawler script fetches the content of the URL using file_get_contents().

  2. It sets the user agent as "CrawlerBot" just above the line where file_get_contents() is called using ini_set('user_agent').

My concern is when I write ini_get('user_agent') in the code of URL, it gets a blank value. However when I use $_SERVER['HTTP_USER_AGENT'] it detects the correct user agent. Both the files are hosted on same server.

Anybody aware why does it happen?

Upvotes: 0

Views: 748

Answers (2)

Raphaël G.
Raphaël G.

Reputation: 19

Here is an example of code to set user agent and retrive a ressource with file_get_contents.

//Set uri       
$uri = 'http://example.com';

//Init context
$ctx = stream_context_create(
        array(
                'http' => array(
                        'user_agent' => 'MySuperAgent/3.0'
                )
        )
);

//Try to retrieve content
if (($data = file_get_contents($uri, false, $ctx)) === false) {
        die('file_get_contents error');
}

ps : Note that the context array should be under http key even for https ressources.

ps2 : I strongly suggests that you set the timeout and maximum acceptable redirections on the context to avoid slowdown in your application.

Upvotes: 0

elixenide
elixenide

Reputation: 44823

That's not what ini_get() does. It's for retrieving server configuration values (the configuration of your server), not request-specific values like the user agent sent by a requesting browser/script/whatever.

So, you can use ini_get() to find out what user agent value, if any, is set for requests made by your server, like the one you are actually making. You cannot use it to find out the user agent of a request made to your server.

Upvotes: 1

Related Questions