【问题标题】:Not getting expected response from Guzzle没有得到 Guzzle 的预期响应
【发布时间】:2014-05-14 11:36:45
【问题描述】:

我正在尝试构建一个端点,将传递给它的数据转发到使用 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' => '.+?' ] );`

然后这给了我...

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)

...而不是我期待的“酷”的回应。

如果我只是 var_dump $client->sendRequest( $request ) 我会得到 200 OK,并且 url 是我所期望的,http://localhost:8000/test?access_token=foo

我有另一个请求,但只使用了$client->post(...),它可以正常工作而没有给我返回流的东西。

我尝试使用底部的示例 (http://guzzle.readthedocs.org/en/latest/http-client/response.html) 读取流,但它告诉我 feof 不存在。

有人知道我在这里遗漏了什么或做错了什么吗?

【问题讨论】:

标签: php curl slim guzzle


【解决方案1】:

您正在 var_dumping 的主体是 Guzzle 流对象。可以将此对象视为字符串或根据需要从中读取。 Documentation for Guzzle Stream here

【讨论】:

  • 是的,但不应该,这不是我想要做的。
  • 对不起,我很笨,而且阅读正常。现在一切都好。谢谢。
【解决方案2】:

可能是;

$response = $client->send($request)->getBody()->getContents();
$response = $client->send($request)->getBody()->read(1024*100000);

这也可以用作速记;

$response = ''. $client->send($request)->getBody();
$response = (string) $client->send($request)->getBody();

// 最后一个例子见__toString()方法:http://php.net/manual/en/language.oop5.magic.php#object.tostring

【讨论】:

    【解决方案3】:

    刚刚遇到了一个奇怪的情况。请注意,您只能获取一次正文内容!

    我希望每次拨打 getContents() 时都能获得相同的内容。

    $html1 = $this->response->getBody()->getContents();
    $html2 = $this->response->getBody()->getContents();
    
    $same = ($html1 == $html2);
    
    strlen($html1); //x
    strlen($html2); //0
    

    但它们不是!我错过了 Guzzle 响应是 stream 的信息,所以第一个 getContents() 我们阅读了所有内容,没有任何内容可供第二次调用 .

    【讨论】:

      【解决方案4】:

      我遇到了同样的问题,问题是,如果你 getBody 它是一个流,这意味着它有一个指针,当你在它上面执行 getContents 时,它会将指针留在文件末尾,这意味着如果你想要多次获取body,需要将指针寻回0。

      $html1 = $this->response->getBody()->getContents();
      $this->response->getBody()->seek(0);
      $html2 = $this->response->getBody()->getContents();
      $this->response->getBody()->seek(0);
      

      这应该工作:)

      @mrW 希望对你有帮助

      【讨论】:

      • 谢谢!是的,在某些情况下会有所帮助。
      猜你喜欢
      • 2018-11-23
      • 1970-01-01
      • 2020-09-28
      • 1970-01-01
      • 1970-01-01
      • 2018-08-04
      • 2014-10-31
      • 1970-01-01
      相关资源
      最近更新 更多