PHP If file_get_contents fails, do this instead

后端 未结 7 1798
谎友^
谎友^ 2021-01-02 12:23

I have a function to translate the current text string using the Free Bing translator API. I just want to make sure if anything fails or something happens with the Applicat

7条回答
  •  离开以前
    2021-01-02 13:12

    Have you tried failing it on purpose to see what happens?

    If it's an exception, just catch it and handle it...

    try{
        //enter code to catch
    }catch(Exception $ex){
        //Process the exception
    }
    

    If there is an error outputted by the function, just @ to hide the error and handle the incorrect output of $translate_feed or $translate manually.

    You can try failing it on purpose by simply passing an invalid URI to file_get_contents and then forcefully feed non XML or invalid XML to simplexml_load_string to see what happens.

提交回复
热议问题