Symfony2 : send a HTTP Request

后端 未结 6 759
旧巷少年郎
旧巷少年郎 2021-01-31 21:57

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 tri

6条回答
  •  灰色年华
    2021-01-31 22:34

    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.

提交回复
热议问题