【问题标题】:How to get website using cURL如何使用 cURL 获取网站
【发布时间】:2016-06-24 21:52:23
【问题描述】:

我无法在 php 中使用 cURL 获取数据,因为我可以在终端中使用curl 命令来执行此操作,如下所示 -

curl http://www.universalapplianceparts.com/search.aspx?find=w10130694

此命令在终端中提供预期结果。

我希望使用 curl 在 php 中得到相同的结果

下面是我的 php 代码 -

$url = "http://www.universalapplianceparts.com/search.aspx?find=W10130694";
$ch = curl_init();  
    curl_setopt($ch, CURLOPT_URL,"http://www.universalapplianceparts.com/");
    curl_setopt($ch, CURLOPT_POST, true);  
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    $output = curl_exec ($ch); 
    curl_close ($ch); 
    var_dump($output);

请让我知道为什么我在终端中使用 php curl 没有得到相同的结果。

谢谢

【问题讨论】:

  • 你想通过这个做什么?
  • 当我进入终端时,我想在浏览器中获取 html 数据。我认为 php 代码中缺少一些东西
  • 据我所知,您正在使用 POST,而您实际上想要 GET。我认为如果您将 POST 更改为 GET 并删除 postfields 和标题行,它应该可以工作......
  • 检查更新的答案。

标签: php curl web-scraping libcurl


【解决方案1】:

试试这个 ::

    $url = "http://www.universalapplianceparts.com/search.aspx?find=W10130694";
$ch1= curl_init();
curl_setopt ($ch1, CURLOPT_URL, $url );
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch1,CURLOPT_VERBOSE,1);
curl_setopt($ch1, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)');
curl_setopt ($ch1, CURLOPT_REFERER,'http://www.google.com');  //just a fake referer
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch1,CURLOPT_POST,0);
curl_setopt($ch1, CURLOPT_FOLLOWLOCATION, 20);

$htmlContent= curl_exec($ch1);
echo $htmlContent;

【讨论】:

  • 试试上面的代码......它会帮助你......它对我有用
  • 完美.. 这就是我所需要的。完美的作品..但你能解释一下吗?所以我可以正确理解我在做什么..我不想只是使用这个我也想学习。它也将有助于许多伙伴也
  • 看你首先需要了解什么是 curl 以及它是如何工作的。请参考php.net/manual/en/book.curl.php这将对您有所帮助..
  • 此代码提供了正确的 OP,但在为特定 div 编写正则表达式时,它再次返回完整的 html。你能检查一次吗?这是我的正则表达式 preg_match_all('/.*<div.*class=\"product\-list\-options\".*>.*<a href=\"(.*)\">.*<\/a>.*<\/div>/s',$htmlContent,$matches); 我只需要 href 值
【解决方案2】:

试试这个:

 $url = "http://www.universalapplianceparts.com/search.aspx?find=W10130694";
                $options = Array(
    CURLOPT_RETURNTRANSFER => TRUE,  
    CURLOPT_FOLLOWLOCATION => TRUE,  
    CURLOPT_AUTOREFERER => TRUE,
    CURLOPT_HTTPHEADER => array("Content-Type: application/text"),
    CURLOPT_TIMEOUT => 120,  
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_USERAGENT => "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1a2pre) Gecko/2008073000 Shredder/3.0a2pre ThunderBrowse/3.2.1.8",
    CURLOPT_URL => $url,  
    );

    $ch = curl_init(); 

    curl_setopt_array($ch, $options);  

    $data = curl_exec($ch);

    curl_close($ch);   

【讨论】:

  • 我在获取数据时仍然收到错误页面。您可以检查终端命令输出吗?它在终端中显示正确的完整 html 代码相同的输出应该在浏览器中显示,以便我可以从该输出复制内容
  • 它什么也不返回。完整的空白页
【解决方案3】:

我尝试了您的代码并收到与长度相关的错误,因此您还需要在 curl 请求中添加内容长度。试试下面的代码,希望对你有帮助:

$url = "http://www.universalapplianceparts.com/search.aspx?find=W10130694";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/text"));
curl_setopt($ch, CURLOPT_USERAGENT, array("Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1a2pre) Gecko/2008073000 Shredder/3.0a2pre ThunderBrowse/3.2.1.8"));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Length: 0'));
curl_setopt($ch, CURLOPT_POST, 1);
$result = curl_exec($ch);
var_dump($result);

【讨论】:

  • 请在下方添加 curl_setopt($ch, CURLOPT_USERAGENT, array("Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1a2pre) Gecko/2008073000 Shredder/3.0 a2pre ThunderBrowse/3.2.1.8"));
  • 它正在显示对象移动页面.. 不显示结果
  • 你添加了USER AGENT
  • 是的。我也尝试了接受的答案,但它在正则表达式中再次返回完整的 html。仍然我的正则表达式应该只从锚标记中的 div 类中获取 href 内容,这是我的正则表达式 preg_match_all('/.*<div.*class=\"product\-list\-options\".*>.*<a href=\"(.*)\">.*<\/a>.*<\/div>/s',$htmlContent,$matches);
猜你喜欢
  • 1970-01-01
  • 2016-05-28
  • 2015-09-22
  • 2021-04-02
  • 2015-09-14
  • 1970-01-01
  • 2012-03-09
  • 2014-11-29
相关资源
最近更新 更多