问题
I have been trying to use file_get_html() in AliExpress but I am getting an error.
When I try to use the following, everything works:
$url = "http://pt.aliexpress.com/br_home.htm";
$retorno = file_get_html($url);
When I try to use this, everything collapses:
$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);
And I get the error: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
I can't figure out why the first URL goes okay and the second I can't use.
If somebody can help me, I will be glad. Thanks.
回答1:
Crawling/grabbing content from URL is not legal most of the case. You should use their provided API. Here is the curl code for grabbing content from a 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 );
来源:https://stackoverflow.com/questions/30416546/php-cant-use-file-get-html-in-aliexpress