1

I want an if statment thats checks for specific error from php script the error is this

Warning: file_get_contents(http://www.google.com): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in C:\xampp\htdocs\google\index.php on line 196

my code

if (file_get_contents('http://www.google.com') === false {
   echo 'faild';  
}

I want to skip only 404 error

ps: I'm not using my code on google website but I cant write the real url

(sorry for my bad english)

  • You can not check for 404 error with `file_get_contents(), this function will return `true` or `false`. Your best shut is to use an `.htacces` file, set the conditional `ErrorDocument 404 /404.html` end then create the `404.html page which will automatically display if a `404` error occurs. – Franco Oct 08 '16 at 11:21

2 Answers2

0

You can try to add a error handler and parse the error message to check for the 404 Not Found or check the http code before doing your file_get_contents

Community
  • 1
  • 1
Gwendal
  • 1,273
  • 7
  • 17
0

You can use php curl for fetching the remote page. It allows you to check the http status code. See example 2 on http://php.net/manual/en/function.curl-getinfo.php

Nadir Latif
  • 3,690
  • 1
  • 15
  • 24