codezero/curl
Composer 安装命令:
composer require codezero/curl
包简介
Simple cURL wrapper
README 文档
README
This package wraps most of the cURL functions in a dedicated Curl class, making it feel more object oriented and a little easier to use.
More importantly, the Request class provides some friendly methods that will set the most commonly used cURL options for you, so you don't have to memorize these.
This is a simple cURL wrapper. Some features are not supported (yet):
- Including headers in the response output (
CURLOPT_HEADER) - cURL multi handles
- cURL share handles
Installation
You can download this package, or install it through Composer:
"require": {
"codezero/curl": "1.*"
}
Usage
Send requests, the easy way!
Create a Request instance:
$request = new \CodeZero\Curl\Request();
You can now use this for all of your requests. You don't need to create a new instance for every request.
Configure the request:
$url = 'http://my.site/api';
$data = ['do' => 'something', 'with' => 'this']; //=> Optional
$headers = ['Some Header' => 'Some Value']; //=> Optional
If you want you can set custom cURL options, before sending the request:
$request->setOption(CURLOPT_USERAGENT, 'My User Agent');
... or unset one:
$request->unsetOption(CURLOPT_USERAGENT);
NOTE: Only the URL, data and headers option, and a few options that are required for the given request method will be reset automatically on every request. Custom options will remain set until you unset them. An overview of all cURL options can be found here: http://php.net/manual/en/function.curl-setopt.php
Send the request: (one of the following)
$response = $request->get($url, $data, $headers);
$response = $request->post($url, $data, $headers);
$response = $request->put($url, $data, $headers);
$response = $request->patch($url, $data, $headers);
$response = $request->delete($url, $data, $headers);
All of these methods will return an instance of the CodeZero\Curl\Response class.
Get the response body
$body = $response->getBody();
Get additional request info
Fetch an array with all info:
$info = $response->info()->getList();
Fetch specific information:
$httpCode = $response->info()->getHttpCode(); //=> "200"
$responseType = $response->info()->getResponseType(); //=> "application/json"
$responseCharset = $response->info()->getResponseCharset(); //=> "UTF-8"
TIP: For an overview of all the available info, take a look at the
CodeZero\Curl\ResponseInfoclass or refer to http://php.net/manual/en/function.curl-getinfo.php
Exceptions
cURL issues
A CodeZero\Curl\CurlException will be thrown, if there was a problem initializing cURL. This will probably be the case if the cURL extension is not loaded by PHP, or if there is some other local server issue.
Request issues
A CodeZero\Curl\RequestException will be thrown, if cURL was unable to execute the request. This might be the case if your request is not properly configured (unsupported protocol, etc.). Find more information about these kind of errors on http://curl.haxx.se/libcurl/c/libcurl-errors.html
Response issues
HTTP response errors >= 400 will not throw an exception, unless you set the CURLOPT_FAILONERROR cURL option to true. If you do, these errors will also throw a CodeZero\Curl\RequestException, with the proper error message.
Another way to handle HTTP errors is to check the $httpCode value:
if ($httpCode >= 400)
{
// Handle error...
// or throw your own exception...
}
Curl Class
You can also use the Curl class instead of the Request class. The difference is that you will need to provide all of the cURL options yourself and you will just get the raw response back instead of the Response object.
Quick example:
$options = [
CURLOPT_URL => 'http://my.site/api',
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPGET => true,
CURLOPT_RETURNTRANSFER => true
];
$curl = new \CodeZero\Curl\Curl();
// Send & get results
$responseBody = $curl->sendRequest($options); //=> Returns the actual output
$info = $curl->getRequestInfo(); //=> Returns info array
// Get errors
$error = $curl->getError(); //=> For PHP < 5.5.0 this is the same as $curl->getErrorDescription()
$errorCode = $curl->getErrorCode();
$errorDescription = $curl->getErrorDescription();
// Close cURL resource
$curl->close();
Options are not reset automatically after each request. If you need to reset them, you can either run $curl->close() to force a new cURL resource to be initialized at the next request, or you can call $curl->reset().
Long example:
The following will do the exact same thing as the previous example:
$options = [
CURLOPT_URL => 'http://my.site/api',
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPGET => true,
CURLOPT_RETURNTRANSFER => true
];
$curl = new \CodeZero\Curl\Curl();
// Setup & send
$curl->initialize();
$curl->setOptions($options);
$curl->sendRequest();
// Get results
$responseBody = $curl->getResponse(); //=> Returns the actual output
$info = $curl->getRequestInfo(); //=> Returns info array
// Get errors
$error = $curl->getError();//=> For PHP < 5.5.0 this is the same as $curl->getErrorDescription()
$errorCode = $curl->getErrorCode();
$errorDescription = $curl->getErrorDescription();
// Close cURL resource
$curl->close();
Error handling:
The Curl class will only throw a CodeZero\Curl\CurlException, when there is some cURL initialization issue. You will need to check the error code to determine if the request had any problems:
if ($errorCode > 0)
{
// Do something...
}
Testing
$ vendor/bin/phpspec run
Security
If you discover any security related issues, please email ivan@codezero.be instead of using the issue tracker.
License
The MIT License (MIT). Please see License File for more information.
codezero/curl 适用场景与选型建议
codezero/curl 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 42.37k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2014 年 08 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「curl」 「http」 「api」 「request」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 codezero/curl 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 codezero/curl 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 codezero/curl 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
cURL and cURL based Soap-Client for PHP
repository php library
A PSR-7 compatible library for making CRUD API endpoints
A fluent PHP CURL wrapper
http客户端
Easily add Excepct-CT header to your project
统计信息
- 总下载量: 42.37k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 2
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-08-22