nielsv
nielsv

Reputation: 6800

Symfony2 DOMCrawler + U use a browser too old for this website

I want to get the data from the following page: http://klvv.be/#/matches/ranking/2/2
When I check the webpage in my browser everything is fine. Now I want to use data from the webpage in my PHP action like this:

$html = file_get_contents("http://klvv.be/#/matches/ranking/2/2");

$crawler = new Crawler($html);

print_r($html);

But when I print out the html I get a different page of the webpage. This is what I get: enter image description here

The text is in Dutch but it says that I use a browser to old for the website. But I'm still in the same browser as I check the page ... Is there a way around this?

Upvotes: 0

Views: 272

Answers (2)

Dave Chen
Dave Chen

Reputation: 10975

The website in question uses JavaScript to download the match information. You will need to access their API which includes everything found on their charts.

<?php

$json = file_get_contents('http://klvv.be/server/seriedata.php?serieId=2');
$json = json_decode($json, true);

echo '<table border="1">';
foreach($json['rankings'][0]['rankingRows'] as $rankings) {
    echo '<tr>';
    foreach($rankings as $index => $rank)
        echo '<td>' . $index . '</td><td>' . $rank . '</td>';
    echo '</tr>';
}
echo '</table>';

You can use print_r($json) to see what data it provides.

Output:

Upvotes: 2

Marc B
Marc B

Reputation: 360592

You're NOT using your browser in PHP. The request that PHP is making via file_get_contents() to the site from your server is COMPLETELY independent of the request that you browser made to your PHP script. You need to set a user agent so it'll identify as some other browser, e.g. using curl:

curl_setopt($ch, CURLOPT_USERAGENT, 'Name of browser you want to use');

If you insist on using the internal PHP handers, you'll have to set up a stream and set the same option there.

Upvotes: 0

Related Questions