A simple REST client with PHPs cURL.
composer require chriha/rest-client
Define your options
$options = [
'url' => 'http:https://api.localhost/v1',
];
See \Chriha\Clients\Rest::getDefaultOptions()
for all default options.
$rest = new \Chriha\Clients\Rest( $options );
$rest->get( '/posts' );
$post = [
"title" => "lorem",
"body" => "lorem ipsum dolor set"
];
$rest = new \Chriha\Clients\Rest( $options );
$rest->post( '/posts', $post );
$post = [
"title" => "lorem"
];
$rest = new \Chriha\Clients\Rest( $options );
$rest->put( '/posts/1', $post );
$rest->patch( '/posts/1', $post );
$rest = new \Chriha\Clients\Rest( $options );
$rest->delete( '/posts/1' );
Recommended only in dev environment, so default is false
$options = [
'allow_self_signed' => true,
];
$options = [
'curl_options' => [...],
];
$options = [
'authentication' => 'oauth1',
'token' => 'YOUR_API_TOKEN',
'secret' => 'YOUR_API_SECRET',
];
Make an alias like alias rest='vendors/bin/rest'
for simpler usage of the client inside the project.
With the following command you can do a request via the rest client.
$ ./rest GET http:https://api.localhost.io/v1/posts "parameters=specified&as=simple&query=string" "Content-Type:application/json;Accept-Charset: utf-8"
If you want to use token
and secret
for your authentication, you can place them as JSON in the .rest
file of your project root:
{
"token": "YOUR_API_TOKEN",
"secret": "YOUR_API_SECRET"
}
The output of the rest client will be shown as the following:
Request took 23.45ms
Response Code: 200
Response Body:
{
"meta": "info",
"data": [
{
"title": "lorem"
}
]
}