php 没有得到 Guzzle 的预期回应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22824117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Not getting expected response from Guzzle
提问by Johnathan Barrett
I'm trying to build an endpoint that forwards the data passed to it to an API using the Slim PHP Framework and I'm having trouble getting my response from a Guzzle request.
我正在尝试构建一个端点,该端点使用 Slim PHP 框架将传递给它的数据转发到 API,但我无法从 Guzzle 请求中获取响应。
$app->map( '/api_call/:method', function( $method ) use( $app ){
$client = new GuzzleHttp\Client([
'base_url' => $app->config( 'api_base_url' ),
'defaults' => [
'query' => [ 'access_token' => 'foo' ],
]
]);
$request = $client->createRequest( $app->request->getMethod(), $method, [
'query' => $app->request->params()
]);
var_dump( $client->send( $request )->getBody() );
})->via( 'GET', 'POST', 'PUT', 'PATCH', 'DELETE' )->conditions( [ 'route' => '.+?' ] );`
This then gives me...
这然后给了我...
object(GuzzleHttp\Stream\Stream)[59]
private 'stream' => resource(72, stream)
private 'size' => null
private 'seekable' => boolean true
private 'readable' => boolean true
private 'writable' => boolean true
private 'meta' =>
array (size=6)
'wrapper_type' => string 'PHP' (length=3)
'stream_type' => string 'TEMP' (length=4)
'mode' => string 'w+b' (length=3)
'unread_bytes' => int 0
'seekable' => boolean true
'uri' => string 'php://temp' (length=10)
...instead of the response of 'cool' I was expecting.
......而不是我期待的“酷”的回应。
If I just var_dump $client->sendRequest( $request )
I get a 200 OK, and the url is what I expect, http://localhost:8000/test?access_token=foo
.
如果我只是 var_dump,$client->sendRequest( $request )
我会得到 200 OK,而 url 正是我所期望的,http://localhost:8000/test?access_token=foo
.
I have another request, but only using $client->post(...)
and it works fine without giving me the stream thing back.
我有另一个请求,但只使用$client->post(...)
它并且它工作正常而没有给我流的东西。
I've tried reading the stream using the example at the bottom (http://guzzle.readthedocs.org/en/latest/http-client/response.html) but it's telling me feof
doesn't exist.
我尝试使用底部的示例(http://guzzle.readthedocs.org/en/latest/http-client/response.html)阅读流,但它告诉我feof
不存在。
Anyone have any idea what I'm missing or doing wrong here?
任何人都知道我在这里遗漏了什么或做错了什么?
采纳答案by Michael Dowling
The body that you are var_dumping is a Guzzle stream object. This object can be treated like a string or read from as needed. Documentation for Guzzle Stream here
你 var_dumping 的主体是一个 Guzzle 流对象。可以将此对象视为字符串或根据需要从中读取。Guzzle Stream 的文档在这里
回答by K-Gun
Could be;
可能;
$response = $client->send($request)->getBody()->getContents();
$response = $client->send($request)->getBody()->read(1024*100000);
This also work as a shorthand;
这也可以作为速记;
$response = ''. $client->send($request)->getBody();
$response = (string) $client->send($request)->getBody();
// see __toString()
method for last examples: http://php.net/manual/en/language.oop5.magic.php#object.tostring
// 查看__toString()
最后一个例子的方法:http: //php.net/manual/en/language.oop5.magic.php#object.tostring
回答by Renato Mefi
I was having the same issue, and the thing is, if you getBody its a stream, which means it has a pointer, when you do getContents on it its leaving the pointer in the end of the file, which means if you want to get the body multiple times you need to seek the pointer back to 0.
我遇到了同样的问题,问题是,如果你 getBody 它是一个流,这意味着它有一个指针,当你对它执行 getContents 时,它会将指针留在文件的末尾,这意味着如果你想得到身体多次需要将指针寻找回 0。
$html1 = $this->response->getBody()->getContents();
$this->response->getBody()->seek(0);
$html2 = $this->response->getBody()->getContents();
$this->response->getBody()->seek(0);
This should work :)
这应该有效:)
@mrW I hope this helps you
@mrW 我希望这对你有帮助
回答by Vladimir Vukanac
Just had a strange situation. Note that you can get body content only once!
刚刚遇到了一个奇怪的情况。请注意,您只能获取一次正文内容!
I have expected to get same content every time I call getContents()
.
我希望每次调用时都能获得相同的内容getContents()
。
$html1 = $this->response->getBody()->getContents();
$html2 = $this->response->getBody()->getContents();
$same = ($html1 == $html2);
strlen($html1); //x
strlen($html2); //0
But they are not!I have missed information that Guzzle response is stream
so with first getContents()
we read all contents and nothing is left for second call.
但他们不是!我错过了 Guzzle 响应的信息,stream
首先getContents()
我们阅读了所有内容,第二次通话没有任何内容。