lowem / easy-curl
Allow you to make requests to API end points easily
Requires
- ext-curl: *
Requires (Dev)
- ext-json: *
- phpunit/phpunit: ^9.5
README
Easy Curl
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();
- Example:
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 ]
- Example:
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();
- Example:
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 ]
- Example:
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();
- Example:
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
- @Lowe-Man - Idea & Initial work
See also the list of contributors who participated in this project.