metaer / curl-wrapper-bundle
Curl 包装器捆绑包
1.0.6
2021-04-27 19:44 UTC
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 包装器捆绑包
为 symfony 框架提供的 Curl 包装器捆绑包
https://packagist.org.cn/packages/metaer/curl-wrapper-bundle
安装
composer require metaer/curl-wrapper-bundle
如果您使用 Symfony 4+ 且使用 symfony flex - 那就全部搞定了
如果您使用 Symfony 2 或 3(且不使用 symfony flex),请将其添加到 AppKernel.php 中
new \Metaer\CurlWrapperBundle\MetaerCurlWrapperBundle(),
基本用法
$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 }
通过控制器注入的基本用法
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'); } }
通过控制器动作参数的基本用法
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'); } }
在另一个服务中自动装配的基本用法
<?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 } } }
如何简单地更改服务行为或扩展
Symfony-4 示例
# 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 } }
所以,您不需要复制粘贴整个类代码。只需更改您想要更改的方法即可。
另一种完成相同任务的方法
# services.yaml services: # your services #... metaer_curl_wrapper.curl_wrapper: class: 'App\MyCurlWrapper'
超时设置
$connectionTimeout = 8; //seconds $serverResponseTimeout = 20; //seconds $options = [ CURLOPT_CONNECTTIMEOUT => $connectionTimeout, CURLOPT_TIMEOUT => $serverResponseTimeout, ]; $cw->getQueryResult($options);
另请参阅
您还可以使用方法
CurlWrapper::getResponseBody
CurlWrapper::getRequestBody
CurlWrapper::getRequestUrl