开发者

PHP: CURL library: Getting status of curl_exec

开发者 https://www.devze.com 2023-03-17 05:39 出处:网络
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:

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

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 HTM开发者_StackOverflow中文版L 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).

Thank you.


You can use

curl_getinfo($fileIn, CURLINFO_HTTP_CODE); 

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


Try this:

$isCopied = curl_getinfo($fileIn, CURLINFO_HTTP_CODE) != 404;

0

精彩评论

暂无评论...
验证码 换一张
取 消