file_get_html奇怪的输出

时间:2013-09-10 12:47:51

标签: php file-get-contents simple-html-dom

我已经编写了一小段代码,如下所示。

<?php
include_once 'simple_html_dom.php';

$url = "http://passenger.cm.be/nl/contactpoints/723?service-1057";

$html = file_get_html($url);

echo $html;
?>

当我在我的wamp homeserver上运行此代码时,它正在显示网页。 但是,当我将其上传到我的在线服务器时,我得到的结果如下:

{"contactpoint_address_id":723,"hq":null,"id":723,"language":2,"name":"CM-kantoor Aalst","zf":12,"address":{"bus":null,"contactpoint_municipality_id":631,"id":723,"lat":50.93767,"lng":4.037621,"municipality_name_de":"AALST","municipality_name_fr":"AALST","municipality_name_nl":"AALST","number":"10","street":"HOPMARKT","street_de":"HOPMARKT","street_fr":"HOPMARKT","street_nl":"HOPMARKT","zip":9300,"town":{"cm_id":1,"description_nl":"AALST","description_fr":"AALST","description_de":"AALST","id":9300,"nis":9300,"zip":9300}},"region":{"cm_region_id":120,"description_de":"CM Midden-Vlaanderen","description_fr":"CM Midden-Vlaanderen","description_nl":"CM Midden-Vlaanderen","id":12},"contactpoint_services":[{"contactpoint_id":723,"id":1057,"remark_closingdays":null,"remark_general":null,"remark_id":null,"remark_openinghours":"De CM-consulenten werken ook op afspraak. Een afspraak maken kan op de afsprakenwebsite of op 09 224 77 22.","service_id":1,"service":{"available_in_de":1,"available_in_fr":1,"available_in_nl":1,"description_de":"Gesch\u00e4ftsstelle","description_fr":"Agence","description_nl":"kantoor","icon":null,"id":1},"contactinfos":[{"contactinfo_type_id":2,"contactpoint_service_id":1057,"display_order":1,"id":379,"info":"053 76 15 16"},{"contactinfo_type_id":3,"contactpoint_service_id":1057,"display_order":1,"id":633,"info":"aalst.mvl@cm.be"},{"contactinfo_type_id":1,"contactpoint_service_id":1057,"display_order":1,"id":259,"info":"09 224 77 22"}],"hourinfo":{"contactpoint_service_id":1057,"id":2252,"remark_de":null,"remark_fr":null,"remark_nl":null},"openinghours":[{"contactpoint_service_id":1057,"day":1,"from":"09:30","id":2248,"sort_order":1,"until":"12:00","day_name":"Maandag"},{"contactpoint_service_id":1057,"day":1,"from":"13:30","id":2249,"sort_order":2,"until":"16:00","day_name":"Maandag"},{"contactpoint_service_id":1057,"day":2,"from":"09:30","id":2250,"sort_order":1,"until":"12:00","day_name":"Dinsdag"},{"contactpoint_service_id":1057,"day":3,"from":"09:30","id":2251,"sort_order":1,"until":"12:00","day_name":"Woensdag"},{"contactpoint_service_id":1057,"day":3,"from":"13:30","id":2252,"sort_order":2,"until":"16:00","day_name":"Woensdag"},{"contactpoint_service_id":1057,"day":4,"from":"09:30","id":2253,"sort_order":1,"until":"12:00","day_name":"Donderdag"},{"contactpoint_service_id":1057,"day":4,"from":"13:30","id":8451,"sort_order":2,"until":"16:00","day_name":"Donderdag"},{"contactpoint_service_id":1057,"day":5,"from":"09:30","id":2254,"sort_order":1,"until":"12:00","day_name":"Vrijdag"}]},{"contactpoint_id":723,"id":906,"remark_closingdays":null,"remark_general":null,"remark_id":null,"remark_openinghours":null,"service_id":2,"service":{"available_in_de":1,"available_in_fr":1,"available_in_nl":1,"description_de":"Briefkasten","description_fr":"Bo\u00eete aux lettres ou correspondant mutualiste","description_nl":"brievenbus","icon":null,"id":2},"contactinfos":[],"openinghours":[]},{"contactpoint_id":723,"id":4252,"remark_closingdays":null,"remark_general":"De adviserend geneesheer werkt op afspraak.","remark_id":null,"remark_openinghours":null,"service_id":4,"service":{"available_in_de":0,"available_in_fr":0,"available_in_nl":1,"description_de":"Vertrauensarzt","description_fr":null,"description_nl":"adviserend geneesheer","icon":null,"id":4},"contactinfos":[],"openinghours":[]},{"contactpoint_id":723,"id":1330,"remark_closingdays":null,"remark_general":"Je maakt een afspraak wanneer het jou past.\nAls je dat verkiest, komen we aan huis.\n\nTelefonisch bereikbaar op 09 267 59 09\nvan 8.30 tot 12.00 en van 13.00 tot 17.00 uur.\nOp dinsdag tot 12.00 en vrijdag tot 16.00 uur.\nE-mail: mwzorg.mvl@cm.be\n\nJe kunt ook langskomen tijdens de openingsuren.","remark_id":null,"remark_openinghours":null,"service_id":6,"service":{"available_in_de":1,"available_in_fr":1,"available_in_nl":1,"description_de":"Sozialdienst","description_fr":"Service social","description_nl":"dienst Maatschappelijk Werk","icon":null,"id":6},"contactinfos":[{"contactinfo_type_id":1,"contactpoint_service_id":1330,"display_order":1,"id":2196,"info":"09 267 59 09"}],"hourinfo":{"contactpoint_service_id":1330,"id":7182,"remark_de":null,"remark_fr":null,"remark_nl":null},"openinghours":[{"contactpoint_service_id":1330,"day":1,"from":"13:00","id":7182,"sort_order":1,"until":"17:00","day_name":"Maandag"}]},{"contactpoint_id":723,"id":3992,"remark_closingdays":null,"remark_general":"De Thuiszorgcentra zijn telefonisch bereikbaar via de Zorgtelefoon op 078 152 152. Je kunt mailen naar thuiszorgcentrum.mvl@cm.be","remark_id":null,"remark_openinghours":null,"service_id":7,"service":{"available_in_de":0,"available_in_fr":0,"available_in_nl":1,"description_de":null,"description_fr":null,"description_nl":"Thuiszorgcentrum","icon":null,"id":7},"contactinfos":[{"contactinfo_type_id":1,"contactpoint_service_id":3992,"display_order":1,"id":2197,"info":"078 152 152"}],"openinghours":[]}]}

我完全不知道这是什么以及为什么会这样显示。 有没有其他人以前遇到过这个问题并且知道解决方案?

提前致谢。

2 个答案:

答案 0 :(得分:0)

这是对你的暗示,明确表示你不应该抓取并解析原始HTML,而应使用文明的API。

答案 1 :(得分:0)

使用cURL代替下载网页...

这是我用于此目的的一个功能:

function dlPage($href) {

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_URL, $href);
    curl_setopt($curl, CURLOPT_REFERER, $href);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    $str = curl_exec($curl);
    curl_close($curl);

    return $str;
}

然后以这种方式加载内容:

$url = "http://passenger.cm.be/nl/contactpoints/723?service-1057";

// Create a DOM object & load HTML from a URL 
$html = new simple_html_dom();

// Load HTML from a string
$html->load(dlPage($url));

echo $html;

确保你已经激活了php cURL模块...