五种方法:
前三种都是php基本的文件操作函数
curl()是php扩展需要开启,linux下需要安装
exec()执行的是linux命令行下的命令wget下载远程文件
其中wget命令在本地虚机测试请求时,没有成功,在远程服务器上却可以,考虑时DNS解析的问题,于是直接请求IP成功下载了index.html的文件。
这里只提供了方法,其中的优缺点需要详细了解每一个方法的功能和缺陷。
一、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);二、file()函数
$lines = file("http://www.jb51.net/article/48866.htm");
readfile(http://www.jb51.net/article/48866.htm);三、file_get_contents()函数
$content = file_get_contents(http://www.jb51.net/article/48866.htm);
四、curl() 请求远程url数据
$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);
五、exec() 执行命令行命令
//exec("wget 220.181.111.188");
shell_exec("wget 220.181.111.188");| 欢迎光临 一起源码网 (https://www.171739.xyz/) | Powered by Discuz! X3.3 |