lowem/easy-curl

Allow you to make requests to API end points easily

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

This package is auto-updated.

Last update: 2024-05-16 23:48:06 UTC


README

Easy Curl

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

This is a PHP wrapper for cURL

📝 Table of Contents

🧐 About

This project was created to help developers easily interact with the underlying cURL API.

🏁 Getting Started

These instructions will get you a copy of Easy Curl up and running.

Prerequisites

In order to install this package you have to install composer which can be done by following the steps based on your system here.

If you have not done so already run composer init in the root of your project directory, do so now to start using composer. Just follow the prompts as they appear.

Installing

To install Easy Curl run the command below while in your project root.

composer require lowem/easy-curl

Create a new PHP file and add the code below to the top of the file to automatically load in the package as well as any others you may have installed. The use statement prevents you from having to type in the full namespace of the package.

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

🎈 API Usage

GET Request get(header)

  • header: This is an array of HTTP header fields
    • Example:
      [
        "Content-Type: application/json",
        "Accept: application/json"
      ]

    Example Usage:

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

POST Request post(postFields, header)

  • postFields: This is the data that you want to send to the API. It can be JSON, XML, an array with key value pairs, etc.
    • Example:
      [
        "title" => "foo",
        "body" => "bar",
        "userId" => 1
      ]
  • header: This is an array of HTTP header fields
    • Example:
      [
        "Content-Type: application/json",
        "Accept: application/json"
      ]

    Example Usage:

    $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 Request put(postFields, header)

  • postFields: This is the data that you want to send to the API. It can be JSON, XML, an array with key value pairs, etc.
    • Example:
      [
        "id" => 1,
        "title" => "foo",
        "body" => "bar",
        "userId" => 1
      ]
  • header: This is an array of HTTP header fields
    • Example:
      [
        "Content-Type: application/json",
        "Accept: application/json"
      ]

    Example Usage:

    $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 Request delete()

Example Usage:

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

$test->close();

✍️ Authors

See also the list of contributors who participated in this project.