【问题标题】:Simple html dom not working for a table[class]简单的 html dom 不适用于表格[类]
【发布时间】:2016-06-11 15:10:57
【问题描述】:

我想使用网站的内容并将它们包含在我的 "Simple Html DOM" 中,不幸的是,通常适用于其他网站的代码在这种特定情况下失败:

<?php

// Note you must download the php files from the link above 
// and link to them on this line below. 
include_once('simple_html_dom.php');


$html = file_get_html('http://www.comelmar.it/index.aspx?idmnu=23&midx=3&mbidx=2&idmnub=8&Lang=EN');
$elem = $html->find('table[class=Descrizione]', 0);
echo $elem;

?> 

警告: file_get_contents(http://www.comelmar.it/index.aspx?idmnu=23&midx=3&mbidx=2&idmnub=8&Lang=EN) [function.file-get-contents]:无法打开流:HTTP 请求 失败的! HTTP/1.1 500 内部服务器错误 public_html/elin-custom-code/simple_html_dom.php 上 第 75 行

致命错误:在非对象上调用成员函数 find() public_html/elin-custom-code/sklad-1.php 第 9 行

【问题讨论】:

  • 看起来页面需要用户代理什么的。它抛出一个 500,这意味着有一个错误。你的Fatal error 是因为$html 是假的。

标签: php html dom include


【解决方案1】:

作为参考链接:-

file_get_contents - failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found

现在大多数主机提供阻止 furl_open 参数,该参数允许您使用 file_get_contents() 从外部 url 加载数据。

您可以使用CURLPHP 客户端库,例如Guzzle

为了确保我只是使用CURL 在下面的代码的帮助下检查确切的问题:-

<?php
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL,'http://www.comelmar.it/index.aspx?idmnu=23&midx=3&mbidx=2&idmnub=8&Lang=EN');
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Your application name');
$query = curl_exec($curl_handle);
curl_close($curl_handle);
echo "<pre/>";print_r($query);
?>

输出:- http://prntscr.com/a91nl5

因此,出于安全原因,这些方法调用(CURLfile_get_contents)似乎在该站点上被阻止。

你也可以试试这个链接的答案,也许你会得到一些有用的输出:-

PHP file_get_contents() returns "failed to open stream: HTTP request failed!"

祝你好运。

【讨论】:

  • 感谢您的回答!如果我理解正确,出于安全原因,这是不可能的?
  • 看起来,这就是为什么我在努力的同时给了你两个链接。还有更多答案,您需要检查并尝试它们。也许你能得到一些有用的东西。对我来说,安全似乎是你的问题。
  • 感谢您的标记。如果没有投票,那么也投票。
猜你喜欢
  • 1970-01-01
  • 2011-03-17
  • 2016-07-16
  • 2012-02-11
  • 1970-01-01
  • 2019-06-17
  • 2014-07-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多