我正在使用 DOMDocument
从网页中检索几位文本并将它们放入一个数组中。相同的代码在另一台服务器上工作,但在我的服务器上不起作用。对于 while
循环的每次迭代,我得到 Trying to get property of non-object
并且数组在最后保持为空。
$html = file_get_contents("http://sugarkettle.site44.com/catering.html");
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($html);
libxml_clear_errors();
$meatPrices = array();
function fillArrayFromDOM($array,$type) {
global $doc;
$i = 0;
$label = 1;
$array = array();
while ($label <= 15):
$array[$i] = $doc->getElementById($type.$label)->textContent;
$i++;
$label++;
endwhile;
return $array;
}
fillArrayFromDOM($meatPrices,"meat");
echo var_dump($meatPrices);
这是一个有效的链接: http://www.evintr.com/willtest.php
他在运行 GoDaddy 服务器,而我有一个本地 WAMP (2.2) 服务器。我可以提供的任何配置选项都可以解释为什么会发生这种情况?还是问题与我的服务器配置无关?
非常感谢任何帮助。提前致谢!
更新 1 - 2012 年 11 月 16 日
在我的服务器上,我测试了 $meatPrices[1] = $doc->getElementById('meat1')->textContent;
并且它有效。无论出于何种原因,在 while
循环中相同的表达式(除了 getElementById
参数中的变量)抛出错误:Trying to get property of non-object
.
更新 2 - 2012 年 11 月 17 日
我的 WAMP 服务器运行的是 PHP 版本 5.3.13。 我 friend 的服务器运行的是 PHP 版本 5.3.6。
最佳答案
尝试在您的 PHP 配置文件 (php.ini
) 中添加 allow_url_fopen=on
。保存并重启Apache;它应该工作...
编辑:
还要检查您是否启用了扩展程序 php_openssl.dll
(extension=php_openssl.dll
在您的 php.ini
文件中)。同样,需要重新启动 Apache。
编辑:
这取决于您拥有的 PHP 版本,但这里有两个可能的解决方案:
- 将行
fillArrayFromDOM($meatPrices,"meat");
替换为$meatPrices = fillArrayFromDOM($meatPrices,"meat")
;你也可以 更改您的函数以删除必要的 $meatPrices 参数)。
或
- 将行
function fillArrayFromDOM($array,$type) {
替换为function fillArrayFromDOM(&$array,$type) {//注意新字符 &;它将保留对 $array 变量的引用,因此它可以更改;
您还可以删除行:$array = array();
两者都应该有效;我很匆忙,没有时间等待您的评论回复。让我们知道您得到了什么...
关于php - 为什么 DOMDocument 不能与 WAMP 服务器一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13427403/