Tomato
Tomato

Reputation: 129

DOMDocument sometimes return tangle of characters

I need to extract DOM from external website in php. I tried testing URL, but sometimes it shows a many many chinesse letters :) (more specifically characters in unicode I though) It's strange, that if I use different link, it works, but if I use link below and run php for example 3 times, after 3. try it stops working (but for the 1, a 2. time it shows normal DOM structure)

URL: https://www.csfd.cz/film/300902-bohemian-rhapsody/prehled/

DOM after 3. (ca.) run: https://i.sstatic.net/lnM1I.png

Code:

$doc = new \DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTMLFile("https://www.csfd.cz/film/300902-bohemian-rhapsody/prehled/");
dd($doc->saveHTML());

Does anybody know, what to do?

Upvotes: 1

Views: 38

Answers (1)

Andrii  Filenko
Andrii Filenko

Reputation: 984

I guess it is because of the site compression, you can extract data by using good old curl:

<?php

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://www.csfd.cz/film/300902-bohemian-rhapsody/prehled/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');

curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');

$headers = array();
$headers[] = 'Connection: keep-alive';
$headers[] = 'Cache-Control: max-age=0';
$headers[] = 'Save-Data: on';
$headers[] = 'Upgrade-Insecure-Requests: 1';
$headers[] = 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36';
$headers[] = 'Dnt: 1';
$headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8';
$headers[] = 'Accept-Encoding: gzip, deflate, br';
$headers[] = 'Accept-Language: en-US;q=0.8,en;q=0.7,uk;q=0.6';
$headers[] = 'Cookie: nette-samesite=1; developers-ad=1;';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close ($ch);

$doc = new \DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($result);
dd($doc->saveHTML());

Upvotes: 1

Related Questions