0

this time I'm looking for something really special. In my PHP page, I got a table generated by a Javascript, here is the example :

Example Page

This table is racing game results. I didn't write the JS, and I can't change the format of the results. What I need is to parse these results to get variables, to generate championship results giving points to guys, adding points of multiple series, etc...

I tried :

  • Parsing with DOMDocument, also substr, but as it's JS generated it can't work.

  • >>this solution<< which sounded good but it doesn't work.

Do you guys have any idea to get an exploitable array ? If not, what do you suggest as alternative solution ? I'm not able to reproduce the JS in a PHP function, too hard.

Here is the JS : click Thank you !

Community
  • 1
  • 1
Niko
  • 65
  • 1
  • 8
  • 1
    You're not going to be able to get, via PHP, anything resulting from JavaScript. It's a Chronology issue; PHP happens before JS. If PHP loads the page it will see only source code; JS is not run. You can use what's called a headless browser to have PHP execute the JS, but this is not without complication and needs a bit of set-up. – Mitya Aug 17 '14 at 21:54
  • 1
    Its actually fairly trivial with phantomJS, but it requires installation on the server. recreating the js logic in php is the way forward. – Steve Aug 17 '14 at 21:57
  • 1
    Your best bet would be to try and parse the data source itself, not the presentation. It appears to be in a JavaScript array called resultslines. That's very doable with PHP. Grabbing the JavaScript generated table, however, isn't since it runs on the client (browser) and PHP runs on the server. – thordarson Aug 17 '14 at 21:59
  • So I had another idea, still in the way to make it simple for me. You have to understand that I'm really not familiar with JS. So, what do you think about populate a PHP form (with a table appearence) with this JS instead of printing it ? By this way, I'll be able to get all values when submitting, right ? If you think that's possible, don't hesitate to give me keys ;) – Niko Sep 02 '14 at 13:36

1 Answers1

1

Here's how you'd be able to parse the initial array in PHP:

$url = 'http://mxs-concept.com/liveresults/test.php';
$page = file_get_contents($url); // fetch the page

preg_match('/resultslines=(.+)print_race_analysis/sim', $page, $matches); // find javascript array
$js = preg_replace('/,\s+\]$/', ']', $matches[1]); // fix last comma before "]"

$array = json_decode($js); // decode the array
var_dump($array); // see the exact array you had in javascript

However, after reading your edit, it seems that you'd need more work if you're taking the PHP way. You might want to try to install NodeJS and make it parse the JS but might be an overkill. If I would you, I'd translate the JS to PHP even if it will take a day just because NodeJS might take a week instead.

s3v3n
  • 8,203
  • 5
  • 42
  • 56
  • The problem is I don't know JavaScript, so I'm gonna try to find the equivalent of each things like `Math.floor` , etc. So trust me it will be 1 week. – Niko Aug 18 '14 at 13:51
  • 1
    It's dangerous to go alone! Take MDN with you: https://developer.mozilla.org/en-US/ ! – s3v3n Aug 18 '14 at 14:00
  • Merci ! I didn't know MDN, looks helpfull. – Niko Aug 19 '14 at 06:51
  • You know what, I thought with your code I'll get an output like the PHP one but I just have the script tag text. So I had another idea, still in the way to make it simple for me. You have to understand that I'm really not familiar with JS. So, what do you think about populate a PHP form (with a table appearence) with this JS instead of printing it ? By this way, I'll be able to get all values when submitting, right ? If you think that's possible, don't hesitate to give me keys ;) – Niko Aug 20 '14 at 19:10