我正在尝试从我的一个控制器发出一个HTTP请求来联系另一个URL,目标是联系另一个URL,并在我的页面中打印HTML答案。 我试过了:
$r = new Request();
$r->create('http://www.google.com', 'GET');
return $this->render(...mytemplate..., array('name' => $r->getContent());
我的模板只是打印变量“name”
现在,当我这样做时,没有任何回报。在我看来,请求永远不会被发送,这就是为什么没有返回。
我的问题是:如何发送请求并获取响应内容?
提前致谢。
答案 0 :(得分:18)
编辑:我为Buzz浏览器制作了GremoBuzzBundle。它与SensioBuzzBundle类似,但它有一些不错的配置选项。
我建议使用Buzz browser和依赖注入。 Buzz是cURL或file_get_contents的包装器。修改添加以下行的deps
文件:
[Buzz]
git=https://github.com/kriswallsmith/Buzz.git
target=/buzz
然后安装供应商以实际下载库:
php bin/vendors install
然后添加两项服务(src/YourCompany/YourBundle/Resources/config/services.yml
):
# cURL client for Buzz
buzz.client.curl:
class: Buzz\Client\Curl
public: false
calls:
- [setVerifyPeer, [false]]
# Buzz browser
buzz.browser:
class: Buzz\Browser
arguments: ['@buzz.client.curl']
第一个服务是客户端(我更喜欢cURL而不是file_get_contents),后者是浏览器本身。最后一步是在自动加载器(app/autoload.php
)中添加一行代码:
$loader->registerNamespaces(array(
'Buzz' => __DIR__.'/../vendor/buzz/lib',
));
然后,您可以在控制器代码中获取服务并使用浏览器:
$browser = $this->get('buzz.browser');
$response = $browser->get('http://www.google.com');
答案 1 :(得分:17)
两个问题。
首先,这不是Symfony\Component\HttpFoundation\Request::create()
的正确用法,$r = Request::create( 'http://www.google.com', 'GET' );
是一种静态初始化器/工厂。您的代码应如下所示
{{1}}
现在你有一个合适的Request对象。然而,这是无关紧要的,这是你的第二个问题:这不是Symfony的请求对象的设计工作方式。其目的不是执行 HTTP请求,而是将表示它们作为框架中的对象。
长话短说,你不能这样做。也许你可以use cURL to scrape the page想要吗?
答案 2 :(得分:9)
我建议您使用GuzzleHttp客户端 - 我知道的最佳客户端:http://docs.guzzlephp.org/en/latest/
已经有很好的捆绑包将它集成到Symfony2项目中: https://github.com/8p/GuzzleBundle
然后从你的控制器你可以打电话:
$client = $this->get('guzzle.client');
// GET request with parameters
$response = $client->get('http://httpbin.org/get', [
'headers' => ['X-Foo-Header' => 'value'],
'query' => ['foo' => 'bar']
]);
$code = $response->getStatusCode();
$body = $response->getBody();
可在以下网址找到更多信息:http://docs.guzzlephp.org/en/latest/index.html
答案 3 :(得分:8)
为什么不使用卷曲?来自PHP manual
$ch = curl_init("http://www.example.com/");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
result = curl_exec($ch);
curl_close($ch);
答案 4 :(得分:3)
显然,您可以使用Symfony的内置HTTP客户端。看到: http://api.symfony.com/2.0/Symfony/Component/HttpKernel.html
以下是一个非常粗略的代码库,使用Silex(构建在Symfony之上)。它只是实例化一个新的HTTP客户端。
<?php
require_once __DIR__ . '/silex/vendor/autoload.php';
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpKernel\Controller\ControllerResolver;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\Client;
//use Symfony\Component\HttpFoundation\Response;
$dispatcher = new EventDispatcher();
$resolver = new ControllerResolver();
$kernel = new HttpKernel( $dispatcher, $resolver );
$client = new Client( $kernel );
var_dump( $client );
&GT;
您还有一个Symfony2的HTTP客户端的详细示例,作为单元测试文档的一部分。请参阅:http://symfony.com/doc/current/book/testing.html
但是(编辑)这些客户端是您应用的本地客户端。使用Symfony2的BrowserKit组件可以更好地实现此处说明的概念。 Symfony内的无头浏览器。
更好的是,使用Goutte对外部网站的请求。有关详细信息,请参阅https://github.com/FriendsOfPHP/Goutte。
答案 5 :(得分:3)
https://github.com/CircleOfNice/CiRestClientBundle
用于轻松使用curl库的漂亮API,它返回symfony响应而不是字符串结果
$restClient = $this->container->get('ci.restclient');
$restClient->get('http://www.someUrl.com');
$restClient->post('http://www.someUrl.com', 'somePayload');
$restClient->put('http://www.someUrl.com', 'somePayload');
$restClient->delete('http://www.someUrl.com');
$restClient->patch('http://www.someUrl.com', 'somePayload');
$restClient->head('http://www.someUrl.com');
$restClient->options('http://www.someUrl.com', 'somePayload');
$restClient->trace('http://www.someUrl.com');
$restClient->connect('http://www.someUrl.com');