我一直试图使用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可以正常使用,而第二个不能使用。
如果有人能帮助我,我会很高兴。谢谢。从URL抓取内容在大多数情况下是不合法的。您应该使用他们提供的API。下面是curl代码,用于从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 );