php用file_get_contents读取远程文件失败的解决办法

使用PHP的file_get_contents函数读取远程文件有时会出错,这时我们可以使用下面的函数来取代file_get_contents函数。如下:
程序代码 程序代码

$str=get_http('www.520music.com','/MusicList/520music.com_14185.htm');

function get_http($host,$url){
    $fp=fsockopen($host,80,$errno,$errstr,30);
    if(!$fp) echo "$errstr ($errno)";
    else{
        $out="GET $url HTTP/1.0\r\n";
        $out.="Host: $host\r\n";
        $out.="Connection: Close\r\n\r\n";

        fputs($fp,$out);
        $str="";
        while(!feof($fp)) $str.=fgets($fp,128);
        fclose($fp);
    }
    return $str;
}


文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: PHP 小偷 函数
相关日志:
评论: 0 | 引用: 0 | 查看次数: 3177
发表评论
你没有权限发表评论!