lowem / easy-curl

允许您轻松地向 API 终端发送请求

v5.0.1 2021-08-29 03:30 UTC

This package is auto-updated.

Last update: 2024-09-17 00:31:32 UTC


README

Easy Curl

Status GitHub issues GitHub pull requests GitHub GitHub tag (latest SemVer)

这是一个 cURL 的 PHP 封装器

📝 目录

🧐 关于

此项目旨在帮助开发者轻松地与底层 cURL API 交互。

🏁 入门

以下步骤将使 Easy Curl 运行。

先决条件

为了安装此软件包,您必须安装 composer,可以通过根据您的系统遵循以下步骤来完成:[此处](https://composer.php.ac.cn/doc/00-intro.md)。

如果您尚未这样做,请现在在项目根目录中运行 composer init,开始使用 composer。只需按照提示操作即可。

安装

要在项目根目录中安装 Easy Curl,请运行以下命令。

composer require lowem/easy-curl

创建一个新 PHP 文件,并将以下代码添加到文件的顶部以自动加载该软件包以及您可能已安装的其他任何软件包。使用 use 语句可以防止您输入完整的包命名空间。

require_once "vendor/autoload.php";
use Lowem\EasyCurl\EasyCurl;

🎈 API 使用

GET 请求 get(header)

  • header:这是一个包含 HTTP 头字段 的数组
    • 示例:
      [
        "Content-Type: application/json",
        "Accept: application/json"
      ]

    示例用法

    $test = new EasyCurl("https://jsonplaceholder.typicode.com/posts");
    try {
      $test->get();
    } catch (HTTPRequestException $e) {
      echo $e->getCustomMessage();
    }
    print_r($test->getExecMessage());
    
    $test->close();

POST 请求 post(postFields, header)

  • postFields:这是您想要发送到 API 的数据。它可以 JSON、XML、键值对数组等。
    • 示例:
      [
        "title" => "foo",
        "body" => "bar",
        "userId" => 1
      ]
  • header:这是一个包含 HTTP 头字段 的数组
    • 示例:
      [
        "Content-Type: application/json",
        "Accept: application/json"
      ]

    示例用法

    $test = new EasyCurl("https://jsonplaceholder.typicode.com/posts");
    try {
      $data = [
        "title" => "foo",
        "body" => "bar",
        "userId" => 1
      ];
      $test->post(json_encode($data), [
        "Content-type: application/json; charset=UTF-8",
      ]);
    } catch (HTTPRequestException $e) {
      echo $e->getCustomMessage();
    }
    print_r($test->getExecMessage());
    
    $test->close();

PUT 请求 put(postFields, header)

  • postFields:这是您想要发送到 API 的数据。它可以 JSON、XML、键值对数组等。
    • 示例:
      [
        "id" => 1,
        "title" => "foo",
        "body" => "bar",
        "userId" => 1
      ]
  • header:这是一个包含 HTTP 头字段 的数组
    • 示例:
      [
        "Content-Type: application/json",
        "Accept: application/json"
      ]

    示例用法

    $test = new EasyCurl("https://jsonplaceholder.typicode.com/posts/1");
    try {
      $data = [
        "id" => 1,
        "title" => "foo",
        "body" => "bar",
        "userId" => 1
      ];
      $test->put(json_encode($data), [
        "Content-type: application/json; charset=UTF-8",
      ]);
    } catch (HTTPRequestException $e) {
      echo $e->getCustomMessage();
    }
    print_r($test->getExecMessage());
    
    $test->close();

DELETE 请求 delete()

    示例用法

    $test = new EasyCurl("https://jsonplaceholder.typicode.com/posts/1");
    try {
      $test->delete();
    } catch (HTTPRequestException $e) {
      echo $e->getCustomMessage();
    }
    print_r($test->getExecMessage());
    
    $test->close();

✍️ 作者

还可以查看参与此项目的贡献者列表