我在PHP中使用cURL來編寫一個函數來將遠程xml文件導入到本地文件夾中。一切工作正常,但是我有一個問題:PHP:CURL庫:獲取curl_exec的狀態
$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應該是假的時,遠程文件不存在,不應該有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_exec($ fileIn);之後打印了它嗎?如果是這樣,請嘗試使用curl_errno($ ch)來查看是否有任何錯誤(另外:var_dump比調試更適合用於調試) – Itako
這很奇怪。 curl_getinfo始終是0 :( –