Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/metaer/curl-wrapper-bundle
Curl wrapper bundle for symfony
https://github.com/metaer/curl-wrapper-bundle
curl php symfony
Last synced: 3 months ago
JSON representation
Curl wrapper bundle for symfony
- Host: GitHub
- URL: https://github.com/metaer/curl-wrapper-bundle
- Owner: metaer
- Created: 2017-11-24T00:57:56.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2023-03-30T20:32:36.000Z (over 1 year ago)
- Last Synced: 2024-09-30T16:22:02.885Z (3 months ago)
- Topics: curl, php, symfony
- Language: PHP
- Size: 30.3 KB
- Stars: 2
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[![Build Status](https://travis-ci.org/metaer/curl-wrapper-bundle.png?branch=master)](https://travis-ci.org/metaer/curl-wrapper-bundle)
Curl wrapper bundle
===
Curl Wrapper bundle for symfony framework
https://packagist.org/packages/metaer/curl-wrapper-bundleInstallation
---
``` sh
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:
``` php
new \Metaer\CurlWrapperBundle\MetaerCurlWrapperBundle(),
```Basic Usage
---
``` php
$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:
---
``` 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,
];try {
$result = $curlWrapper->getQueryResult($options);
} catch (CurlWrapperException $e) {
//handle exception
}
return new Response('something');
}
}
```Basic Usage with autowire in another service:
---
``` php
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
# config/packages/metaer_curl_wrapper.yaml
metaer_curl_wrapper:
wrapper: custom_curl_wrapper
```
``` yaml
# services.yaml
services:
# your services
#...
custom_curl_wrapper:
class: 'App\MyCurlWrapper'
```
``` php
// 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:``` yaml
# services.yaml
services:
# your services
#...
metaer_curl_wrapper.curl_wrapper:
class: 'App\MyCurlWrapper'
```Timeouts settings
---
```php
$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
```