63639

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

Question:

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

Answer1:

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

See <a href="http://php.net/manual/en/filesystem.configuration.php#ini.user-agent" rel="nofollow">http://php.net/manual/en/filesystem.configuration.php#ini.user-agent</a> (via <a href="http://php.net/manual/en/wrappers.http.php" rel="nofollow">http://php.net/manual/en/wrappers.http.php</a>)

<h2>Update</h2>

An example (as requested)

ini_set('user_agent', 'RTM');

Answer2:

You can set the user_agent property in the php.ini config file, or use <a href="http://us3.php.net/ini_set" rel="nofollow">ini_set</a> to change it without modifying the php.ini, so you can customize on a per-script basis.

Also, one of the comments <a href="http://php.net/manual/en/wrappers.http.php" rel="nofollow">from this page</a> 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'); ?>

Recommend

  • Jsoup returns Status 400
  • How can I force PHP's fopen() to return the current version of a web page?
  • PHP Soap ssl how to trust self-signed certificate
  • Scrapy store returned items in variables to use in main script
  • Enabling domxml in php
  • Change of MySQL database location not working
  • Changing MySQL charset to UTF8 on linux so it will work with JDBC
  • Getting syntax error in mysql-php. You have an error in your SQL syntax;
  • Compress a file with GZipStream while maintaining its meta-data
  • How do I retrieve the user information of a user authenticated with Apache's mod_ldap?
  • How can I include If-None-Match header in HttpRequestMessage
  • Suppressing passwd when calling sqlplus from shell script
  • Security issues with PHP's Readfile method
  • Parse a date string in a specific locale (not timezone!)
  • Install PHP intl extension on MacOS
  • Database structure design with variable amounts of fields
  • Display issues when we change from one jquery mobile page to another in firefox
  • Why ng-show works with ng-repeat but ng-if doesn't? [duplicate]
  • How do I fake an specific browser client when using Java's Net library?
  • Get object from AWS S3 as a stream
  • How to get a value (ex: baseURL) in every Karate feature?
  • Cross-Platform Protobuf Serialization
  • Validaiting emails with Net.Mail MailAddress
  • Upload files with Ajax and Jquery
  • Do I've to free mysql result after storing it?
  • Invalid access key error using credentials redeemed from an amazon open id token
  • Circular dependency while pushing http interceptor
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • A cron job substitute?
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • json Serialization in asp
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How to set the response of a form post action to a iframe source?
  • coudnt use logback because of log4j
  • Change div Background jquery
  • Qt: Run a script BEFORE make
  • Checking variable from a different class in C#
  • reshape alternating columns in less time and using less memory
  • UserPrincipal.Current returns apppool on IIS