PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I use file() in a script to read a remote webpage, but sometimes this remote site has problems and can cause a "502 bad gateway" error and so the rest of the script wont complete properly because the info wasnt loaded.
What would be the best way to deal with this error? I mean, retry loading the page if it fails? PHP does the whole on screen "
cant do this..." error stuff when the 502 error happens, so its not like the error slips through.
I know theres the try...catch method, but would this catch the 502 error?
Could i maybe do something like:
[syntax=php]$worked = 0;
while($worked == 0){
try{
file("...");
if it worked set $worked = 1
}catch(exception...){
}
}
[/syntax]
Any input is appreciated :)
Per the manual file() returns false if it fails. If you need to catch the specific error code from the remote server I would recommend using curl. You could wrap the code in a try/catch block as well.