简单的 DOM file_get_html 不返回任何内容 [英] Simple DOM file_get_html returns nothing

查看:37
本文介绍了简单的 DOM file_get_html 不返回任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从某些网站抓取数据.对于几个站点,它似乎一切正常,但对于一个站点,它似乎无法获得任何 HTML.这是我的代码:

I'm trying to scrape data from some websites. For several sites it all seems to go fine, but for one website it doesn't seem to be able to get any HTML. This is my code:

<?php include_once('simple_html_dom.php');

$html = file_get_html('https://www.magiccardmarket.eu/?mainPage=showSearchResult&searchFor=' . $_POST['data']);

echo $html; ?>

我正在使用 ajax 来获取数据.当我在我的 js 中记录返回的值时,它完全是空的.

I'm using ajax to fetch the data. When I log the returned value in my js it's completely empty.

会不会是因为这个网站是在https上运行的?如果是这样,有没有办法解决它?(我尝试将 url 更改为 http,但结果相同)

Could it be due to the fact that this website is running on https? And if so, is there any way to work around it? (I've tried changed the url to http, but I get the same result)

更新:

如果我 var_dump $html 变量,我得到 bool(false).

If I var_dump the $html variable, I get bool(false).

我的 PHP 错误日志是这样写的:

My PHP error log says this:

[2014 年 2 月 27 日 22:20:50 欧洲/阿姆斯特丹] PHP 警告:file_get_contents(http://www.magiccardmarket.eu/?mainPage=showSearchResult&searchFor=tarmogoyf):无法打开流:HTTP 请求失败!HTTP/1.0 403 禁止在第 75 行的/Users/leondewit/PhpstormProjects/Magic/stores/simple_html_dom.php 中

[27-Feb-2014 22:20:50 Europe/Amsterdam] PHP Warning: file_get_contents(http://www.magiccardmarket.eu/?mainPage=showSearchResult&searchFor=tarmogoyf): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden in /Users/leondewit/PhpstormProjects/Magic/stores/simple_html_dom.php on line 75

推荐答案

这是你的用户代理,file_get_contents 默认不发送,所以:

It's your user agent, file_get_contents doesn't send one by default, so:

$url = 'http://www.magiccardmarket.eu/?mainPage=showSearchResult&searchFor=tarmogoyf';
$context = stream_context_create(array('http' => array('header' => 'User-Agent: Mozilla compatible')));
$response = file_get_contents($url, false, $context);
$html = str_get_html($response);
echo $html;

这篇关于简单的 DOM file_get_html 不返回任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆