How to extract data from Mailgun API response object in PHP?

I’m working with the Mailgun API in PHP and getting a response object when I send emails. The response contains information like status codes and message details, but I’m having trouble accessing the specific properties.

$emailService = new MailgunClient($this->getApiCredentials());
$testDomain = "sandbox9999.mailgun.org";
$response = $emailService->sendEmail($testDomain, [
    'from' => '[email protected]',
    'to' => '[email protected]', 
    'subject' => $emailData->getTitle(),
    'text' => $emailData->getContent()
]);
print_r($response);

The output shows:

stdClass Object (
    [http_response_body] => stdClass Object (
        [message] => "Queued. Thank you."
        [id] => "<[email protected]>"
    )
    [http_response_code] => 200
)

When I try json_decode($response) it returns NULL. How can I properly access properties like the response code or message from this object?

You’re treating an already-parsed object like a JSON string. Mailgun’s PHP client automatically converts the JSON response into a stdClass object - that’s why json_decode() fails. It expects a string, not an object. Just use regular PHP object properties instead. Like if ($response->http_response_code === 200) { echo $response->http_response_body->message; } to check if the email queued successfully. I always grab the message ID from $response->http_response_body->id too - super useful for tracking emails or debugging delivery problems later.

Your response is already a parsed stdClass object - that’s why json_decode returns NULL. Just access the properties directly with object notation. Use $response->http_response_code for the status code, $response->http_response_body->message for the message text, and $response->http_response_body->id for the message ID. I ran into this exact same thing with Mailgun integration last year. The Mailgun PHP client handles JSON parsing internally, so you can work with the object properties right away without any extra decoding.

u don’t need json_decode since the response is already an object. just access the props directly like this: $response->http_response_code for the code and $response->http_response_body->message for the message. stdClass uses → not .