0

I am querying an API and the response I get is a Multidimensional Object object(stdClass) that also contains arrays. I need to be able to check if the response is an error condition or was successful. If the response is successful I need to return TRUE. If the response was an error, I need to return the error message contained in the response. The response formats for success and error are completely different. The response for an error looks like this:

object(stdClass)#837 (3) {
  ["errors"]=> array(1) {
                [0]=> object(stdClass)#838 (2) {
                        ["code"]=>int(324)
                        ["message"]=>string(80) "Duration too long, maximum:30000, actual:37081 (MediaId: snf:840912013693931526)"
                    }
                }
  ["httpstatus"]=>int(400)
  ["rate"]=>NULL
}

The response for success looks like this:

  object(stdClass)#837 (27) {
  ["created_at"]=> string(30) "Sun Mar 12 13:41:43 +0000 2017"
  ["id"]=> int(840920745073102850)
  ["id_str"]=> string(18) "940920795073102850"
  ["text"]=> string(32) "The Details Posted Here"
  ["truncated"]=> bool(false)
  ["entities"]=> object(stdClass)#838 (5) {
                ["hashtags"]=>
    ........ Way More is in the Response but it does not matter...

I have tried changing the response to an array then using isset to establish if it was an error, and if so then get the values of the error details like so:

$RESPONSEARRAY = (array) $RESPONSE; 
(isset($RESPONSEARRAY["errors"])) {
$ERRORMSG_CODE= $RESPONSEARRAY['errors'][0]['code'];
$ERRORMSG_MESSAGE = $RESPONSEARRAY['errors'][0]['message'];
$ITWASANERROR = $ERRORMSG_CODE.": ".$ERRORMSG_MESSAGE;
return $ITWASANERROR;
} else {
return true;
}

But doing the above give me the following error:

Fatal error:  Cannot use object of type stdClass as array

Can anyone suggest a way of doing what I am trying to do with the least overhead on the server. Maybe without needing to convert the stdClass object to an array, or if that has to be done, then that's fine, but I just need it to work. Any help someone can offer would be super appreciated.

2
  • 2
    if (isset($object->errors) ) { echo $object->errors[0]->message; } Commented Mar 12, 2017 at 13:56
  • WoW... As easy as that it works.. Thanks so much @RiggsFolly Commented Mar 12, 2017 at 14:09

2 Answers 2

1

Below is the correct way to access the object inside the array.

$RESPONSEARRAY = (array) $RESPONSE; 
if(isset($RESPONSEARRAY["errors"])) {
   $ERRORMSG_CODE= $RESPONSEARRAY['errors'][0]->code;
   $ERRORMSG_MESSAGE = $RESPONSEARRAY['errors'][0]->message;
   $ITWASANERROR = $ERRORMSG_CODE.": ".$ERRORMSG_MESSAGE;
   return $ITWASANERROR;
} else {
   return true;
}
Sign up to request clarification or add additional context in comments.

Comments

1

$RESPONSEARRAY = (array) $RESPONSE; You can get result:

  ["errors"]=>
  array(1) {
    [0]=>
    object(stdClass)#1 (2) {
      ["code"]=>
      int(324)
      ["message"]=>
      string(80) "Duration too long, maximum:30000, actual:37081 (MediaId: snf:8
40912013693931526)"
    }
  }
  ["httpstatus"]=>
  int(400)

So $ERRORMSG_CODE= $RESPONSEARRAY['errors'][0]['code']; should be $ERRORMSG_CODE= $RESPONSEARRAY['errors'][0]->code. And so on

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.