开发者

PHP CURL - how to tell if the entire file requested was not fully downloaded

开发者 https://www.devze.com 2023-01-28 05:08 出处:网络
I\'m using CURL and a proxy to grab some xml files, occasionally only part of the XML document comes through and fails when I try to load/use the xml (simplexml_load_string).

I'm using CURL and a proxy to grab some xml files, occasionally only part of the XML document comes through and fails when I try to load/use the xml (simplexml_load_string).

I thought something like..

    if(curl_errno($ch))
    {
        $error = curl_error($ch);
        // handle error
    }

would catch this sorta error via CURL errno..

CURLE_PARTIAL_FILE (18)

A file transfer was shorter or larger than expected. This happens when the server first reports an expected transfer size, and then delivers data that doesn't match the previously given size.

However, this does not work, I think it might be due to using a proxy. Anything else I can check? My only thought now is to do 开发者_如何转开发a preg_match for the last bit of the XML document, but that seems less than ideal as I'm getting multiple types of XML documents and I'd have to write checks for each type.


I have experienced the same problem with a proxy and I fell short of solving the issue using cURL's error handlers. If you have access to both scripts (the one requesting and the one delivering the XML) have the requesting one deliver a unique code which it expects at the end of the XML:

// Request
http://localhost/getxml.php?id=4&uniq=1337

And append a comment at the end of the output:

<?xml encoding="..." ..?>
...
<!--1337-->


Well, having an error already tells you that the XML file you got is invalid. What you need to do is catch that error and handle it.

One quick fix is this:

$xml = @simplexml_load_string($xmlString);
if($xml === false){ /* The XML was not valid. */ }

One log fix is this one:

libxml_use_internal_errors(true);
libxml_clear_errors();
$xml = simplexml_load_string($xmlString);
if( ($err = libxml_get_last_error()) !== false ){ /* We got ourselves an XML error. */ }
0

精彩评论

暂无评论...
验证码 换一张
取 消