PHP CURL - 如何判断所请求的整个文件是否未完全下载 [英] PHP CURL - how to tell if the entire file requested was not fully downloaded

查看:851
本文介绍了PHP CURL - 如何判断所请求的整个文件是否未完全下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用CURL和代理来抓取一些xml文件,偶尔只有一部分XML文档来通过,当我尝试加载/使用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
    }

会通过CURL errno捕获此sorta错误..

would catch this sorta error via CURL errno..


CURLE_PARTIAL_FILE(18)

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.

但是,这不工作,我认为这可能是由于使用代理。还有什么我可以检查?我现在唯一的想法是为XML文档的最后一位做一个preg_match,但这似乎不太理想,因为我得到多种类型的XML文档,我必须为每种类型写检查。

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.

推荐答案

很好,有一个错误已经告诉你,你得到的XML文件是无效的。

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.

一个快速解决方法是:

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

一个日志修复是这样:

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

这篇关于PHP CURL - 如何判断所请求的整个文件是否未完全下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆