【问题标题】:File get contents not working properly [closed]文件获取内容无法正常工作[关闭]
【发布时间】:2012-02-03 05:13:22
【问题描述】:

我正在使用这个 file_get_contents 来获取外部网站的 html。但它返回的输出与实时输出不同。

ExternalUrlhttp://www.target.com/c/baby-baby-bath-bath-safety/-/N-5xtji#?lnk=nav_t_spc_3_inc_1_1

我的实时代码: http://apptoplay.com/getImageUrl/file_get_contents.php

代码

$url="http://www.target.com/c/baby-baby-bath-bath-safety/-/N-5xtji#?lnk=nav_t_spc_3_inc_1_1";
$html = file_get_contents($url);
    echo $html;

编辑:区别在于 HTML。两者都显示不同的完全不同的内容。

【问题讨论】:

  • 浏览器中显示的内容与 file_get_contents 的差异是意料之中的。您的浏览器是不同于 PHP 的 UserAgent。对 HTTP GET 请求的响应取决于发送的标头。另外,请记住,浏览器会评估任何 JavaScript,而 PHP 不会。如果这还没有回答您的问题,请更新您的问题以指出其中的区别,而不是让我们访问这些网站。
  • 编辑了帖子。区别在于 HTML。
  • 好吧,我想我们想了这么多:)

标签: php html dom


【解决方案1】:

你必须告诉 cURL 接受 cookie。

试试这个:

<?php
/* STEP 1. let’s create a cookie file */
$ckfile = tempnam ("/tmp", "CURLCOOKIE");

/* STEP 2. visit the homepage to set the cookie properly */
$ch = curl_init ("http://www.target.com/");
curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);

/* STEP 3. visit cookiepage.php */
$ch = curl_init ("http://www.target.com/c/baby-baby-bath-bath-safety/-/N-5xtji#?lnk=nav_t_spc_3_inc_1_1");
curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
echo $output = curl_exec ($ch);
?>

More description

【讨论】:

    猜你喜欢
    • 2014-06-09
    • 1970-01-01
    • 2014-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-15
    相关资源
    最近更新 更多