PHP CURL - 如何判断请求的整个文件是否未完全下载
我使用 CURL 和代理来获取一些 xml 文件,有时当我尝试加载/使用 xml (simplexml_load_string) 时,只有部分 XML 文档通过并失败。
我想类似的东西..
if(curl_errno($ch))
{
$error = curl_error($ch);
// handle error
}
会通过 CURL errno 捕获这种错误..
CURLE_PARTIAL_FILE (18)
文件传输较短或较大 比预期的。当 服务器首先报告一个预期的 传输大小,然后传送数据 与之前的不匹配 给定尺寸。
但是,这不起作用,我认为这可能是由于使用代理所致。我还可以检查什么吗?我现在唯一的想法是对 XML 文档的最后一位进行 preg_match,但这似乎不太理想,因为我正在获取多种类型的 XML 文档,而且我必须为每种类型编写检查。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在使用代理时遇到了同样的问题,但无法使用 cURL 的错误处理程序解决该问题。如果您有权访问这两个脚本(请求脚本和提供 XML 的脚本),请让请求脚本提供它期望在 XML 末尾的唯一代码:
并在输出末尾附加注释:
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:
And append a comment at the end of the output:
好吧,出现错误就已经告诉您,您获得的 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.
One quick fix is this:
One log fix is this one: