symfony2从BuzzBundle响应中获取确切内容

时间:2014-11-13 15:10:21

标签: php symfony httpresponse buzz

如何在没有标题或其他信息的情况下从BuzzBundle获取确切内容

目前我正在使用,

 $buzz = $this->container->get('buzz');
 $response = $buzz->get('http://api.ipify.org?format=json');
 echo $response;

和输出是,

HTTP/1.1 200 OK Server: Cowboy Connection: close Access-Control-Allow-Origin: * Content-Type: text/plain Date: Thu, 13 Nov 2014 14:51:40 GMT Content-Length: 14 Via: 1.1 vegur {"ip":"54.254.210.209"} 

这里只需要{"ip":"111.20.67.90"}

或其他任何方式,使用不同的bundle,或者我必须直接在PHP代码中使用curl?

1 个答案:

答案 0 :(得分:1)

对于后代,BuzzBundle只是一个injects the Buzz library into Symfony的小配置文件。

当您使用get()的{​​{1}}方法时,它会返回Browser的{​​{3}}实例... a return value of MessageInterface

call()

如果您熟悉/** * Returns the message document. * * @return string The message */ public function __toString(); ,这只相当于一个头文件,但很明显C++文件会返回完整的消息,包括标题。因此,如果我们查看该文件的其余部分,我们会发现let's examine that file

__toString()

因此,如果您将/** * Returns the content of the message. * * @return string The message content */ public function getContent(); 视为对象而不是像字符串那样尝试访问它(它提供了默认值MessageInterface),那么这就是您要使用的代码:

__toString()