codezero / curl

简单的cURL封装

1.1.1 2015-02-19 19:53 UTC

This package is auto-updated.

Last update: 2024-09-11 13:57:02 UTC


README

GitHub release License Build Status Scrutinizer Total Downloads

此包封装了大多数cURL函数,通过专门的Curl类使其更具面向对象,并略微易于使用。

更重要的是,Request类提供了一些友好的方法,可以为您设置最常用的cURL选项,因此您无需记住这些。

这是一个 简单 的cURL封装。一些功能(目前)不支持

  • 包括响应输出中的头部(CURLOPT_HEADER
  • cURL多处理
  • cURL共享处理

安装

您可以下载此包,或通过Composer安装它

"require": {
	"codezero/curl": "1.*"
}

使用方法

轻松发送请求!

创建一个Request实例

$request = new \CodeZero\Curl\Request();

现在您可以使用此实例发送所有请求。您无需为每个请求创建新的实例。

配置请求

$url = 'http://my.site/api';
$data = ['do' => 'something', 'with' => 'this']; //=> Optional
$headers = ['Some Header' => 'Some Value']; //=> Optional

如果您想,您可以在发送请求之前设置自定义cURL选项

$request->setOption(CURLOPT_USERAGENT, 'My User Agent');

... 或者取消设置

$request->unsetOption(CURLOPT_USERAGENT);

注意: 每次请求都会自动重置URL、数据和头部选项,以及为给定请求方法所需的一些选项。自定义选项将保留设置,直到您取消设置。所有cURL选项的概述请见此处:https://php.ac.cn/manual/en/function.curl-setopt.php

发送请求:(以下任选其一)

$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);

所有这些方法都将返回CodeZero\Curl\Response类的一个实例。

获取响应体

$body = $response->getBody();

获取额外的请求信息

获取所有信息的数组

$info = $response->info()->getList();

获取特定信息

$httpCode = $response->info()->getHttpCode(); //=> "200"
$responseType = $response->info()->getResponseType(); //=> "application/json"
$responseCharset = $response->info()->getResponseCharset(); //=> "UTF-8" 

提示: 要查看所有可用信息,请参阅CodeZero\Curl\ResponseInfo类或参考https://php.ac.cn/manual/en/function.curl-getinfo.php

异常

cURL问题

如果在初始化cURL时出现问题,将抛出CodeZero\Curl\CurlException异常。这可能是因为PHP没有加载cURL扩展,或者存在其他本地服务器问题。

请求问题

如果cURL无法执行请求,将抛出CodeZero\Curl\RequestException异常。这可能是因为您的请求配置不当(不支持的协议等)。有关此类错误的更多信息,请参阅http://curl.haxx.se/libcurl/c/libcurl-errors.html

响应问题

HTTP响应错误 >= 400 不会抛出异常,除非您将CURLOPT_FAILONERROR cURL选项设置为true。如果这样做,这些错误也会抛出带有正确错误消息的CodeZero\Curl\RequestException异常。

处理HTTP错误的另一种方式是检查$httpCode

if ($httpCode >= 400)
{
	// Handle error...
	// or throw your own exception...
}

Curl类

您还可以使用Curl类代替Request类。区别在于您需要自己提供所有cURL选项,并且您将只获取原始响应,而不是Response对象。

快速示例

$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();

请求后,选项不会自动重置。如果您需要重置它们,可以运行$curl->close()强制在下一个请求时初始化新的cURL资源,或者您可以调用$curl->reset()

长示例

以下将执行与上一个示例完全相同的功能

$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();

错误处理

Curl类仅在存在cURL初始化问题时才会抛出CodeZero\Curl\CurlException。您需要检查错误代码以确定请求是否存在任何问题

if ($errorCode > 0)
{
	// Do something...
}

测试

$ vendor/bin/phpspec run

安全性

如果您发现任何安全相关的问题,请发送电子邮件至ivan@codezero.be,而不是使用问题跟踪器。

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件

Analytics