PHP无法在AliExpress中使用file_get_html()

时间:2015-05-23 19:05:20

标签: php html url file-get-contents

我一直在尝试在AliExpress中使用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

我无法弄清楚为什么第一个网址没问题,第二个网址我不能使用。

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

1 个答案:

答案 0 :(得分:3)

在大多数情况下,从网址抓取/抓取内容并非合法。您应该使用他们提供的API。以下是从URL

获取内容的卷曲代码
$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 );