pablosanches / owl
此包已被放弃,不再维护。未建议替代包。
Owl 是一个简单的 PHP cURL 封装器
1.0.0
2017-08-11 12:56 UTC
Requires
- php: >=5.6
Requires (Dev)
- phpunit/phpunit: 4.2.*
This package is auto-updated.
Last update: 2021-06-18 13:48:57 UTC
README
Owl 是一个简单的 PHP cURL 封装器
要求
- PHP >= 5.6
- cURL 库已启用
安装
Composer
{ "require": { "pablosanches/owl": "1.0.*" } }
require_once './vendor/autoload.php';
用法
// Namespace shortcut use Owl\Method as Http; // Template $request = new Http\<METHOD_NAME>( string $url [, array $options ] );
方法有
Get()Head()Options()Post()Put()Patch()Delete()
构造函数 $options
[
'data' => [ // Data to send, available for `Post`, `Put` and `Patch`
'foo' => 'bar'
],
'headers' => [ // Additional headers (optional)
'Authorization: foobar'
],
'ssl' => '/cacert.pem', // Use it for SSL (optional)
'is_payload' => true, // `true` for sending a payload (JSON-encoded data, optional)
'autoclose' => true // Is the request must be automatically closed (optional)
]
公开方法
// Send a request $request->send(); // HTTP status code $request->getStatus(); // HTTP header $request->getHeader(); // HTTP body response $request->getResponse(); // Used cURL options $request->getCurlOptions(); // Set a cURL option $request->setCurlOption(CURLOPT_SOMETHING, $value); // Manually close the handle (necessary when `autoclose => false` is used) $request->close();
示例
基本
// Namespace shortcut use Owl\Method as Http; // Standard GET request $request = new Http\Get('http://domain.com'); // Send this request $request->send(); echo $request->getResponse(); // body response echo $request->getStatus(); // HTTP status code
发送有效负载
// Namespace shortcut use Owl\Method as Http; // JSON-encoded POST request $request = new Http\Post($this->endpoint, [ 'data' => [ 'name' => 'foo', 'email' => 'foo@domain.com' ], // With 'is_payload' => true // You don't have to json_encode() your array of data // Moreover, the appropriate headers will be set for you 'is_payload' => true ]); // Send this request $request->send(); echo $request->getResponse(); // body response echo $request->getStatus(); // HTTP status code
手动关闭
// Namespace shortcut use Owl\Method as Http; // Set `autoclose` option to `false` $request = new Http\Get('http://domain.com', [ 'autoclose' => false ]); // Send this request $request->send(); // Now you can retrieve a cURL info as the handle is still open $request->getCurlInfo(CURLINFO_SOMETHING); echo $request->getResponse(); // Manually close the handle $request->close();
测试
在项目目录下
composer install获取phpunitphpunit运行测试