athlon1600/php-curl-client

简单的 PHP cURL 客户端

v1.2.0 2023-10-24 23:17 UTC

This package is auto-updated.

Last update: 2024-09-15 00:41:01 UTC


README

GitHub Workflow Status (with event) Packagist Downloads (custom server)

PHP Curl 客户端

一个非常简单的 cURL 客户端 - 不到 100 行代码。非常适合作为基类。

安装

composer require athlon1600/php-curl-client "^1.0"

示例

use Curl\Client;

$client = new Client();

// returns standardized Response object no matter what
$response = $client->get('https://stackoverflow.com');

// 200
$status = $response->status;

// HTML content
$body = $response->body;

// curl_error() OR null
$error = $response->error;

// CurlInfo instance
$info = $response->info;

更新: $response->info 现在返回一个对象,将在您的 IDE 上自动完成。

;

也适用于 POST 请求

$client->post('http://httpbin.org/post', ['username' => 'bob', 'password' => 'test']);

或者您可以发起一个完全定制的请求

$client->request('GET', 'https://www.whatismyip.com/', null, [
    'User-Agent' => 'Mozilla/5.0 (iPhone; CPU iPhone OS 12_0 like Mac OS X)'
], [
    CURLOPT_PROXY => '127.0.0.1:8080',
    CURLOPT_TIMEOUT => 10
]);

待办事项

  • 使其兼容 PSR-7 和 PSR-18