PHP can't use file_get_html() in AliExpress

荒凉一梦 提交于 2019-12-12 07:29:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!