2

我尝试使用 simple_html_dom 从网页中检索信息,如下所示:

<?PHP
include_once('dom/simple_html_dom.php');
$urlpart="http://w2.brreg.no/motorvogn/";
$url = "http://w2.brreg.no/motorvogn/heftelser_motorvogn.jsp?regnr=BR15597";
$html = file_get_html($url);

foreach($html->find('a') as $element) 
       if(preg_match('*dagb*',$element)) {
       $result=$urlpart.$element->href;

       $resultcontent=file_get_contents($result);
       echo $resultcontent;

       }

?>

$result 变量首先给了我这个 URL: http ://w2.brreg.no/motorvogn/dagbokutskrift.jsp?dgbnr=2011365320&embnr=0®nr=BR15597

当使用我的浏览器访问上述 URL 时,我得到了我期望的内容。

使用 $resultcontent 检索内容时,我得到了不同的结果,它在挪威语中显示为“无效输入”。

任何想法为什么?

4

2 回答 2

1

问题在于您的 URL 查询参数。

http://w2.brreg.no/motorvogn/dagbokutskrift.jsp?dgbnr=2011365320&embnr=0&regnr=BR15597

URL 中的字符串 '®' 将在 file_get_contents 函数中转换为 Symbol ®,这会阻止您获得实际结果。

您可以html_entity_decode在 #11 行中使用函数

$resultcontent=file_get_contents(html_entity_decode($result));
于 2013-10-11T09:36:39.683 回答
1
foreach($html->find('a') as $element) 
       if(preg_match('*dagb*',$element)) {
       $result=$urlpart.$element->href;
       $resultcontent=file_get_contents(html_entity_decode($result));
       echo $resultcontent;

       }

这应该可以解决问题。

于 2013-10-11T10:30:43.927 回答