2

I'm trying to find a best solution to save from performance, memory usage etc. for checking if a file exist on different domain or not. In my case, the file is an XML and the size can be between 10KB up to 10MB.

Which of these would be the best to use? If you have a better approach, I'll be happy to use it instead.

Thanks

CURL

$ch = curl_init("http://www.example.com/hello.xml");

curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// $retcode > 400 -> not found, $retcode = 200, found.
curl_close($ch);

FOPEN

$url = "http://www.example.com/hello.xml";

if (@fopen($url, "r")) {
   echo "File Exists";
} else {
   echo "Can't Connect to File";
}
BentCoder
  • 12,257
  • 22
  • 93
  • 165
  • 3
    curl is faster. See [this previous question on SO](http://stackoverflow.com/questions/636678/what-are-the-important-differences-between-using-fopenurl-and-curl-in-php) – Doa Jul 26 '12 at 08:38
  • Incase of XML file Why not `simplexml_load_file`? – Tamil Jul 26 '12 at 08:40

1 Answers1

1
$opts = array('http' =>
  array(
    'method'  => 'HEAD'
  )
);

$context  = stream_context_create($opts);

$result = fopen('http://example.com/submit.php', 'rb', false, $context);

Example taken (but shortened) from the manual

Then use stream_get_meta_data() to fetch the response headers, something like

$meta = stream_get_meta_data($result);
var_dump($meta['wrapper_data']);
KingCrunch
  • 128,817
  • 21
  • 151
  • 173
  • As **Dow** says above comment, cURL is faster. There is a test run in [here](http://stackoverflow.com/questions/636678/what-are-the-important-differences-between-using-fopenurl-and-curl-in-php) too. – BentCoder Jul 26 '12 at 10:25
  • Even if I could (slightly) reproduce the behaviour I wouldn't name some uncommented numbers "a test". In my opinion the streams are much easier to handle. "Performance" is not everything – KingCrunch Jul 26 '12 at 11:02