I am trying to make a HTTP Request from one of my controller to contact another URL, the goal being to contact another URL, and to simply print the HTML answer in my page. I tried :
$r = new Request();
$r->create('http://www.google.com', 'GET');
return $this->render(...mytemplate..., array('name' => $r->getContent());
My template is simply printing the variable "name".
Now when I do that, nothing is returned. It seems to me that the request is never sent, which is why nothing is returned.
My question is : how do I send the request and get the content of the response?
Thanks in advance.
Two problems.
First of all, that's not the proper usage of
Symfony\Component\HttpFoundation\Request::create()
, which is a static initializer/factory of sorts. Your code should look like thisNow you have a proper Request object. However, this is irrelevant which is your second problem: that's not how Symfony's request object is designed to work. Its purpose is not for executing HTTP requests, its for representing them as objects in the framework.
Long story short, you can't do it that way. Perhaps you can use cURL to scrape the page you want?
I would recommend you using GuzzleHttp Client - best client that I know: http://docs.guzzlephp.org/en/latest/
There is already nice bundle that integrates it into Symfony2 project: https://github.com/8p/GuzzleBundle
Then from your controller you can call:
More info can be found on: http://docs.guzzlephp.org/en/latest/index.html
https://github.com/CircleOfNice/CiRestClientBundle
Nice API for easy usage of the curl library and it returns a symfony response instead of a string result
Apparently, you can use Symfony's built-in HTTP client. See: http://api.symfony.com/2.0/Symfony/Component/HttpKernel.html
The following is a very crude codebase, using Silex (built on top of Symfony). It simply instantiates a new HTTP client.
?>
You also have a detailed example of a HTTP client for Symfony2 as part of the unit testing documentation. See: http://symfony.com/doc/current/book/testing.html
BUT (edit) these clients are local to your app. The concepts illustrated here are better implemented with the BrowserKit component of Symfony2. A headless browser within Symfony.
Better even, use Goutte for requests to external websites. See https://github.com/FriendsOfPHP/Goutte for details.
EDIT: I made a GremoBuzzBundle for Buzz browser. It's similar to SensioBuzzBundle but it has some nice configuration options.
I would suggest to use Buzz browser and dependency injection. Buzz is a wrapper around cURL or file_get_contents. Edit your
deps
file adding these lines:Then install vendors to actually download the library:
Then add two services (
src/YourCompany/YourBundle/Resources/config/services.yml
):The first service is the client (i prefer cURL over file_get_contents), the latter is the browser itself. The last step is to add one line of code in the autoloader (
app/autoload.php
):Then you can get the service and user the browser in your controller code:
Why not use curl? From PHP manual