leaseweb / api-caller-bundle
具有完整Web调试工具栏集成的cURL API调用功能
Requires
- php: ^7.1
- ext-curl: *
- symfony/framework-bundle: ^4.4|^5.0
- symfony/security-bundle: ^4.4|^5.0
Requires (Dev)
- doctrine/doctrine-bundle: *
- swiftmailer/swiftmailer: *
- symfony/validator: ^5.0
- symfony/yaml: ^5.0
- twig/twig: *
- willdurand/propel-typehintable-behavior: dev-master
README
LswApiCallerBundle为您的Symfony2应用程序添加了CURL API调用功能。它易于从代码中使用,旨在提供完整的调试功能。
阅读关于LswApiCallerBundle的LeaseWebLabs博客
要求
- PHP 5.3带curl支持
- Symfony >= 2.1
安装
安装分为以下步骤
- 使用composer下载LswApiCallerBundle
- 启用Bundle
- 确保PHP中的cURL模块已启用
步骤1:使用composer下载LswApiCallerBundle
在您的composer.json中添加LswApiCallerBundle
{ "require": { "leaseweb/api-caller-bundle": "*", ... } }
现在运行以下命令让composer下载bundle
$ php composer.phar update leaseweb/api-caller-bundle
Composer会将bundle安装到您的项目目录的vendor/leaseweb
下。
步骤2:启用Bundle
在kernel中启用bundle
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new Lsw\ApiCallerBundle\LswApiCallerBundle(), ); }
步骤3:确保PHP中的cURL模块已启用
在基于Debian的发行版(如Ubuntu)中,该软件包名为"php5-curl",可以使用以下命令安装
$ sudo apt-get install php5-curl $ sudo service apache2 restart
在基于RedHat的发行版(如CentOS)中,该软件包名为"php-curl",可以使用以下命令安装
$ sudo yum install php-curl $ sudo service httpd restart
创建并运行以下内容的PHP文件以进行检查
<?php phpinfo() ?>
它应显示“cURL支持”选项设置为“启用”。
如果PHP中启用了CURL支持,此软件包也应在Windows安装上工作。
使用方法
您可以通过获取服务“api_caller”并使用可用的调用类型之一使用“call”函数来使用调用者
- HttpGetJson
- HttpPostJson
- HttpPostJsonBody(主体将被json_encode)
- HttpPutJson
- HttpDeleteJson
- HttpGetHtml
使用“HttpGetJson”调用类型的用法示例
use Symfony\Bundle\FrameworkBundle\Controller\Controller use Lsw\ApiCallerBundle\Call\HttpGetJson; class SomeController extends Controller { public function someAction() { ... $output = $this->get('api_caller')->call(new HttpGetJson($url, $parameters)); ... } }
使用“HttpPostJsonBody”调用类型的用法示例
use Symfony\Bundle\FrameworkBundle\Controller\Controller use Lsw\ApiCallerBundle\Call\HttpPostJsonBody; class SomeController extends Controller { public function someAction() { ... $arrayToPost = array( 'ip_address' => array( 'name' => 'IpName', 'hostname' => 'HostName', 'ip' => '192.168.0.1', 'throttling_template' => array( 'name' => 'Throttling Template' ) ) ); // this will be json_encode. If you don't want to json_encode, use HttpPostJson instead of HttpPostJsonBody $output = $this->get('api_caller')->call(new HttpPostJsonBody($url, $arrayToPost, true, $parameters)); // true to have an associative array as answer ... } }
配置
默认情况下,它使用以下cURL选项
parameters: api_caller.options: timeout: 10 # maximum transport + execution duration of the call in sec. ssl_verifypeer: false # to stop cURL from verifying the peer's certificate. useragent: "LeaseWeb API Caller" # contents of the "User-Agent: " header. followlocation: true # to follow any "Location: " header that the server sends. sslversion: 3 # set to 3 to avoid any bugs that relate to automatic version selection. fresh_connect: false # set to true to force full reconnect every call.
许可证
此bundle位于MIT许可证之下。
网络调试工具栏中的“wall-socket”图标是Picas图标集的一部分(官方网站:http://www.picasicons.com)。图标已授权,并且只能用于在Symfony2网络调试工具栏中标识LswApiCallerBundle。此图标的全部所有权和版权均归Rok Benedik所有。