faso-dev/simple-curl-client

一个用于发送GET和POST请求的curl包装客户端

v0.0.2 2023-02-09 00:07 UTC

This package is auto-updated.

Last update: 2024-09-09 03:41:42 UTC


README

Simple Curl Client是一个简单的PHP库,用于使用cURL扩展发送HTTP请求。此包是基本GET和POST请求的简单实现。

要求

在开始使用此包之前,您需要安装以下要求

  • PHP 7.4或更高版本
  • ext-curl
  • ext-json

安装

您可以使用composer安装此包

composer require faso-dev/simple-curl-client

用法

安装此包后,您可以在代码中使用它

require __DIR__ . '/vendor/autoload.php';

use FasoDev\SimpleCurlClient\CurlClient;
use FasoDev\SimpleCurlClient\CurlClientBuilder;

$client = CurlClientBuilder::create()
	->defineProxySslVerifyPeer(false)
	->defineTimeOut(30)
	->defineConnectTimeOut(30)
	->defineUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36')
	->build()
;

$client->get('https://httpbin.org/get');
$client->get('https://httpbin.org/get', ['foo' => 'bar']);
$client->post('https://httpbin.org/post', ['foo' => 'bar']);
$client->post('https://httpbin.org/post', ['foo' => 'bar'], [
    'headers' => [
        'Content-Type' => 'application/json',
        'Accept' => 'application/json',
    ],
    CURLOPT_SSL_VERIFYPEER => true,
]);

get方法用于发送GET请求,并具有以下参数

  • $url (string): 要发送请求的URL。
  • $queries (array): 要添加到URL中的查询参数。
  • $options (array): 要添加到请求中的选项。注意,在$options数组中有一个headers键,可以添加请求头。

post方法用于发送POST请求,并具有以下参数

  • $url (string): 要发送请求的URL。
  • $data (array): 要发送到URL的数据。
  • $options (array): 要添加到请求中的选项。注意,在$options数组中有一个headers键,可以添加请求头。

贡献

欢迎提交pull请求。对于重大更改,请首先打开一个问题来讨论您想要更改的内容。

许可

MIT