通过php将网站源代码保存到文件中


Save website sourecode to file via php

嗨,我想保存的源代码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;
?>

但是我不知道怎么做!

感谢

使用

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

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

使用file_get_contents()。

在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调用中使用上下文选项。也可以看到这个类似的答案,它显示了如何。你可能需要设置一个用户代理之类的东西,让其他网站的服务器相信你是一个浏览器。