gpt4 book ai didi

php - 没有得到 Guzzle 的预期响应

转载 作者:IT王子 更新时间:2023-10-29 00:10:13 25 4
gpt4 key购买 nike

我正在尝试构建一个端点,使用 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 不存在。

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

最佳答案

可能是;

$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

关于php - 没有得到 Guzzle 的预期响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22824117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com