Requests是一个PHP的HTTP类库。相对于cURL
等类库来讲,它具备简单易用且友好的API,且不依赖于cURL
。它支持HEAD、 GET、 POST、 PUT、 DELETE和PATCH等方法,基本能知足任何形式的HTTP请求。php
Requests不依赖于任何PHP标准库外的扩展,惟一的要求就是须要PHP5.2+的版本。可是若是PHP的cURL可用,Requests会优先使用它,不然会使用socket。html
{ "require": { "rmccue/requests": ">=1.0" }, "autoload": { "psr-0": {"Requests": "library/"} } }
可使用Composer的加载器:git
phpinclude('/path/to/composer/vendor/autoload.php');
也可使用Requests自带的:github
phpinclude('/path/to/library/Requests.php'); Requests::register_autoloader();
php$response = Requests::get('http://httpbin.org/get');
php$response = Requests::post('http://httpbin.org/post');
须要传数据的话,可使用第三个参数:json
php$data = array('key1' => 'value1', 'key2' => 'value2'); $response = Requests::post('http://httpbin.org/post', array(), $data);
若是须要传原始数据的话,第三个参数请传字符串。api
其余请求方法都大同小异:浏览器
php$response = Requests::put('http://httpbin.org/put'); $response = Requests::delete('http://httpbin.org/delete'); $response = Requests::patch('http://httpbin.org/patch', array('If-Match' => 'e0023aa4e')); $response = Requests::head('http://httpbin.org/headers');
须要注意的是equests::patch()
方法的第二个参数为必传。服务器
看API文档,你会发现这些方法接受的参数几乎同样:$url
,$headers
,$data
(只有POST、PUT和PATCH有),$options
。事实上它们只是对Requests::request()
方法进行了一次封装:cookie
php/** * Send a GET request */ public static function get($url, $headers = array(), $options = array()) { return self::request($url, $headers, null, self::GET, $options); } /** * Send a HEAD request */ public static function head($url, $headers = array(), $options = array()) { return self::request($url, $headers, null, self::HEAD, $options); } /** * Send a DELETE request */ public static function delete($url, $headers = array(), $options = array()) { return self::request($url, $headers, null, self::DELETE, $options); } /** * Send a POST request */ public static function post($url, $headers = array(), $data = array(), $options = array()) { return self::request($url, $headers, $data, self::POST, $options); } /** * Send a PUT request */ public static function put($url, $headers = array(), $data = array(), $options = array()) { return self::request($url, $headers, $data, self::PUT, $options); }
$header
容许咱们自定义请求头,例如POST方法的话,咱们一般须要指定Content-Type
,使服务器知道咱们正在发送的的数据是什么格式:session
php$url = 'https://api.github.com/some/endpoint'; $headers = array('Content-Type' => 'application/json'); $data = array('some' => 'data'); $response = Requests::post($url, $headers, json_encode($data));
$options
容许咱们对请求进行配置,例如超时时间:
php$options = array( 'timeout' => 5 ); $response = Requests::get('https://httpbin.org/', array(), $options);
更多的选项配置请看:http://requests.ryanmccue.info/api/source-class-Requests.html#_request
Requests里全部的请求方法(HEAD、 GET、 POST、 PUT、 DELETE和PATCH)返回的都是一个Requests_Response
对象,这个对象包含了响应的各类信息:
$body
:响应体。$raw
:原始的HTTP响应数据。$headers
:响应头。$status_code
:状态码。$success
:标识请求是否成功。$redirects
:请求的重定向次数。$url
:请求的URL。$history
:请求的历史记录。$cookies
:cookie信息。更多
Requests_Response
的信息请看:http://requests.ryanmccue.info/api/source-class-Requests.html#_request
当你须要对同一网站发出多个请求,那么Requests_Session
对象能够帮到轻易的设置一些默认参数:
php$url = 'https://api.github.com/'; $header = array('X-ContactAuthor' => 'rmccue'); $data = array(); $options = array('useragent' => 'My-Awesome-App'); $session = new Requests_Session($url, $header, $data, $options); $response = $session->get('/zen');
Requests_Session
的构造函数接受url
、headers
、data
和options
这4个参数,顺序跟Requests::request()
方法一致。同时你也能够经过访问属性的方式去修改options
参数:
php// 设置option属性 $session->useragent = 'My-Awesome-App'; // 跟上面的做用一致 $session->options['useragent'] = 'My-Awesome-App';
更多Requests_Session
的信息请看:http://requests.ryanmccue.info/api/source-class-Requests_Session.html
Requests会默认帮忙处理HTTPS请求,就跟在浏览器访问HTTPS网站同样:
php$response = Requests::get('https://httpbin.org/');
可是若是你想使用其余的证书或者自签证书,你能够指定证书文件(PEM格式):
php$options = array( 'verify' => '/path/to/cacert.pem' ); $response = Requests::get('https://httpbin.org/', array(), $options);
若是你想禁用HTTPS的验证,能够经过设置options
:'verify' => false
。
HTTP基本验证功能能够经过options
的auth
实现:
php$options = array( 'auth' => array('user', 'password') ); Requests::get('http://httpbin.org/basic-auth/user/password', array(), $options);
代理能够经过options
的proxy
实现:
php$options = array( 'proxy' => '127.0.0.1:3128' ); Requests::get('http://httpbin.org/ip', array(), $options);
若是代理须要验证:
php$options = array( 'proxy' => array( '127.0.0.1:3128', 'my_username', 'my_password' ) ); Requests::get('http://httpbin.org/ip', array(), $options);
经过Requests的钩子系统,咱们能够经过注册本身的钩子去扩展Requests的功能:
php$hooks = new Requests_Hooks(); $hooks->register('requests.after_request', 'mycallback'); $request = Requests::get('http://httpbin.org/get', array(), array('hooks' => $hooks));
Request提供的钩子请看:http://requests.ryanmccue.info/docs/hooks.html
经过实现Requests_Auth
接口,咱们能够为请求添加自定义的验证。假设服务器会检查HTTP请求里的Hotdog
请求头的值是否是为Yummy
。咱们先实现咱们的验证类:
phpclass MySoftware_Auth_Hotdog implements Requests_Auth { protected $password; public function __construct($password) { $this->password = $password; } public function register(Requests_Hooks &$hooks) { $hooks->register('requests.before_request', array(&$this, 'before_request')); } public function before_request(&$url, &$headers, &$data, &$type, &$options) { $headers['Hotdog'] = $this->password; } }
能够看到,类实现了Requests_Auth
接口,同时代码实现也用到了钩子。下面咱们经过options
的auth
去调用咱们的自定义验证:
php$options = array( 'auth' => new MySoftware_Auth_Hotdog('yummy') ); $response = Requests::get('http://hotdogbin.org/admin', array(), $options);
文章开始提到了Requests的一些优势,这个官网有个专门的页面进行详细的介绍,同时还提到了Requests跟其余相似类库的对比。经过这个对比,你们对Requests会有进一步的认识,同时也科普下还有哪些HTTP请求相关的类库。
请猛击:http://requests.ryanmccue.info/docs/why-requests.html