gpt4 book ai didi

php - 将 getBody() 分配给变量 Guzzle

转载 作者:行者123 更新时间:2023-12-04 09:38:35 25 4
gpt4 key购买 nike

我正在使用 Guzzle 发出 Http 请求,
我收到了预期的回复,但是,我想根据收到的回复做出一些决定,但我没有收到任何帮助,我将不胜感激。

到目前为止我尝试过的
我尝试将响应分配给变量

  $result= $response->getBody(); // {"id": 1420053, "name": "guzzle", ...}
echo $result; //{"id": 1420053, "name": "guzzle", ...}
$someArray = json_decode($result);
echo $someArray; //<!-- Object of class stdClass could not be converted to string (500 Internal Server Error) -->

echo '<pre>' . print_r($response->getBody()->getContents(), true) . '</pre>'; //<pre></pre>

echo '<pre>' . print_r($response->getBody()) . '</pre>'; /***
GuzzleHttp\Psr7\Stream Object
(
[stream:GuzzleHttp\Psr7\Stream:private
] => Resource id #495
[size:GuzzleHttp\Psr7\Stream:private
] =>
[seekable:GuzzleHttp\Psr7\Stream:private
] => 1
[readable:GuzzleHttp\Psr7\Stream:private
] => 1
[writable:GuzzleHttp\Psr7\Stream:private
] => 1
[uri:GuzzleHttp\Psr7\Stream:private
] => php: //temp [customMetadata:GuzzleHttp\Psr7\Stream:private
] => Array
(
)

)
<pre>1</pre>
**/


return response()->json(["body"=>$result],201); //{"body": {},}

我真的很感激任何帮助
$response = $client->request('GET', 'https://api.github.com/repos/guzzle/guzzle');

echo $response->getStatusCode(); // 200
echo $response->getHeaderLine('content-type'); // 'application/json; charset=utf8'
echo $response->getBody(); // '{"id": 1420053, "name": "guzzle", ...}```

最佳答案

响应是一个流,转换它:

$contents = (string)$response->getBody();

关于php - 将 getBody() 分配给变量 Guzzle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62434728/

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