PHP Curl 包装器,支持多 cURL。使用 CurlRequest 和 CurlResponse 对象简单易用。

v1.1 2018-12-07 10:06 UTC

This package is auto-updated.

Last update: 2024-09-28 04:08:38 UTC


README

Build Status Scrutinizer Code Quality Code Coverage GPL Licence

此库是 "https://github.com/php-curl-class/php-curl-class" 的替代品。使用 CurlRequest 和 CurlResponse 对象简单易用。

安装

通过 Composer 安装

$ composer require php-guard/curl

要求

  • php: ^7.1

使用方法

快速入门
require __DIR__ . '/vendor/autoload.php';

use \PhpGuard\Curl\Curl;
use \PhpGuard\Curl\CurlError;

$this->curl  =  new  Curl();
	
try {
	// Execute a single request
	$response = $this->curl->get('http://example.com'); // Return a CurlResponse
	echo $response->raw();

	// Execute multiple requests
	$responses = $this->curl->multi() // Create a MultiCurl object
		->get('http://example.com')   // Add a get request
		->post('http://example.com')  // Add a post request
		->execute();                  // Return an array of CurlResponse

	foreach ($responses as $response) {
		echo $response->raw();
	}
} catch (CurlError $e) {
	echo 'Error: ' . $curl->getCode(). ': ' . $curl->getMessage(). "\n";
}
可用方法

\PhpGuard\Curl\Curl

HTTP 方法

get(string $url, $query = null, array $headers = []): CurlResponse
post(string $url, $data = null, $query = null, array $headers = []): CurlResponse
put(string $url, $data = null, $query = null, array $headers = []): CurlResponse
patch(string $url, $data = null, $query = null, array $headers = []): CurlResponse
delete(string $url, $data = null, $query = null, array $headers = []): CurlResponse

运行单个请求

execute(CurlRequest  $request): CurlResponse

准备多个请求

multi(array  $options  = []): MultiCurl

编辑请求和响应处理(高级使用)

getCurlRequestFactory(): CurlRequestFactory
getRequestModifierPipeline(): RequestModifierPipeline

\PhpGuard\Curl\CurlRequest

您可以使用 Curl 执行请求,或者可以使用 CurlRequestFactory 返回 CurlRequest 实例。

url、method 和 data 属性有 getter 和 setter。

方法 setHeaderContentType(string $contentType)

$curlRequest->getHeaders['Content-Type'] = $contentType

其他方法

execute(bool $throwExceptionOnHttpError = false): CurlResponse
getCurlOptions(): CurlOptions
getHeaders(): Headers

\PhpGuard\Curl\MultiCurl

get(string $url, $query = null, array $headers = []): self
post(string $url, $data = null, $query = null, array $headers = []): self
put(string $url, $data = null, $query = null, array $headers = []): self
patch(string $url, $data = null, $query = null, array $headers = []): self
delete(string $url, $data = null, $query = null, array $headers = []): self
execute(): CurlResponse[]

\PhpGuard\Curl\CurlResponse

statusCode(): int
isError(): bool // True if status code >= 300
headers(): Headers
raw() // Raw content of the response
json() // Array or false if not a json response

\PhpGuard\Curl\CurlRequestFactory

为所有请求设置基本 URL

 setBaseUrl(?string $baseUrl): self

设置 curl CURLOPT_SSL_VERIFYPEER 选项

 setSslVerifyPeer(bool $value): self

创建一个 CurlRequest

 create(string $method, string $url, $data = null, $query = null, array $headers = []): CurlRequest

编辑其他默认 cURL 选项

 getDefaultCurlOptions(): CurlOptions
 getDefaultHeaders(): Headers

\PhpGuard\Curl\Collection\CurlOptions

此类实现了 \ArrayAccess。因此可以用作数组。

\PhpGuard\Curl\Collection\Headers

此类实现了 \ArrayAccess。因此可以用作数组。

此外,键名不区分大小写。

\PhpGuard\Curl\RequestModifierPipeline

添加对象以修改 CURL 请求

pipe(RequestModifierInterface $requestModifier): self

默认情况下,FileRequestModifier 和 PlainTextRequestModifier 是活动的。如有必要,您可以添加 ProxyRequestModifier 的实例

  • ProxyRequestModifier 允许您定义用于使用代理的 cURL 选项

  • FileRequestModifier 用于管理以 @ 开头的文件路径,并通过将它们转换为 CurlFile 并修改 HTTP Content-Type 标头来传递作为参数。

  • PlainTextRequestModifier 在将字符串作为参数传递时将 HTTP Content-Type 标头更改为 text/plain。