总结了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");