本文介绍了PHP:CURL库:获取curl_exec的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PHP中使用cURL来编写一个函数来获取远程xml文件到我的本地文件夹。一切正常,但我有一个问题:

I am using cURL in PHP to write a function to get a remote xml file into my local folder. Everything works fine however I have a question:

  $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的内容

Based on the documentation I read, $isCopied is supposed to be false when the remote file does not exist, and there shouldn't be myLocal.xml but my if(!$isCopied) doesn't seem tobe working. And this is the content of my 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>

我的问题是:如何获取一个布尔变量告诉我什么时候成功,什么时候没有。 (表示远程文件不存在时)。

My question is: How to get a boolean variable telling me when it succeeded and when it did not. (means when the remote file doesn not exist).

谢谢。

推荐答案

您可以使用

curl_getinfo($fileIn, CURLINFO_HTTP_CODE);

查看返回的HTTP代码是什么(您正在查找200)。

to see what http code was returned (you're looking for 200).

这篇关于PHP:CURL库:获取curl_exec的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 01:36