I have a script that checks if a port is accessible on an IP address. So whether I choose to do that with file_get_contents or with fsockopen, the concept stays the same, which is that I have to call either of those functions in order to get my result. E.g., there is no equivalent to file_exists which would "guarantee" that calling file_get_contents would not generate an error when calling a URL.
What bugs me in this kind of a scenario, is that an error is a completely acceptable part in the flow of the program, and therefore shouldn't be treated as other ("actual") errors across my application. Because what I usually have running is a custom error handler, for any occasion where something unexpected would fail, which writes the error message in a log file, and then displays a big 404-like error page. Which means that in this case, I have to do a restore_error_handler, in order to be able to do @file_get_contents without a massive error screen appearing, in order to check if the result of it is false.
So currently that's how it works, and the output is correct, but the approach seems wrong. What I am wondering is, is there a prettier way of solving this type of a scenario? One that wouldn't require me to unset something that that is application-wide, and that would resemble a try-catch approach more.
@, you should capture errors and handle them gracefully for the user while logging them for your perusal.