A Guzzle 5 Bundle
Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:
$ composer require Kopjra/GuzzleBundle "~1"This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.
Then, enable the bundle by adding the following line in the app/AppKernel.php
file of your project:
<?php
// app/AppKernel.php
// ...
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
// ...
new Kopjra\GuzzleBundle\KopjraGuzzleBundle(),
);
// ...
}
// ...
}/** @var \GuzzleHttp\Client $client */
$client = $this->get("guzzle");
/** @var \Kopjra\GuzzleBundle\OAuth\OAuthSubscriber $oauth */
$oauth = $this->get("guzzle_oauth");
$client->getEmitter()->attach(
$oauth->config([
'consumer_key' => 'key',
'consumer_secret' => 'secret',
'signature_method' => $oauth::SIGNATURE_METHOD_HMAC
])
);
/** @var \GuzzleHttp\Message\ResponseInterface $response */
$response = $client->get(
'http://oauthbin.com/v1/request-token',
['auth' => 'oauth']
);/** @var \GuzzleHttp\Client $client */
$client = $this->get("guzzle");
/** @var \Kopjra\GuzzleBundle\Cache\CacheSubscriber $oauth */
$cache = $this->get("guzzle_cache");
$client->getEmitter()->attach($cache);
/** @var \GuzzleHttp\Message\ResponseInterface $response */
$response = $client->get('http://httpbin.org/cache/60');At the moment it only checks the existence of a cached copy, returning it no matter what.
/** @var \GuzzleHttp\Client $client */
$client = $this->get("guzzle");
/** @var \Kopjra\GuzzleBundle\Cache\CacheSubscriber $oauth */
$cache = $this->get("guzzle_cache");
// Sets the cache type to server side
$cache->setCacheType("server");
$client->getEmitter()->attach($cache);
/** @var \GuzzleHttp\Message\ResponseInterface $response */
$response = $client->get('http://httpbin.org/cache/60');/** @var \GuzzleHttp\Client $client */
$client = $this->get("guzzle");
/** @var \Kopjra\GuzzleBundle\Retry\RetrySubscriber $oauth */
$retry = $this->get("guzzle_retry");
// Sets the configuration for the subsrcriber
$retry->config([
'filter' => $retry::createChainFilter([
$retry::createIdempotentFilter(),
// Retries only if the response header has a 304 code.
$retry::createStatusFilter([304])
]),
// Each retry will be delayed by 1000ms...
'delay' => function () { return 1000; },
// ...for max 10 times.
'max' => 10
]);
$client->getEmitter()->attach($retry);
/** @var \GuzzleHttp\Message\ResponseInterface $response */
$response = $client->get('http://httpbin.org/status/304');More doc: /Services/README.md
/** @var \GuzzleHttp\Client $client */
$client = $this->get("guzzle");
/** @var \Kopjra\GuzzleBundle\Services\Services $services */
$services = $this->get("guzzle_services");
$webServices = $services->setWebServices(["webservice1"], ["webservice2"], ["webservice3"]);
/** @var \GuzzleHttp\Command\Guzzle\GuzzleClient $client */
$client = $services->attachWebService($client, $webService->webservice2);
$response = $client->foo(['bar' => 'baz']);Content of the file app/Resources/webservice/foo.json
{
"baseUrl": "http:\/\/httpbin.org\/",
"operations": {
"foo": {
"httpMethod": "GET",
"uri": "\/get?{bar}",
"responseModel": "getResponse",
"parameters": {
"bar": {
"type": "string",
"location": "query"
}
}
}
},
"models": {
"getResponse": {
"type": "object",
"additionalProperties": {
"location": "json"
}
}
}
}Emanuele Minotto
Joy Lazari (Gionni Valeriana)
