卷曲,没有获取页面源代码

时间:2017-01-10 07:22:23

标签: php curl

在这个脚本中curl工作正常。 $ output有页面但是当我尝试回显它的源代码时它什么都没显示。我认为它与页眉有关。任何帮助!!

header('content-type:text/plain');
$ch = curl_init();
$url = "https://www.amazon.com/gp/css/shiptrack/view.html/ref=TE_SIMP_typ?ie=UTF8&addressID=olqnkqjstjp&latestArrivalDate=1483848000&orderID=103-0753412-5410653&shipmentDate=1483706902&orderingShipmentId=4160817392100&packageId=1";
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_HEADER, false);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
$output = curl_exec($ch);
curl_close($ch);
echo $output;

1 个答案:

答案 0 :(得分:0)

我修改了你的代码.. 试试这个。

移除
标题('内容类型:text / plain');

并用我的代码替换你的代码。

$ch = curl_init();
$url = "https://www.amazon.com/gp/css/shiptrack/view.html/ref=TE_SIMP_typ?ie=UTF8&addressID=olqnkqjstjp&latestArrivalDate=1483848000&orderID=103-0753412-5410653&shipmentDate=1483706902&orderingShipmentId=4160817392100&packageId=1";
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_HEADER, false);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
$output = curl_exec($ch);
curl_close($ch);
echo $output;

修改

由于您需要ctrl+u

这是用于获取指定的任何网站的html源代码的PHP代码。 fopen函数用于打开网站URL。 stream_get_contents用于读取打开的URL。获取的代码显示在textarea中。

$domain = 'https://www.amazon.com/gp/css/shiptrack/view.html/ref=TE_SIMP_typ?ie=UTF8&addressID=olqnkqjstjp&latestArrivalDate=1483848000&orderID=103-0753412-5410653&shipmentDate=1483706902&orderingShipmentId=4160817392100&packageId=1';
$handle = fopen($domain, 'r');
$content = stream_get_contents($handle);
/**
// alternative to stream_get_contents
$contents = '';
while (!feof($handle)) {
    $content .= fread($handle, 8192);
}
**/ 
fclose($handle);
/** 
 * print html content on textarea
 */
echo "<textarea rows='20' cols='80'>$content</textarea>";

输出将是这样的: enter image description here

  

另外,如果你想以某种方式操纵检索到的页面,你可能会这样做   想尝试一些php DOM解析器。我发现PHP Simple HTML DOM Parser非常容易使用。