Is it possible to set a User-Agent when reading a remote file using readfile() in php?

柔情痞子 提交于 2019-12-23 02:05:29

问题


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()?


回答1:


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');



回答2:


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');
?>


来源:https://stackoverflow.com/questions/13696421/is-it-possible-to-set-a-user-agent-when-reading-a-remote-file-using-readfile-i

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!