, (*1)
Curl wrapper bundle
Curl Wrapper bundle for symfony framework
https://packagist.org/packages/metaer/curl-wrapper-bundle, (*2)
Installation
``` sh
composer require metaer/curl-wrapper-bundle, (*3)
If you use Symfony 4+ with symfony flex - that's all</br>
If you use Symfony 2 or 3 (without symfony flex), add to AppKernel.php:
``` php
new \Metaer\CurlWrapperBundle\MetaerCurlWrapperBundle(),
Basic Usage
``` php
$options = [
CURLOPT_URL => 'http://example.ex',
CURLOPT_RETURNTRANSFER => true,
];, (*4)
$cw = $container->get('metaer_curl_wrapper.curl_wrapper');, (*5)
try {
$result = $cw->getQueryResult($options);
} catch (CurlWrapperException $e) {
//handle exception
}, (*6)
Basic usage with injection into your controller:
---
``` php
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:
``` php
MyController {
public function myAction(CurlWrapper $curlWrapper) {
$options = [
CURLOPT_URL => 'http://example.ex',
CURLOPT_RETURNTRANSFER => true,
];, (*7)
try {
$result = $curlWrapper->getQueryResult($options);
} catch (CurlWrapperException $e) {
//handle exception
}
return new Response('something');
}
}, (*8)
Basic Usage with autowire in another service:
---
``` php
<?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
``` yaml, (*9)
metaer_curl_wrapper:
wrapper: custom_curl_wrapper, (*10)
``` yaml
# services.yaml
services:
# your services
#...
custom_curl_wrapper:
class: 'App\MyCurlWrapper'
``` php
// src/MyCurlWrapper.php
namespace App;, (*11)
use Metaer\CurlWrapperBundle\CurlWrapper;, (*12)
class MyCurlWrapper extends CurlWrapper
{
public function getQueryResult(array $curlOptions)
{
//your code here
return 'something';
}, (*13)
public function myCustomMethod()
{
//something else
}
}, (*14)
So, you do not need copy-paste full class code. Only methods which you want change.</br></br>
Another way to do the same thing:
``` yaml
# 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, (*15)
CurlWrapper::getResponseBody
CurlWrapper::getRequestBody
CurlWrapper::getRequestUrl