0

I have a script

$data = file_get_contents('http://my_example_link/data.xml');

and if http://my_example_link/data.xml is showing error

Failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found

I would like to create condition if the link is showing error above I will showing alert notification using bootstrap panel.

can I doing like that ?

Sinto
  • 3,915
  • 11
  • 36
  • 70
Gusti
  • 69
  • 3
  • 11
  • Check existence of file & show error message – Sinto Aug 27 '18 at 07:06
  • 1
    Possible duplicate of [How to check if a file exists from a url](https://stackoverflow.com/questions/7684771/how-to-check-if-a-file-exists-from-a-url) – Sinto Aug 27 '18 at 07:07
  • well your error is pretty self-explanatory; the url doesn't exist. you can use curl or get_headers to check if the url exists and if not serve an error. the logic of which is outlined in the above url @Sinto provided – Alex Aug 27 '18 at 08:44

1 Answers1

0

You can do it with curl, Please refer curl_getinfo

<?php
// Create a cURL handle
$ch = curl_init('http://www.example.com/');

// Execute
curl_exec($ch);

// Check HTTP status code
if (!curl_errno($ch)) {
  switch ($http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE)) {
    case 200:  # OK
      break;
    default:
      echo 'Unexpected HTTP code: ', $http_code, "\n";
  }
}

// Close handle
curl_close($ch);
?>