hidayat / restclient
A Package for handling rest api request in laravel, This package provide interface over PHP cURL Api
Requires
- php: >=5.4.0
This package is auto-updated.
Last update: 2024-06-29 04:58:34 UTC
README
laravel-restclient
This provide calling rest api from laravel
composer require hidayat/restclient
usage
Include the facade provided by package in your class as belows
use hidayat\restclient\Facade\Client;
$url = "http://some-site.com";
//simple GET request with no additional param
$response = Client::call($url);
dd($response)
// GET request with query param and headers
$data = ['field1' => 'value1', 'field2' => 'value2'];
$headers = ['Authorization: Basic ' . $token, 'accept: application/json'];
$response = Client::setGetData($data)
->setHeaders($headers)
->call($url);
Note: All methods are chainable except the call() method when setting any additional param call() method should be call at the end
//example with post method and post data
$headers = ['content-type: application/x-www-form-urlencoded', Authorization: Basic ' . $token, 'accept: application/json'];
$postData = "field1=somevalue&field2=somefield2";
Note: when sending post data then there is no need to explicitly set method to post as it will be automatically set. When sending post request without any data you must set method to post
$response = Client::setPostData($postData)
->setMethod('post')
->setHeaders($headers)
->call($url);
//PUT method example
$response = Client::setPostData($postData)
->setMethod('put')
->setHeaders($headers)
->call($url);
//For url that require BasicHttpAuth you can use setHttpBasicAuthData method
$basicAuthCred = ['username' => 'abc', 'password' => 'bdjdjdjd']
$response = Client::setHeaders($headers)
->setHttpBasicAuthData($basicAuthCred)
->call($url);
Since this package is an interface over PHP cURL Api you can set any of the cURL Options by passing an array to setOptions method
You must provide Option key value pair format as below
$response = Client::setOptions([
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_TIMEOUT => 60
])
->call($url);