【问题标题】:PHP can't use file_get_html() in AliExpressPHP 无法在速卖通中使用 file_get_html()
【发布时间】:2015-05-23 19:05:20
【问题描述】:

我一直在尝试在速卖通中使用 file_get_html(),但出现错误。

当我尝试使用以下内容时,一切正常:

        $url = "http://pt.aliexpress.com/br_home.htm";
    $retorno = file_get_html($url);

当我尝试使用它时,一切都崩溃了:

        $url = "http://pt.aliexpress.com/item/2015-Hot-Men-s-Fashion-Casual-Slim-Fit-Suit-Jacket-Solid-Color-High-Quality-Masculine-Blazer/32272100970.html?s=p";
    $retorno = file_get_html($url);

我得到了错误:Warning: file_get_contents(http://pt.aliexpress.com/item/2015-Hot-Men-s-Fashion-Casual-Slim-Fit-Suit-Jacket-Solid-Color-High-Quality-Masculine-Blazer/32272100970.html?s=p): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /Users/nando/htdocs/aliex/public/simple_html_dom.php on line 75

我不知道为什么第一个 URL 正常,而第二个我不能使用。

如果有人可以帮助我,我会很高兴。谢谢。

【问题讨论】:

  • 改用 curl() 函数
  • 你有什么例子吗?感谢您的回复。
  • 检查答案。谢谢
  • 不知道为什么我被否决了。

标签: php html url file-get-contents


【解决方案1】:

在大多数情况下,从 URL 抓取/抓取内容是不合法的。您应该使用他们提供的 API。这是从 URL 抓取内容的 curl 代码

$url = "Your URL";
$ch = curl_init();
curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_ENCODING, "" );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
echo  $content = curl_exec( $ch );

【讨论】:

    猜你喜欢
    • 2018-07-21
    • 2015-01-30
    • 2015-06-21
    • 2021-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-23
    相关资源
    最近更新 更多