0

I'm doing some testing of Requests for PHP and I'd like to set a custom user-agent for a basic GET request.

Peeking at the source code, I thought perhaps this test would pass:

$url = 'http://httpbin.org/user-agent'; $user_agent = 'my-test-agent'; $options = array('useragent', $user_agent); $response = Requests::get($url, array(), $options); $json = json_decode($response->body, true); $this->assertEqual($json['user-agent'], $user_agent); 

However, http://httpbin.org/user-agent returns the default library value php-requests/1.6 as the user-agent.

Is it possible to do this or do I need to use the Requests_Session object as demonstrated here?

3
  • When you just use Requests::get, the second parameter passes HTTP headers “as-is”, so you will have to use the right header name instead of useragent (which Requests_Session seems to handle internally). Commented Oct 14, 2013 at 6:46
  • It looks to me like useragent is an option, not a header. In any event, passing a useragent header as the second parameter did not change the result. Commented Oct 14, 2013 at 6:52
  • Yes, it’s an option to set when using Requests_Session. If you want a special header to be set when using Requests::get, then just do that. Commented Oct 14, 2013 at 6:54

1 Answer 1

1

You have to define the options this way:

$options = array('useragent' => $user_agent); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.