如何获取源代码或远程服务器


How to fetch source code or remote server?

我想要远程网站的源代码。所以我用了:

<?php
include_once('simple_html_dom.php');
$f = file_get_contents("http://163.53.77.55");
echo htmlspecialchars( $f ); 

我通过这个得到了源代码...但现在我想要以下源代码:

$f = file_get_contents("http://163.53.77.55/offers/");

我得到了这个错误:

警告:file_get_contents(http://163.53.77.55/offers):无法打开流:HTTP 请求失败!HTTP/1.1 500 服务器错误

这意味着我可以看到 stackoverflow.com 的源代码,但看不到 stackoverflow.com/questions/!

你将不得不使用 curl。 但是首先关闭JavaScript,看看你需要的信息是否在那里。 例如,报价页面通过JavaScript获取图像。

此页面的设计者试图阻止您。

当您使用 curl 时,请使用旧的智能手机用户代理。

这奏效了:

$request = array();
$request[] = "Host: www.flipkart.com";
$request[] = "Connection: keep-alive";
$request[] = "Cache-Control: no-cache";
$request[] = "Pragma: no-cache";
$request[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$request[] = "User-Agent: MOT-V9mm/00.62 UP.Browser/6.2.3.4.c.1.123 (GUI) MMP/2.0";
$request[] = "Accept-Language: en-US,en;q=0.5";
$ch = curl_init('http://www.flipkart.com/offers/');
curl_setopt($ch, CURLOPT_ENCODING,"");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_FILETIME, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 100);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_TIMEOUT,100);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request);
$data = curl_exec($ch);
if (curl_errno($ch)){
    $data .= 'Retreive Base Page Error: ' . curl_error($ch);
}
else {
  $skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE)); 
  $head = substr($data,0,$skip);
  $data = substr($data,$skip);
 }
echo $data;