drewlabs / curl-client

提供基于 cURL 库的 PHP 类,围绕 PHP cURL 对象和 Psr18 客户端

v0.4.0 2024-05-23 18:09 UTC

README

该项目倾向于实现一个面向对象的 PHP cURL 客户端,并提供基于 cURL 客户端对象的 PSR18 实现。

安装

推荐使用 PHP 包管理器 composer 安装库,运行以下命令

composer require drewlabs/curl-client

用法

该库包含两个 cURL 客户端,第一个是基于 PHP cURL 库的对象化 cURL 类和 PSR18 兼容客户端。

OOP cURL 客户端

要创建客户端的新实例,使用类构造函数

use Drewlabs\Curl\Client;

// Creates an instance of the cURL client
$client = new Client();

这将创建一个新的客户端实例并初始化一个新的 cURL 会话。开发者可以向客户端实例传递一个基本 URL 以定义主机服务器 URL

use Drewlabs\Curl\Client;

// Creates an instance of the cURL client
$client = new Client('http://127.0.0.1:8000');

/ Passing request options to the cURL client
$client = new Client([
    'base_url' => 'http://127.0.0.1:5000',
    'headers' => [
        'Content-Type' => 'application/json',
    ],
    'cookies' => [
        'clientid' => '...',
        'clientsecret' => '...'
    ]
]);


// Create request client with a base URL
$client = new Client('http://127.0.0.1:5000');

Curl 选项

cURL 选项允许开发者自定义发送到应用服务器的 cURL 请求。使用 Client::setOption()Client::setOptions()(对于数组类型选项),开发者可以自定义发送到服务器的 cURL 请求。

use Drewlabs\Curl\Client;

// Creates an instance of the cURL client
$client = new Client(/* Parameters */);

$client->setOption(\CURLOPT_RETURNTRANSFER, false);
$client->setOption(\CURLOPT_WRITE, function($curl, $write) {
    // Listener for response output from the curl session
});

为了将 cURL 选项作为数组传递,我们使用 setOptions() 数组方法

use Drewlabs\Curl\Client;

// Creates an instance of the cURL client
$client = new Client(/* Parameters */);

$client->setOptions([
    \CURLOPT_CUSTOMREQUEST  => 'POST',
    \CURLOPT_URL            => 'http://127.0.0.1:300/api/posts',
    \CURLOPT_RETURNTRANSFER => true,
    \CURLOPT_HEADER         => false,
    \CURLOPT_CONNECTTIMEOUT => 150,
]);

发送 cURL 请求

客户端为开发者提供 Client::send() 方法,用于向服务器发送 HTTP 请求

use Drewlabs\Curl\Client;

// Creates an instance of the cURL client
$client = new Client(/* Parameters */);

// ... Use pre-configure cURL options
$client->send();

// Passing request options
$client->send([
    'headers' => [
        'Content-Type' => 'application/json'
    ],
    'body' => [
        'title' => 'Hello World'
    ]
]);

// To specify the request url when sending the request
$client->send('GET', 'http://127.0.0.1:3000/api/posts');

// or simply a path
$client->send(null, '/api/posts');

// To override or provide the request method
$client->send('POST', '/api/posts');

有时你可能已经构建了 cURL 请求,并希望简单地执行 cURL 请求。为此,客户端通过提供 Client::exec() 方法来模仿 cURL 的 exec() 方法,用于向服务器发送请求。

use Drewlabs\Curl\Client;

// Creates an instance of the cURL client
$client = new Client(/* Parameters */);

$client->setRequestMethod('POST');
$client->setRequestUri('http://127.0.0.1:5000/api/posts');
$this->setOption(CURLOPT_HTTPHEADER, [
    'Content-Type: application/json',
    'Accept: */*'
])
$client->setOption(\CURLOPT_POSTFIELDS, json_encode([/* JSON fields*/]));

// Execute the Curl request
$client->exec();
  • 辅助方法

客户端包含一些用于基本 cURL HTTP 选项的辅助方法,如 CURLOPT_URL\CURLOPT_SSL_VERIFYPEER\CURLOPT_HTTP_VERSION 等...以下是大多数常用用例的简要 API 定义

Client::setRequestUri(string $url) - 设置请求 URI Client::setRequestMethod(string $method) - 设置请求方法 Client::setProtocolVersion(string $version) - 设置 HTTP 协议版本 Client::withAutoReferer() - 自动设置自动引用头,在它跟随重定向时 Client::followLocation() - 跟随任何位置重定向,除非设置了 \CURLOPT_MAXREDIR。 Client::maxRedirects(int $n) - cURL 客户端要跟随的重定向次数 Client::proxy($proxy, $port = null, $username = null, $password = null) - 定义一个通过它转发请求的 HTTP 代理。 Client::setUserAgent($user_agent) - 定义请求用户代理 Client::timeout(int $milliseconds) - 为指定毫秒数的请求设置超时 Client::verifyHost() - 确保 cURL 客户端验证主机域名和 SSL 证书

注意 默认情况下,当对象被销毁时,cURL 连接由 PHP 运行时关闭。如果开发者想关闭 cURL 会话或重置 cURL 参数,实例提供 close()release() 方法以处理每种具体情况

use Drewlabs\Curl\Client;

// Creates an instance of the cURL client
$client = new Client(/* Parameters */);

// Sending the cURL request
$client->send();

// Closing the cURL session
$client->close();

// or reset cURL resources
$client->release();