guilhermednt / http-service-bundle
Symfony HttpServiceBundle
Installs: 5
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: >=5.3.0
- guzzle/guzzle: >=3.7.0
This package is auto-updated.
Last update: 2024-12-12 03:24:58 UTC
README
Currently, this bundle turns Guzzle HTTP Client into a service to allow application wide configuration (like curl proxy settings).
It uses a ClientFactory
so that you can adapt it to other HTTP Clients.
Installation
Step 1: Add package as requirement in Composer
Add the bundle to your composer.json
:
{ "require": { "guilhermednt/http-service-bundle": "dev-master" } }
Then run the update command:
$ composer update guilhermednt/http-service-bundle
Step 2: Tell Symfony2 about it.
Enable the bundle in your AppKernel.php
:
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new Donato\HttpServiceBundle\DonatoHttpServiceBundle(), ); }
Step 3: Add desired parameters
This bundle does not require you to change config.yml
, so that you can have different configuration scenarios for each environment you have.
The minimum configuration needed is this:
# app/config/parameters.yml parameters: http_client_factory_class: Donato\HttpServiceBundle\Factory\ClientFactory http_client_config: ~
Below you can see an example for HTTP Proxy with Authentication:
# app/config/parameters.yml imports: - { resource: constants.php } parameters: # ... your regular parameters ... http_client_factory_class: Donato\HttpServiceBundle\Factory\ClientFactory http_client_config: curl: %curl.proxy.type%: HTTP %curl.proxy.host%: my.proxy.example.com %curl.proxy.port%: 1234 %curl.proxy.auth%: username:password
Note that the CURL constants are introduced via constants.php as follows:
<?php // app/config/constants.php $container->setParameter('curl.proxy.type', CURLOPT_PROXYTYPE); $container->setParameter('curl.proxy.host', CURLOPT_PROXY); $container->setParameter('curl.proxy.port', CURLOPT_PROXYPORT); $container->setParameter('curl.proxy.auth', CURLOPT_PROXYUSERPWD);
That's it!
Now you can start using the service named http_client_factory
. To instantiate a Guzzle\Http\Client
you may just do the following:
<?php // SomeController.php public function someAction() { // ... $clientFactory = $this->get('http_client_factory'); $client = $clientFactory->getGuzzleClient(); // ... }