file_get_html weird output

2019-08-09 13:17发布

问题:

I've written a very small piece of code as you can see below.

<?php
include_once 'simple_html_dom.php';

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

$html = file_get_html($url);

echo $html;
?>

When I run this code on my wamp homeserver, it is showing the webpage as it should. However, when I upload it to my online server I'm getting the result below:

{"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":[]}]}

I have absolutely no idea what this is and why it is showing like this. Has anyone else experienced this problem before and know a solution?

Thanks in advance.

回答1:

This is a hint for you, clearly says that you should NOT grab and parse raw HTML but use civilized APIs instead.



回答2:

Use cURL instead to download the web page...

Here's a function I use for that purpose:

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;
}

Then you load the content this way:

$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;

And be sure you have the php cURL module activated...