'How to get HTTP status answer from GuzzleHttp response by php Laravel?

I develop at php Laravel. I receiving GuzzleHttp response from Mailgun as Object and can't to get from it the status.

the Object is:

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:"[email protected]";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;}

I need just last data pair:

"http_response_code";i:200;

to get it into variable, like: $http_response_code = 200; or even just its value.

To get string as I cited above I use

$result_ser = serialize($result);

but yet can't to extract value of variable.

Also I tried this: $this->resultString .= \GuzzleHttp\json_decode($result_ser, true); and get error.

Please, explain me , How to get/extract value I needed?



Solution 1:[1]

To take the response status code you can use the function getStatusCode :

$response = $client->request();

$statusCode = $response->getStatusCode();

while to take the body of response you can use :

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

Solution 2:[2]

let's consider your request is something like

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

$object_res->getStatusCode() is the method to get http status code.

refer docs, there is simple example in this page.

Solution 3:[3]

I found that 'mailgun/mailgun' package uses its own HTTP client which also uses 'RestClient' and these classes are return stdObject.

In that Object there is property 'http_response_code' containing HTTP response code like 200, 400, 401 etc.

This property accessible by standard way $object->property and it's a solution of my query in this case.

For anybody who will read this question and answers I should to explain one thing that I not cleared in question.

I made request to the Mailgun API for subscribing member to mailing list. The API returns stdObject, not JSON or XML data.

But also there is one more strange thing - stdObject returned when request is successful only. If request fails you'll get just Exception thrown with message and without code. This forced me, if fail, to parse message body instead of get and resolve error code.

Solution 4:[4]

$responseObj->getStatusCode();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Prafulla Kumar Sahu
Solution 3 Vlad
Solution 4 Hashan