0

Using PHP, I am trying to capture an error description returned in a REST call and throw it up in a javascript alert.

Here is the XML error response:

<?xml version="1.0" encoding="utf-8"?>
<get_response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <number_results>0</number_results>
    <search_criteria xsi:type="Forte.RestApi.Common.Models.v3.SearchRestrictions">
        <page_size>50</page_size>
        <page_index>0</page_index>
        <home_organization_id>org_334277</home_organization_id>
    </search_criteria>
    <response>
        <environment>live</environment>
        <response_desc>Error[1]: The content in the query string produced errors while parsing. Check that the content is correctly formatted. Error[2]: Error converting value '2018-02-31'. Field: 'end_au_updated_date'</response_desc>
    </response>
    <links>
        <self>https://api.forte.net/v3/paymethods/?filter=start_au_updated_date+eq+%272018-02-01%27+and+end_au_updated_date+eq+%272018-02-31%27</self>
    </links>
</get_response>

Here is what I have that is not working:

$response = curl_exec($ch);

if (strpos($response, 'Error') !== false) {
echo "<script language='javascript'>alert",($response["response"]["response_desc"]),";</script>";
die;
}

The value I want to capture is the "response_desc" field.

What am I missing?

James
  • 161
  • 1
  • 10

1 Answers1

0

You could use SimpleXML to get the string (instead of $response["response"]["response_desc"]) :

$xml = simplexml_load_string($response);
echo (string)$xml->response->response_desc;

Outputs :

Error[1]: The content in the query string produced errors while parsing. Check that the content is correctly formatted. Error[2]: Error converting value '2018-02-31'. Field: 'end_au_updated_date'
Syscall
  • 19,327
  • 10
  • 37
  • 52