Skip to content

Vanilla PHP functions to handle sending and receiving HTTP requests with CURL.

License

Notifications You must be signed in to change notification settings

jyoungblood/http-request

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

VPHP - HTTP Request

Vanilla PHP functions to handle sending and receiving HTTP requests with CURL. This is a set of thin abstractions over a typical CURL request workflow with configurable options.

Installation

Easy install with composer:

composer require jyoungblood/http-request
use VPHP\http;
require __DIR__ . '/vendor/autoload.php';

Usage

http::request($url, $parameters)

Makes an http request to a given url, sending an array of data and returning the raw response.

$api_data = http::request('https://external-api.com/v3/example-response', [
  'method' => 'POST', // optional, GET by default, GET and POST supported currently
  'json_decode' => true, // optional, returns an expected JSON response as a PHP array
  'debug' => true, // optional, returns all request information from curl_getinfo()
  'headers' => [ // optional, define any custom header
    'Cache-Control' => 'no-cache',
    'Content-Type' => 'application/json',
  ],
  'data' => [ // optional, will be submitted as querystring (GET) or FormData (POST)
    'user_id' => 581146,
    'api_key' => '696719xvckvzxspigh24y1e-b'
  ]
]);

http::get($url, $parameters)

Alias to http::request using the default GET method.

$api_data = http::get('https://external-api.com/v3/example-response', [
  'data' => [
    'user_id' => 581146,
    'api_key' => '696719xvckvzxspigh24y1e-b'
  ]
]);

Everything in the the 'data' array will be submitted as a querystring. For example:

https://external-api.com/v3/example-response?user_id=581146&api_key=696719xvckvzxspigh24y1e-b

http::post($url, $parameters)

Alias to http::request using the POST method.

$api_data = http::post('https://external-api.com/v3/example-response', [
  'data' => [
    'user_id' => 581146,
    'api_key' => '696719xvckvzxspigh24y1e-b'
  ]
]);

Everything in the 'data' array will be submitted as FormData.

http::json($url, $parameters)

Alias to http::request() using the json_decode parameter (returns an expected JSON response as a PHP array)

$api_data = http::json('https://external-api.com/v3/example-response', [
  'data' => [
    'user_id' => 581146,
    'api_key' => '696719xvckvzxspigh24y1e-b'
  ]
]);

About

Vanilla PHP functions to handle sending and receiving HTTP requests with CURL.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages