Prest
Prest is a PHP REST client library based on Pest.
In a nutshell Prest is a wrapper around Pest that let you write RESTful client in
a more elegant way (imho)., (*1)
Installation
Via composer cli:, (*2)
composer require zangue/prest
or in your composer.json file:, (*3)
{
"require": {
"zangue/prest": "1.0.0"
}
}
Basic Usage
$factory = new PrestFactory('http://example.com');
$rest = $factory->create()
->url('/resource')
->withHeader('Authorization', 'Basic ' . OAUTH_BASIC))
->contentType('application/x-www-form-urlencoded')
->accept('application/json')
->data('data1', value1)
->data('data2', value2)
->viaPost()
->execute();
if ($rest->succeed()) {
var_dump($rest->getResponseBody());
} else {
...
}
$rest->reset()
->url('/delete/13')
->viaDelete()
->execute();
if ($rest->failed()) {
$e = $rest->getException();
var_dump($e->getMessage());
}
$rest2 = $factory->create('http://base-url.com')
->url('/uri')
->viaGet()
->execute();
...
Just as with Pest one can use the createJSON() and createXML() factory methods to
create JSON and XML-centric version of Prest., (*4)
API
Prest url (string $value)
Set the resource URL., (*5)
string getUrl ()
Returns the resource URL, empty string if not set., (*6)
Prest withHeader (string $key, string $value)
Adds a header., (*7)
array getHeaders ()
Returns the request headers, empty array if no headers were set., (*8)
Prest contentType (string $value)
Shortcut: adds Content-Type header., (*9)
Prest data (mixed $key, mixed $value)
Adds request data, (*10)
Prest arrayData (array $data)
Use this method to pass a prepared array of data. Argument has to be an array else
nothing will happened!, (*11)
array getData ()
Returns the request data, empty array if no data were set., (*12)
Prest withCookie (string $name, string $value)
Add a cookie., (*13)
array getCookies ()
Returns array of cookies or empty array if no cookies were added to the request., (*14)
Prest withAuth (string $user, string $pass, string $auth = 'basic')
Setup authentication. $auth can basic (default) or disgest., (*15)
Prest withProxy (string $host, int $port, [string $user, string $pass])
Setup proxy., (*16)
Prest function curlOpts (string $option, mixed $value)
Set cURL option., (*17)
boolean succeed ()
boolean failed()
Tells if the request was successful/failed., (*18)
int getStatus ()
Get last response status code, (*19)
Exception getException ()
Return the raised exception in case of failure., (*20)
mixed getResponseBody ()
Returns the last response body on success. This method will return an associative array or
a SimpleXMLElement if the Prest object was created using createJSON or createXML
factory method respectively., (*21)
boolean responseHasHeader (string $header)
Checks if last response has a specific header., (*22)
string getResponseHeader (string $header)
Returns the last response header (case insensitive) or NULL if not present., (*23)
Prest viaGet ()
Prest viaPost ()
Prest viaPut ()
Prest viaPatch ()
Prest viaHead ()
Prest viaDelete ()
Use HTTP GET/POST/PUT/PATCH/HEAD/DELETE method., (*24)
Prest execute ()
Executes the request., (*25)