如何通过 php Laravel 从 GuzzleHttp 响应获取 HTTP 状态答案?

Vla*_*lad 5 php laravel guzzle

我使用 php Laravel 进行开发。我从 Mailgun 作为对象接收 GuzzleHttp 响应,但无法从中获取状态。

对象是:

O:8:"stdClass":2:{s:18:"http_response_body";O:8:"stdClass":2:{s:6:"member";O:8:"stdClass":4:{s:7:"address";s:24:"test_of_json-4@zapara.fr";s:4:"name";s:10:"not filled";s:10:"subscribed";b:1;s:4:"vars";O:8:"stdClass":0:{}}s:7:"message";s:36:"Mailing list member has been created";}s:18:"http_response_code";i:200;}

我只需要最后一个数据对:

"http_response_code";i:200;

将其放入变量中,例如: $http_response_code = 200; 甚至只是它的值。

要获取我上面引用的字符串,我使用

$result_ser = serialize($result);

但还无法提取变量的值。

我也尝试过这个: $this->resultString .= \GuzzleHttp\json_decode($result_ser, true); 并收到错误。

请解释一下,如何获取/提取我需要的价值?

Pra*_*ahu 1

让我们考虑一下你的请求是这样的

 $response = $client->get("https://example.com");
 if ( $object_res->getStatusCode() == 200 ) { // here you are checking your http status code
 }
Run Code Online (Sandbox Code Playgroud)

$object_res->getStatusCode()是获取http状态码的方法。

请参阅文档,此页面中有一个简单的示例。