jpdik/request

A simple request class

1.1 2021-07-28 17:29 UTC

This package is auto-updated.

Last update: 2024-05-29 00:16:05 UTC


README

A simple request class for PHP

Instalation

composer require jpdik/request

Example use

Create a base_url:

$req = new Request("https://api.openweathermap.org/data/2.5");

Specify the rest of request and Type (GET, POST, PUT or DELETE) and get the response:

$res = $req->get("/weather", "?q=São joão del rei&appid=d15abc236092bbf9dd28fa2c0a7a02a9");

$status = res->getStatusCode();

if($status == 200 && $res){
  echo json_encode($res);
}

Methods

//GET
get($url, $option = null, array $headers = null, $timeout = null)

//POST
post($url, $body, $option = null, array $headers = null, $timeout = null)

//PUT
put($url, $body, $option = null, array $headers = null, $timeout = null)

//DELETE
delete($url, $option = null, array $headers = null, $timeout = null)

//Get the status from request
getStatusCode()

Example Headers

$headers = [
  "Content-Type: application/json",
  "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbiI6IjcyYTg0NDdhOTA3MGYyZTVmOGIzZDkzYzViZjE4MWE0In0.99kTzhSVwges69qprisg9B3rty4eKTTBurH-1lGKe30"];

if Headers it's not informed, the default is used as JSON:

$headers = [
  "Content-Type: application/json",
];

Full example

$req = new Request("https://api.openweathermap.org/data/2.5");

$res = $req->get("/weather", "?q=São joão del rei&appid=d15abc236092bbf9dd28fa2c0a7a02a9");

$status = res->getStatusCode();

if($status == 200 && $res){
  echo json_encode($res);
}