2014-03-29 101 views
0

我尝试使用下面的代码刮从网站的一些产品细节:403错误

$list_url = "http://www.topshop.com/en/tsuk/category/sale-offers-436/sale-799"; 
$html = file_get_contents($list_url); 
echo $html; 

不过,我得到这个错误:

Warning: file_get_contents(http://www.topshop.com/en/tsuk/category/sale-offers-436/sale-799) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden in /homepages/19/d361310357/htdocs/shopaholic/rss/topshop_f_uk.php on line 123

我认为这是网站阻止拼写的一种方式。有没有办法解决这个问题 - 也许使用cURL并设置用户代理?

如果没有,是否有另一种获取基本产品数据的方法,如项目名称和价格?

编辑

我的代码的上下文是我最终还是希望能够实现如下:

$doc = new DOMDocument(); 
$doc->loadHTML($html); 
$xpath = new DOMXPath($doc); 

回答

0

我已经设法通过添加以下代码来解决它......

ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 6.0)'); 

...按this answer

-1

你应该使用卷曲,不与的file_get_contents的简单方法()。
使用cURL并设置适当的http头来模仿正确的http请求(真实的请求)。

P.S. :设置cURL以遵循重定向。这里是链接到cURL