我正在 PHP 中使用 cURL 编写一个函数,将远程 xml 文件获取到本地文件夹中。一切正常,但我有一个问题:
$fileIn = curl_init("http://some-remote-host.com/file.xml);
$fileOut = fopen('myLocal.xml", "w");
curl_setopt($fileIn, CURLOPT_FILE, $fileOut);
curl_setopt($fileIn, CURLOPT_HEADER, 0);
$isCopied = curl_exec($fileIn);
curl_close($fileIn);
fclose($fileOut);
if(!$isCopied)
return false;
else
//do something else
根据我阅读的文档,当远程文件不存在时, $isCopied 应该为 false,并且不应该有 myLocal.xml 但我的if(!$isCopied)
似乎不起作用。这是我的 myLocal.xml 的内容
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL something.xml was not found on this server.</p>
<hr>
<address>Apache Server at somehost.com Port 443</address>
</body></html>
我的问题是:如何获得一个布尔变量来告诉我它何时退出以及何时不退出。 (表示远程文件不存在时)。
谢谢。
您可以使用
curl_getinfo($fileIn, CURLINFO_HTTP_CODE);
查看返回的 http 代码(您正在寻找 200)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)