パースでは、戻り値をがつがつ食います

がつがつ食うには、PSR-7を実現しました。それがメッセージ本文をデフォルトになります。この手段は、一時的に使用するストリームPHPのストリームに格納されます。すべてのデータを取得するには、変換演算子を使用することができます。

例:
$client = new Client($this->getOptions());
$response = $client->request($method, $url, $options);

次のように我々は2つの値を持つことができます。

$contents = (string) $response->getBody();
// or
$contents = $response->getBody()->getContents();

2つの方法の違いは、使用されるgetContents方法は、使用しない限り、2番目の呼び出しは、何も返さないように、コンテンツの残りの部分を返すことであるrewind方法をか、ストリームの位置を見つけるseek方法は、ポインタの巻き戻し開始位置をストリームします。

$stream = $response->getBody();
$contents = $stream->getContents(); // returns all the contents
$contents = $stream->getContents(); // empty string
$stream->rewind(); // Seek to the beginning
$contents = $stream->getContents(); // returns all the contents

逆に、PHPの文字列の変換操作で、それは最後まで最初からすべてのデータストリームに読み込みます。

$contents = (string) $response->getBody(); // returns all the contents

ドキュメント:http://docs.guzzlephp.org/en/latest/psr7.html#responses
参考:https://stackoverflow.com/questions/30549226/guzzlehttp-how-get-the-body-of-a-response -from-がつがつ食う-6

公開された41元の記事 ウォン称賛21 ビュー70000 +

おすすめ

転載: blog.csdn.net/u010324331/article/details/96485652