php访问url的四种方式-php教程

资源魔 26 0

10.jpg

1.fopen形式

//拜访指定URL函数
function access_url($url) {    
    if ($url=='') return false;    
    $fp = fopen($url, 'r') or exit('Open url faild!');    
    if($fp){  
    while(!feof($fp)) {    
        $file.=fgets($fp)."";  
    }  
    fclose($fp);    
    }  
    return $file;  
}

保举学习:PHP视频教程

2.file_get_contents形式(关上近程文件的时分会造成CPU飙升。file_get_contents其实也能够post)

$content = file_get_contents("http://www.谷歌.com");

3.curl形式

function curl_file_get_contents($durl){  
    $ch = curl_init();  
    curl_setopt($ch, CURLOPT_URL, $durl);  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; // 猎取数据前往    
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; // 正在启用 CURLOPT_RETURNTRANSFER 时分将猎取数据前往    
    $r = curl_exec($ch);  
    curl_close($ch);  
    return $r;  
}

4.fsockopen形式(只能猎取网站主页信息,其余页面不成以)

$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);     
if (!$fp) {     
    echo "$errstr ($errno)<br />\n";     
} else {     
    $out="GET / HTTP/1.1\r\n";     
    $out.="Host: www.example.com\r\n";     
    $out.="Connection: Close\r\n\r\n";     
    fwrite($fp, $out);     
    while (!feof($fp)) {     
        echo fgets($fp, 128);     
    }  
    fclose($fp);     
}

以上就是php拜访url的四种形式的具体内容,更多请存眷资源魔其它相干文章!

标签: php开发教程 php开发资料 php开发自学 fsockopen curl fopen file_get_contents

抱歉,评论功能暂时关闭!