1. Go to this page and download the library: Download chaplean/api-client-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
chaplean / api-client-bundle example snippets
new EightPoints\Bundle\GuzzleBundle\EightPointsGuzzleBundle(),
new Chaplean\Bundle\ApiClientBundle\ChapleanApiClientBundle(),
// If you want to enable Database logging
new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
// If you want to enable Email logging
new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
new Symfony\Bundle\TwigBundle\TwigBundle()
use Chaplean\Bundle\ApiClientBundle\Api\AbstractApi;
use Chaplean\Bundle\ApiClientBundle\Api\Parameter;
use GuzzleHttp\ClientInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class FakeApi extends AbstractApi
{
protected $url;
/**
* AbstractApi at the END of the construct() function.
parent::__construct($client, $eventDispatcher);
}
/**
* We define our api here, we'll dig into this in the next section
*/
public function buildApi()
{
$this->globalParameters()
->urlPrefix($this->url) // here we set base url
->expectsJson();
$this->get('fake_get', 'fake')
->urlParameters([
'id' => Parameter::id(),
]);
}
}
public function buildApi()
{
/*
* You have to call this function first to set basic config
*/
$this->globalParameters()
->urlPrefix('http://some.url/') // set the base url of our api
->urlSuffix('/some-suffix') // Configure a suffix on our url (Optional, default: empty)
/*
* We can then set some configurations that will be the default for every route we create later.
* You have the exact same api available here and available when configuring routes.
* See route definition for detailed descriptions of headers(), urlParameters(), queryParameters() and requestParameters()
*/
->expectsPlain() // Declare we expect responses to be plain text
->expectsJson() // Declare we expect responses to be json
->expectsXml() // Declare we expect responses to be xml
->sendFormUrlEncoded() // Configure that we post our data as classic form url encoded
->sendJson() // Configure that we post our data as json
->sendXml() // Configure that we post our data as xml
->sendJSONString() // Configure that we post our data as a url-encoded key-value pair where the key is JSONString and the value is the request data in json format
->headers([]) // Configure what headers we send
->urlParameters([]) // Configure what url placeholders we define
->queryParameters([]) // Configure what query strings we send
->requestParameters([]); // Configure what post data we send
/*
* Here we define the core of our api, the routes. We can use get(), post(), put(), patch(), delete() functions
* with a route name and a route url (with placeholders in you want) to define routes.
*/
$this->get('query_one', 'data/{id}');
$this->post('create_one', 'data');
$this->patch('update_one', 'data/{id}');
$this->put('update_one', 'data/{id}');
$this->delete('delete_one', 'data/{id}');
/*
* Those function return the route object to further configure it.
* As said previously the route api is the same as the one we get with globalParameters().
*/
$this->post('create_one', 'data/{id}')
->expectsPlain() // Declare we expect responses to be plain text
->expectsJson() // Declare we expect responses to be json
->expectsXml() // Declare we expect responses to be xml
->sendFormUrlEncoded() // Configure that we post our data as classic form url encoded
->sendJson() // Configure that we post our data as json
->sendXml() // Configure that we post our data as xml
->sendJSONString() // Configure that we post our data as a url-encoded key-value pair where the key is JSONString and the value is the request data in json format
->headers([]) // Configure what headers we send
->urlParameters([]) // Configure what url placeholders we define
->queryParameters([]) // Configure what query strings we send
->allowExtraQueryParameters() // Allow extra field in query parameters
->requestParameters([]) // Configure what post data we send
->allowExtraQueryParameters(); // Allow extra field in request parameters
/*
* Finally calling headers(), urlParameters(), queryParameters() or requestParameters() without configuring parameters is sort of useless.
* So let's see how to define parameters.
*/
$this->put('update_data', 'data/{id}')
->urlParameters( // Define the placeholder parameter for the {id} in the url
[
'id' => Parameter::id(),
]
)
/*
* We define a list of key => values pairs where key is the name of the parameter and the value is a parameter type.
*/
->requestParameters(
[
'name' => Parameter::string(),
'birthday' => Parameter::dateTime('Y-m-d'),
'is_human' => Parameter::bool()->defaultValue(true),
'height' => Parameter::int(),
'weight' => Parameter::float()->optional(),
'tags' => Parameter::object(
[
'id' => Parameter::id(),
'name' => Parameter::string(),
]
),
'friends' => Parameter::arrayList(Parameter::id()),
'enum' => Parameter::enum(['foo', 'bar']),
]
);
/*
* Last but not least, you can also directly give any instance of Parameter. Here we use ArrayParameter.
*/
->requestParameters(Parameter::arrayList(
Parameter::object(
[
'id' => Parameter::id()
]
)
))
/*
* Passing an array is actually a shortcut that implies ObjectParameter since it's the most common.
*
* The following two definitions are equivalent.
*/
->requestParameters(
[
'id' => Parameter::id()
]
)
->requestParameters(Parameter::object(
[
'id' => Parameter::id()
]
));
}
// Options available for all types of Parameter
Parameter::xyz()
->optional() // Define the parameter optional
->defaultValue('value') // Define a default value for the field
// Options specific to object Parameter
Parameter::object()
->allowExtraField() // Allow sending a field not defined in the configuration
class FakeApi
{
...
public function buildApi()
{
$this->get('user', '/user/{id}')
->urlParameters(['id' => Parameter::id()])
->expectsJson()
}
...
}
$response = $api->getUser() // the get('user', '/user/{id}') definition added a getUser() method
->bindUrlParameters(['id' => 42]) // we provide a value for the 'id' parameter
->exec(); // we can now execute the request
if ($response->succeeded()) { // Was the response a 2xx?
// The request suceeded.
$content = $response->getContent(); // Get the body of the response,
// will be a string for plain text
// and associative array for json and xml.
...
} else {
$violations = $response->getViolations(); // If the provided parameters were invalid.
// this will contain the violations.
if (!empty($violations)) {
// The request failed because of invalid parameters.
...
} else {
// The request failed due to a network issue or the response was not a 2xx.
...
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.