PHP If file_get_contents fails, do this instead

后端 未结 7 1794
谎友^
谎友^ 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:20

    /*
    It's a modified file_get_contents()
    get_contents(filename, use_include_path, context, offset, maxlen)
    */
    function get_contents($url, $u = false, $c = null, $o = null) {
        $headers = get_headers($url);
        $status = substr($headers[0], 9, 3);
        if ($status == '200') {
            return file_get_contents($url, $u, $c, $o);
        }
        return false;
    }
    
    echo get_contents('https://batas.kz/');
    

提交回复
热议问题