1

嗨,我想将http://stats.pingdom.com/w984f0uw0rey的源代码保存到我网站的某个目录中

<?php
if(!copy("http://stats.pingdom.com/w984f0uw0rey", "stats.html"))
{
echo("failed to copy file");
}
;
?>

但这对我也不起作用:

<?php
$homepage = file_get_contents('http://stats.pingdom.com/w984f0uw0rey');
echo $homepage;
?>

但我不知道该怎么做!

谢谢

4

3 回答 3

4

采用

<?

file_put_contents('w984f0uw0rey.html', file_get_contents('http://stats.pingdom.com/w984f0uw0rey'));

?>

确保脚本对当前目录具有写入权限

于 2012-03-17T16:59:13.197 回答
1

使用file_get_contents()

于 2012-03-17T16:55:55.953 回答
1

您可以在 PHP 中执行的最佳变体是使用stream_copy_to_stream

$url = 'http://www.example.com/file.zip';
$file = "/downloads/stats.html";
$src = fopen($url, 'r');
$dest = fopen($file, 'w');
echo stream_copy_to_stream($src, $dest) . " bytes copied.\n";

如果您需要添加 HTTP 选项(如标头),请在fopen调用中使用上下文选项。另请参阅这个类似的答案,它显示了如何。您可能需要设置用户代理和其他东西,以便其他网站的服务器认为您是浏览器。

于 2012-03-17T17:07:05.050 回答