Is it possible to set a User-Agent when reading a

2019-05-30 04:51发布

For example, in cURL I can use curl_setopt($curlrequest, CURLOPT_USERAGENT, 'myuseragent'); in order to change the User-Agent when I'm requesting the page specified in $curlrequest.

But can I do something similar with readfile()?

2条回答
地球回转人心会变
2楼-- · 2019-05-30 05:19

Yes, you can set a user_agent property in your php.ini config file or via ini_set() at runtime.

See http://php.net/manual/en/filesystem.configuration.php#ini.user-agent (via http://php.net/manual/en/wrappers.http.php)

Update

An example (as requested)

ini_set('user_agent', 'RTM');
查看更多
欢心
3楼-- · 2019-05-30 05:23

You can set the user_agent property in the php.ini config file, or use ini_set to change it without modifying the php.ini, so you can customize on a per-script basis.

Also, one of the comments from this page says you can do something like this:

<?php
    $default_opts = array(
        'http' => array(
            'user_agent' => 'Foobar',
            'header' => array(
                'X-Foo: Bar',
                'X-Bar: Baz'
            )
        )
    );
    stream_context_get_default($default_opts);
    readfile('http://www.xhaus.com/headers');
?>
查看更多
登录 后发表回答