總結了5種方法:php
前三種都是php基本的文件操做函數html
curl()是php擴展須要開啓,linux下須要安裝linux
exec()執行的是linux命令行下的命令wget下載遠程文件shell
其中wget命令在本地虛機測試請求http://www.baidu.com時,沒有成功,在遠程服務器上卻能夠,考慮時DNS解析的問題,因而直接請求IP成功下載了index.html的文件。服務器
這裏只提供了方法,其中的優缺點須要詳細瞭解每個方法的功能和缺陷。curl
1.fopen()函數函數
$file = fopen("http://www.jb51.net", "r") or die("打開遠程文件失敗!"); while (!feof($file)) { $line = fgets($file, 1024); //使用正則匹配標題標記 if (preg_match("/<title>(.*)<\/title>/i", $line, $out)) { $title = $out[1]; //將標題標記中的標題字符取出 break; //退出循環,結束遠程文件讀取 } } fclose($file);
2.file()函數測試
$lines = file("http://www.jb51.net/article/48866.htm"); readfile("http://www.jb51.net/article/48866.htm");
3.file_get_contents()函數url
$content = file_get_contents("http://www.jb51.net/article/48866.htm");
4.curl() 請求遠程url數據.net
$url = "http://www.baidu.com"; $ch = curl_init(); $timeout = 5; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $contents = curl_exec($ch); curl_close($ch);
5.exec() 執行命令行命令
//exec("wget 220.181.111.188"); shell_exec("wget 220.181.111.188");