0

I'm using this curl function to load the remote file file.php.

$ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://example.com/file.php");
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    if(curl_exec($ch) === FALSE) {
         echo 'No file';
    } else {
         echo curl_exec($ch);
    }

    curl_close($ch);

But if the file does not exist, I get the standard server 404 file at example.com.

How can I get curl to echo the "No file" text?

I've looked at this question Easy way to test a URL for 404 in PHP? but I think a simpler example would be useful.

BlueDogRanch
  • 721
  • 1
  • 16
  • 43
  • 2
    Possible duplicate of [Easy way to test a URL for 404 in PHP?](https://stackoverflow.com/questions/408405/easy-way-to-test-a-url-for-404-in-php) – Devon Bessemer Jun 10 '18 at 16:37
  • I tried using the top answer from that question, but I get a Undefined variable: url error, so it woiuld be good to get a simpler example that works with my question. – BlueDogRanch Jun 10 '18 at 17:32
  • It seems like you need to spend a little more time researching/learning PHP then if you can't make that work for you. SO isn't a school and it doesn't make sense to have 100 different answers for small variations on the same problem. – Devon Bessemer Jun 10 '18 at 17:54
  • Of course SO is a school; users ask questions, users give answers. And there are hundreds of questions that are variations on a theme. I know you have a high rep, but since it is a community here, after all, other users can answer. – BlueDogRanch Jun 10 '18 at 18:22

0 Answers0