Good error handling with file_get_contents

后端 未结 5 1650
梦如初夏
梦如初夏 2020-11-30 10:26

I am making use of simplehtmldom which has this funciton:

// get html dom form file
function file_get_html() {
    $dom = new simple_html_dom;
    $args = fu         


        
5条回答
  •  一整个雨季
    2020-11-30 10:44

    To see why a file_get_contents call might have failed, you could just use php's error_get_last function:

    if ($contents = file_get_contents($url)) {
        // it worked
    }
    else {
       die("Failed to fetch ".$url.", error: ".error_get_last()['message']);
    }
    

提交回复
热议问题