6

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?

1
  • Can you post your request code? Commented May 2, 2019 at 0:41

4 Answers 4

5

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();
Sign up to request clarification or add additional context in comments.

2 Comments

This is a good answer, it can be improved by adding a bit more detail about these functions, perhaps links to the documentation of where these functions should be used. It'll help others when they find this question know why this code works rather than it just works! Thanks and welcome to StackOverflow!
thank you , sorry i forget to include the link of of guzzlehttp docs docs.guzzlephp.org/en/stable where you can read throug for a more detailed information
1

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.

7 Comments

Thanks for ry to help!
@Vlad is it what you want or I have not got the question correctly?
Thanks for try to help! My code is: $mgClient = new Mailgun(config('mail.encryption')); $listAddress = config('mail.listName') . config('mail.listAddressMark') . config('mail.listDomain'); try { $result = ''; $result = $mgClient->post("lists/$listAddress/members", [ 'address' => $request->email, ................ ]); } catch (Exception $e) { As you see, it's POST request. Are you mean that I can get status code from my $result variable? Like $result->getStatusCode() ? I'll try right now
I get error Symfony\Component\Debug\Exception\FatalThrowableError: Call to undefined method stdClass::getStatusCode() Should I to 'use' some class or package for correct work of it?
I suggest that response I get is not an Guzzle but Mailgun object. Maybe I should to ask the Mailgun support about their response.
|
0

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.

Comments

0
$responseObj->getStatusCode();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.