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

╄→尐↘猪︶ㄣ 提交于 2019-12-02 18:31:25

问题


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.


回答1:


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-->



回答2:


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. */ }


来源:https://stackoverflow.com/questions/4357920/php-curl-how-to-tell-if-the-entire-file-requested-was-not-fully-downloaded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!