metaer / curl-wrapper-bundle
Curl wrapper bundle
Installs: 7 713
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 3
Forks: 1
Open Issues: 1
Type:symfony-bundle
Requires
- php: >=7.0
- ext-curl: *
- symfony/framework-bundle: ^2.7 || ^3.0 || ^4.0 || ^5.0 || ^6.0
Requires (Dev)
- symfony/phpunit-bridge: ^5.2
README
Curl wrapper bundle
Curl Wrapper bundle for symfony framework
https://packagist.org/packages/metaer/curl-wrapper-bundle
Installation
composer require metaer/curl-wrapper-bundle
If you use Symfony 4+ with symfony flex - that's all
If you use Symfony 2 or 3 (without symfony flex), add to AppKernel.php:
new \Metaer\CurlWrapperBundle\MetaerCurlWrapperBundle(),
Basic Usage
$options = [ CURLOPT_URL => 'http://example.ex', CURLOPT_RETURNTRANSFER => true, ]; $cw = $container->get('metaer_curl_wrapper.curl_wrapper'); try { $result = $cw->getQueryResult($options); } catch (CurlWrapperException $e) { //handle exception }
Basic usage with injection into your controller:
MyController { /** * @var CurlWrapper */ private $curlWrapper; /** * MyController constructor. * @param CurlWrapper $curlWrapper */ public function __construct(CurlWrapper $curlWrapper) { $this->curlWrapper = $curlWrapper; } public function myAction() { $options = [ CURLOPT_URL => 'http://example.ex', CURLOPT_RETURNTRANSFER => true, ]; try { $this->curlWrapper->getQueryResult($options); } catch (CurlWrapperException $e) { //handle exception } return new Response('something'); } }
Basic usage with controllers action argument:
MyController { public function myAction(CurlWrapper $curlWrapper) { $options = [ CURLOPT_URL => 'http://example.ex', CURLOPT_RETURNTRANSFER => true, ]; try { $result = $curlWrapper->getQueryResult($options); } catch (CurlWrapperException $e) { //handle exception } return new Response('something'); } }
Basic Usage with autowire in another service:
<?php namespace App\Service; use Metaer\CurlWrapperBundle\CurlWrapper; use Metaer\CurlWrapperBundle\CurlWrapperException; class MyService { /** * @var CurlWrapper */ private $curlWrapper; /** * MyService constructor. * @param CurlWrapper $curlWrapper */ public function __construct(CurlWrapper $curlWrapper) { $this->curlWrapper = $curlWrapper; } public function myMethod() { $options = [ CURLOPT_URL => 'http://example.ex', CURLOPT_RETURNTRANSFER => true, ]; try { $result = $this->curlWrapper->getQueryResult($options); } catch (CurlWrapperException $e) { //handle exception } } }
How simply change service behaviour or extend
Symfony-4 example
# config/packages/metaer_curl_wrapper.yaml metaer_curl_wrapper: wrapper: custom_curl_wrapper
# services.yaml services: # your services #... custom_curl_wrapper: class: 'App\MyCurlWrapper'
// src/MyCurlWrapper.php namespace App; use Metaer\CurlWrapperBundle\CurlWrapper; class MyCurlWrapper extends CurlWrapper { public function getQueryResult(array $curlOptions) { //your code here return 'something'; } public function myCustomMethod() { //something else } }
So, you do not need copy-paste full class code. Only methods which you want change.
Another way to do the same thing:
# services.yaml services: # your services #... metaer_curl_wrapper.curl_wrapper: class: 'App\MyCurlWrapper'
Timeouts settings
$connectionTimeout = 8; //seconds $serverResponseTimeout = 20; //seconds $options = [ CURLOPT_CONNECTTIMEOUT => $connectionTimeout, CURLOPT_TIMEOUT => $serverResponseTimeout, ]; $cw->getQueryResult($options);
See also
You can also use methods
CurlWrapper::getResponseBody
CurlWrapper::getRequestBody
CurlWrapper::getRequestUrl