sylouuu / php-curl
轻量级 PHP cURL 封装器
0.8.1
2015-08-25 15:37 UTC
Requires
- php: >5.3.0
Requires (Dev)
- phpunit/phpunit: 4.0.*
README
要求
- PHP >= 5.4
- cURL 库已启用
安装
Composer
{ "require": { "sylouuu/php-curl": "0.8.*" } }
require_once './vendor/autoload.php';
用法
// Namespace shortcut use sylouuu\Curl\Method as Curl; // Template $request = new Curl\<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 sylouuu\Curl\Method as Curl; // Standard GET request $request = new Curl\Get('http://domain.com'); // Send this request $request->send(); echo $request->getResponse(); // body response echo $request->getStatus(); // HTTP status code
发送有效负载
// Namespace shortcut use sylouuu\Curl\Method as Curl; // JSON-encoded POST request $request = new Curl\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 sylouuu\Curl\Method as Curl; // Set `autoclose` option to `false` $request = new Curl\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
获取phpunit
phpunit
运行测试
变更日志
2015-08-25 - 0.8.1
- 修复了在没有提供数据的情况下无效的内容长度问题
2015-07-03 - 0.8.0
- 添加了
is_payload
选项,用于执行带有 JSON 编码数据的请求 - 修复了
ssl
选项
2014-10-23 - 0.7.1
- 修复了在某些情况下
Post()
没有发送数据的问题
2014-08-01 - 0.7.0 (向后不兼容)
- 从 psr-0 自动加载移动到 psr-4
- 添加了
Method
目录,然后方法现在位于\sylouuu\Curl\Method\
2014-05-30 - 0.6.1
- 当
data
选项对于Post
、Put
和Patch
未指定时,移除了异常
2014-05-22 - 0.6.0 (向后不兼容)
- 将
url
选项移动到第一个构造函数参数
2014-05-20 - 0.5.0 (向后不兼容)
- 将存储库从
php-rest-client
重命名为php-curl
- 重构了所有代码
- 添加了
autoclose
选项 - 添加了获取/设置 cURL 选项的方式
- 添加了获取 cURL 信息的方式
- 源代码现在符合 psr-2 标准
2014-05-13 - 0.4.0 (向后不兼容)
- 将
RESTClient.class.php
重命名为RESTClient.php
- 将
RESTClient.php
移动到/src
- 将
RESTClientTest.php
移动到/tests
- 添加了对
HEAD
、OPTIONS
和PATCH
的支持 - 添加了
getHeader
方法 - 将
getJSON
重命名为getResponse
- 移除了 JSON 验证
- 添加了
sylouuu
命名空间 - 移除了
gulp
2014-05-09 - 0.3.0
- 添加了
ssl
选项
2014-04-06 - 0.2.1
- 如果返回无效的 JSON 格式,则添加了异常
2014-04-04 - 0.2.0
- 检索结果的新方法
- 添加了 HTTP 状态代码
2014-04-01 - 0.1.0
- 重构了类
- 移除了构造函数参数
- 添加了单元测试
2014-03-24 - 0.0.2
- 将
$api_url
添加为构造函数参数
2014-02-05 - 0.0.1
- 首次发布