Skip to content
Home » SOAP: looks like we got no XML document

SOAP: looks like we got no XML document

Sometimes while doing soap call, it does not return the response result due to an invalid character “” in XML response and throws soap error as “looks like we got no XML document”.

Below code snippet is a simple workaround for this problem.

try
{
    $client = new SoapClient('url');
    $response = $client->GetPosts($request_data);

    /** if xml is not well formed due to invalid character, it will throw error.
     * so you can do some trick in catch part to get the result as you expect.
     */
    $result = $response->GetPostsResult;
    return $result;
}
catch (Exception $e)
{
    /** You can get the returned response XML from __getLastResponse() and process
     * it to make it in the format that you want it to be returned.
     */
    $response_xml = $client->__getLastResponse();

    if ( ! empty($response_xml)) {
        //Replace the invalid character with blank space.
        //Add multiple invalid characters in array as below if required.
        //$response_xml = str_replace(array('', 'other characters'), '', $response_xml);
        $response_xml = str_replace('', '', $response_xml);

        //Convert all element in xml format like "soap:Envelope" as "soapEnvelope"
        $response_xml = preg_replace("/(]*>)/", "$1$2$3", $response_xml);

        //Interprets xml string into an object
        $response_xml = simplexml_load_string($response_xml);

        //returned objects will be converted into associative arrays.
        $response_xml = json_decode(json_encode($response_xml));

        //Finally return your response as you expected from normal return without error.
        return $response_xml->soapBody->GetPostsResponse->GetPostsResult;
    }
}
Tags:

1 thought on “SOAP: looks like we got no XML document”

  1. dear Sir:

    My SOAP XML is format:

    0
    0
    0.0
    1205276
    0
    default
    0
    001
    AC
    0
    ddcd30ee53dd24177dc33faaf03b8cd1
    2015-12-16
    158729
    SM24,THKE,PA24,VBPL,KETO
    TS24_PRO-161215-104308-917
    0
    0
    0
    demo_test
    root.admin
    0
    0
    0
    1

    2015-12-25 09:04:43

    then, i call it

    it return=> looks like we got no XML document

Leave a Reply

Your email address will not be published. Required fields are marked *

0 Shares
Tweet
Pin
Share
Share
Share