Just a piece of PHP for getting data from www.bikeshare.ie. it is in a JSON format at the top of the webpage in JavaScript, I get it out of the webpage using a Regex and then pass it through a JSON decoder in PHP to get the piece I want.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<?php $ch = curl_init("https://www.bikeshare.ie/cork.html"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $htmlData = curl_exec($ch); curl_close($ch); //echo $htmlData; $pattern = '/{\"\s*([^\n\r]*)}]};/'; preg_match($pattern, $htmlData, $matches); $rest = substr($matches[0], 0, -1); $json = json_decode($rest,true); # check if there has been an error decoding: if (! isset($json)) { echo "Decoding error: " . json_last_error_msg() . PHP_EOL; } echo $json['cork'][0]["name"]; echo ", \n"; echo $json['cork'][0]["bikesAvailable"]; echo "\n"; ?> |