如何从 php 运行 wget 以便输出显示在浏览器窗口中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2528466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run wget from php so that output gets displayed in the browser window?
提问by Svolochenok
How to run wget from php so that output gets displayed in the browser window?
如何从 php 运行 wget 以便输出显示在浏览器窗口中?
回答by codaddict
You can just use file_get_contentsinstead. Its much easier.
您可以只使用file_get_contents代替。它要容易得多。
echo file_get_contents('http://www.google.com');
If you have to use wget, you can try something like:
如果您必须使用 wget,您可以尝试以下操作:
$url = 'http://www.google.com';
$outputfile = "dl.html";
$cmd = "wget -q \"$url\" -O $outputfile";
exec($cmd);
echo file_get_contents($outputfile);
回答by Chris Clarke
The execfunction can be used to run wget. I've never used wget for more then simple file downloads but you would use whatever arguments you give to wget to make it output the file contents. The second parameter/argument of exec will be an array, and this array will be filled line by line with the output of wget.
该exec函数可用于运行 wget。我从未将 wget 用于更简单的文件下载,但您可以使用您提供给 wget 的任何参数来使其输出文件内容。exec 的第二个参数/参数将是一个数组,这个数组将用 wget 的输出逐行填充。
So you would have something like:
所以你会有类似的东西:
<?php
exec('wget http://google.com/index.html -whateverargumentisusedforoutput', $array);
echo implode('<br />', $array);
?>
The manual page for exec probably explains this better: http://php.net/manual/en/function.exec.php
exec 的手册页可能更好地解释了这一点:http: //php.net/manual/en/function.exec.php
回答by Iain Sherris
Don't try that on most serversr, they should be blocked from running commands like wget! file_get_contents has just replaced crappy iframe my client insisted on having with this and a quick
不要在大多数服务器上尝试这样做,它们应该被阻止运行像 wget 这样的命令!file_get_contents 刚刚取代了我的客户坚持使用的蹩脚 iframe
<?php
$content = file_get_contents('http://www.mysite.com');
$content = preg_replace("/Comic Sans MS/i", "Arial, Verdana ", $content);
$content = preg_replace("/<img[^>]+\>/i", " ", $content);
$content = preg_replace("/<iframe[^>]+\>/i", " ", $content);
$echo $content;
?>
later to alter the font, images and remove images and iframes etc.... and my site is looking better than ever! (Yes I know my bit of the code isn't brilliant but it's a big improvement for me and removes the annoying formatting!)
稍后更改字体、图像并删除图像和 iframe 等......我的网站看起来比以往任何时候都好!(是的,我知道我的代码部分并不出色,但对我来说这是一个很大的改进,并删除了烦人的格式!)
回答by insign
It works
有用
<?php
system("wget -N -O - 'http://google.com")
?>

