[英]Using the Requests for PHP library, how can I set a custom user-agent?
我正在对PHP请求进行一些测试,我想为基本的GET请求设置自定义用户代理。
偷看源代码,我认为也许该测试会通过:
$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);
但是, http://httpbin.org/user-agent返回默认库值php-requests/1.6
作为用户代理。
是否有可能做到这一点还是我需要使用Requests_Session
对象作为证明这里 ?
您必须通过以下方式定义选项:
$options = array('useragent' => $user_agent);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.