Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dmag's avatar
Level 6

handle response from body string of external api

The external api that I'm trying to integrate works so that it always returns http 200 response and errors or success messages are contained in the xml body instead.

Here're the examples of successful and unsuccessful response bodies:

$response = Http::post('http://example.com/myendpoint', $data);

/** Successful request response */
$response->ok(); // Output: true
$response->body();
/*
"<ProcessJobResponse>
    <ProcessJobResult>
        <Response>
            <Status code="200" text="OK" />
        </Response>
    </ProcessJobResult>
</ProcessJobResponse>"
*/

/** Unsuccessful request response */
$response->ok(); // Output: true
$response->body();
/*
<ProcessJobResponse>
    <ProcessJobResult>
        <Response>
            <Status code="500" text="Import failed on Job: 55 - Object reference not set to an instance of an object." />
        </Response>
    </ProcessJobResult>
</ProcessJobResponse>
*/

Can't figure out how to structure the response returns in my api implementation correctly in this case. I can convert xml to array, no problem there.

0 likes
7 replies
dmag's avatar
Level 6

@MohamedTammam $response->status() will always be ok as the api returns errors in body string with http headers being okey anyway.

Sinnbeck's avatar

Is this an api that could be fixed (return proper status codes) or is it external?

dmag's avatar
Level 6

@Sinnbeck it is external. Basically, I can read the code from the xml body after converting it to array, and then return my own response with proper codes and messages, just wasn't sure how to organize/structure such error handling in the client implementation.

Please or to participate in this conversation.