Symfony2 : send a HTTP Request

后端 未结 6 762
旧巷少年郎
旧巷少年郎 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条回答
  •  猫巷女王i
    2021-01-31 22:36

    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:

    $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();
    

    More info can be found on: http://docs.guzzlephp.org/en/latest/index.html

提交回复
热议问题