24hoursmedia / tesla-client-bundle
此包已被 废弃 并不再维护。没有建议的替代包。
客户端包
v1.0.0
2013-06-20 11:12 UTC
This package is not auto-updated.
Last update: 2022-02-01 12:28:05 UTC
README
Symfony 插件,提供
- http 客户端
- http 客户端缓存
- 代理
客户端请求通过 SF 的 Request 实例的子类来完成。
安装
通过 composer
(1) 在 composer.json 中添加以下行
"require": {
(...)
"24hoursmedia/tesla-client-bundle": "master-dev"
(...)
}
(2) 运行 composer update
php composer.phar update
(3) 将插件添加到 AppKernel.php
$bundles = array(
(....)
new Tesla\Bundle\ClientBundle\TeslaClientBundle()
);
历史
20131125 添加了直接向客户端和代理服务注入额外 curl 选项的功能
示例
http 客户端
从工厂创建客户端并进行请求
use Tesla\Bundle\ClientBundle\Client\HttpClientFactory;
use Tesla\Bundle\ClientBundle\Client\HttpClientInterface;
use Tesla\Bundle\ClientBundle\Client\TeslaRequest;
use Tesla\Bundle\ClientBundle\Client\TeslaResponse;
/* @var $factory HttpClientFactory */
$factory = $client->getContainer()->get('tesla_client.http_client_factory');
/* @var $client HttpClientInterface */
$http = $factory->get($baseUrl);
$request = $http->createRequest('http://www.example.com');
// (add headers and other request stuff here) //
$response = $http->execute($request);
$format = $response->getFormat(); // (json etc)
$content = $response->getContent();
$status = $response->getStatusCode();
在 services.yml 中创建 http 客户端
将 http 客户端配置为服务,可以通过容器获取
acme.http_client:
class: %tesla_client.http_client.class%
factory_service: tesla_client.http_client_factory
factory_method: get
arguments:
- http://www.example.com
在 services.yml 中创建 http 代理服务
创建一个代理服务,将来自 http://foo 和 http://bar/sub 的请求代理到 http://www.example.com
acme.http_proxy_example:
class: %tesla_client.http_proxy.class%
factory_service: tesla_client.httpproxy_factory
factory_method: get
arguments:
- http://www.example.com
calls:
- [addTranslationUrl, ["http://foo"]]
- [addTranslationUrl, ["http://bar/sub"]]
使用代理
使用作为服务定义的代理的示例
use Tesla\Bundle\ClientBundle\Proxy\HttpProxy;
/* @var $proxy HttpProxy */
$proxy = $client->getContainer()->get('tesla_client.http_proxy_example');
// foo host gets proxied to example.com
$responseContent = $proxy->execute($proxy->createRequest('http://foo'))->getContent();
$this->assertContains('example', $responseContent);
// foo bar/sub gets proxied to example.com
$responseContent = $proxy->execute($proxy->createRequest('http://bar/sub'))->getContent();