dav
dav

Reputation: 9267

check if xml is invalid from the response php

I make a usual request by curl to get an xml file

$userAgent = 'Mozilla/5.0 (Windows NT 6.1; rv:21.0) Gecko/20100101 Firefox/21.0';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $requestUrl);
        curl_setopt($ch, CURLOPT_REFERER, $requestUrl);
        curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
        $output = curl_exec($ch);
        curl_close($ch);

then I use

$xml = simplexml_load_string($output);

but I faced an url that has invalid xml structure, and, hence it gives errors when I use simplexml_load_string function, and trying to open in the browser it shows this

This page contains the following errors:

error on line 3 at column 53: invalid character in attribute value

So, I want to try to check if the response contains that error so I can do necessary things in my code

I have tried smth like this

if (strpos($output, "This page contains the following errors:") === false) {
    echo "valid xml";
} else {
    echo "invalid xml structure";
}

but it does not work, and even if the url returns invalid xml , after all it can no find that text in the response.

Thanks

Upvotes: 2

Views: 1057

Answers (1)

Musa
Musa

Reputation: 97717

You can use libxml_use_internal_errors(true) to turn off errors and use libxml_get_errors() to to fetch error information as needed.

http://php.net/manual/en/function.libxml-use-internal-errors.php

Upvotes: 1

Related Questions