将 getBody() 分配给变量 Guzzle

Assign getBody() to a Variable Guzzle

我正在使用 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", ...}```

json_encode returns 默认情况下是一个 stdClass 对象如果你想要一个数组添加所需的标志

$someArray = json_decode($result,true);
  print_r($someArray);

此外,如果您使用 json 回复,您可以这样做

$result= $response->json()

响应是一个流,投射它:

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