2012-10-25 72 views
18

使用Symfony2,我需要访问基于HTTPS的外部API。Symfony2 - 如何执行外部请求

如何调用外部URI并管理响应以“播放”它。例如,要呈现成功或失败消息?

我想在类似的信息(注意,performRequest完全是一种本发明的方法):

$response = $this -> performRequest("www.someapi.com?param1=A&param2=B"); 

if ($response -> getError() == 0){ 
    // Do something good 
}else{ 
    // Do something too bad 
} 

我一直在阅读有关Buzz和其他客户端。但我想Symfony2应该可以自己做。

+0

什么样的要求?只需HTTP GET? –

+0

任何GET或POST都很容易知道;) – ElPiter

回答

25

我建议使用curl:

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'www.someapi.com?param1=A&param2=B'); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json')); // Assuming you're requesting JSON 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

$response = curl_exec($ch); 

// If using JSON... 
$data = json_decode($response); 

注:您的Web服务器上的PHP必须安装php5-curl库。

假设API请求正在返回JSON数据,this page可能会有用。

这不使用任何特定于Symfony2的代码。很可能有一束可以简化这个过程,但如果有我不知道它。

+2

只是想说明这个例子包含错误地使用了“Content-Type”头文件。在请求中使用时,它指示正文的类型(在您的示例中未使用)。为了表明你希望服务器返回什么类型,你可以使用Accept头, 'Accept:application/json' header –

24

Symfony没有为此提供的内置服务,但这是使用依赖注入框架创建自己的完美机会。你可以在这里做的是写一个服务来管理外部呼叫。我们称之为服务“http”。

首先,写一个类有performRequest()方法:

namespace MyBundle\Service; 

class Http 
{  
    public function performRequest($siteUrl) 
    { 
     // Code to make the external request goes here 
     // ...probably using cUrl 
    } 
} 

注册为服务app/config/config.yml

services: 
    http: 
     class: MyBundle\Service\Http 

现在你的控制器访问一个名为 “HTTP” 服务。 Symfony的管理这个类的一个实例,在“集装箱”,并且你可以通过$this->get("http")访问:

class MyController 
{ 
    $response = $this->get("http")->performRequest("www.something.com"); 

    ... 
} 
10

https://github.com/sensio/SensioBuzzBundle似乎是你在找什么。

它实现了Kris Wallsmith buzz库来执行HTTP请求。

我就让你读GitHub的页面上的文档,使用是非常基本的:

$buzz = $this->container->get('buzz'); 

$response = $buzz->get('http://google.com'); 

echo $response->getContent(); 
3

的Symfony没有自己的休息客户,但你已经提到有几个束。这一个是我的一个者优先:

https://github.com/CircleOfNice/CiRestClientBundle

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

您通过

$response = $restclient->get($url); 

发送请求并得到响应Symfony的对象。 然后你就可以通过

$httpCode = $response-> getStatusCode(); 

获取状态码你的代码是这样:

$restClient = $this->container->get('ci.restclient'); 
if ($restClient->get('http://www.yourUrl.com')->getStatusCode !== 200) { 
    // no error 
} else { 
    // error 
} 
12
,我知道

最好的客户是:http://docs.guzzlephp.org/en/latest/

已经有捆绑,它集成到Symfony2的项目: https://github.com/8p/GuzzleBundle

$client = $this->get('guzzle.client'); 

// send an asynchronous request. 
$request = $client->createRequest('GET', 'http://httpbin.org', ['future' => true]); 
// callback 
$client->send($request)->then(function ($response) { 
    echo 'I completed! ' . $response; 
}); 

// optional parameters 
$response = $client->get('http://httpbin.org/get', [ 
    'headers' => ['X-Foo-Header' => 'value'], 
    'query' => ['foo' => 'bar'] 
]); 
$code = $response->getStatusCode(); 
$body = $response->getBody(); 

// json response 
$response = $client->get('http://httpbin.org/get'); 
$json = $response->json(); 

// extra methods 
$response = $client->delete('http://httpbin.org/delete'); 
$response = $client->head('http://httpbin.org/get'); 
$response = $client->options('http://httpbin.org/get'); 
$response = $client->patch('http://httpbin.org/patch'); 
$response = $client->post('http://httpbin.org/post'); 
$response = $client->put('http://httpbin.org/put'); 

更多信息可以在:http://docs.guzzlephp.org/en/latest/index.html

+0

请注意,Guzzle需要PHP> = 5.5.0,这是symfony要求(v5.3.3):) –

+0

From GuzzleBundle github页面: 要求PHP 5.4或更高版本但是现在Guzzle lib需要PHP> = 5.5.0。自从我回答以来,我认为这已经改变了。 –

+0

是的,但这似乎并不是最新的,我只是提出了一个拉式请求来纠正GuzzleBundle存储库。我认为这两个Guzzle和GuzzleBundle的作者终于会与答案:) +1无论如何,我不知道这个优秀的图书馆,感谢谈论它! –