用PHP从我的另一个网站下载网页源代码


Downloading a webpage source code from another one of my websites in PHP

我更像是一个Perl程序员,还在学习PHP...

我在Perl中有这段代码,我可以用来下载我的其他页面之一的源代码,或者我想要的任何页面......

use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
$ua->agent("Mozilla/8.0"); # act like we are very capable browser
$ua->cookie_jar({ file => "$ENV{HOME}/.cookies.txt", autosave => 1 });
#   $ua->cookie_jar(HTTP::Cookies->new(file => "lwpcookies.txt"));
$req = HTTP::Request->new(GET => "http://www.yahoo.com/");
#   $req->header('Accept' => 'text/plain');
$req->referer('http://www.yahoo.com/');
# send request
$res = $ua->request($req);
if ($res->is_success) {
    my $_tableContent = $res->content; # This gets the page content and fills it into the variable $_tableContent...
 ....

我的问题是,有没有办法在 Php 中做到这一点如此简单?甚至更容易?

谢谢。-富

如果启用了 URL 包装器并且您不需要传递额外的标头、存储 cookie 等,您可以使用 file_get_contents() 函数获取页面:

$page_contents=file_get_contents("http://example.com/");

否则,您可以使用 CURL 执行此操作。关注本主题:如何使用 cURL 获取页面内容?