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?
Requests::get, the second parameter passes HTTP headers “as-is”, so you will have to use the right header name instead ofuseragent(which Requests_Session seems to handle internally).useragentis an option, not a header. In any event, passing auseragentheader as the second parameter did not change the result.Requests_Session. If you want a special header to be set when usingRequests::get, then just do that.