phpjsonweb-scrapingsimpledom

Scraping & Returning JSON data php


I currently have a script, Can be found here: http://ddelay.co.uk/bus/find_services2.php

With the following code:

<?php
include('simple_html_dom.php');
$service = $_GET["stop"];
$debug = $_GET["debug"];

$url = $service;
if($debug === "yes"){
echo "URL IS: " . $url . "\n";
}

$search = array('?', ' ', '.asp&');
$replace = array('&', '+', '.asp?');

$url2 = str_replace($search, $replace, $url);
if($debug === "yes"){
echo "REPLACEMENTS: ". $url2 . "\n";
}
$end = "http://tsy.acislive.com" . $url2 . '&showall=1';
if($debug === "yes"){
echo "FINAL URL: ". $end;
}

$html = file_get_html($end);

$ret = $html-> getElementsByTagName('table');

print($ret);
?>

For example which will pull the table from tsy.acislive.com (example: http://ddelay.co.uk/bus/find_services2.php?stop=/web/public_service_stops.asp?service=22?operatorid=36?systemid=30?goingto=Woodhouse)

I then want to be able to convert this table to JSON data to use in my app. Unfortunately I have tried PHP's function JSON_encode($ret); but unfortunately that failed. Would anybody know of how I can convert this table pulled using Simple-Dom-Parser php into Json Data


Solution

  • If you want to convert to JSON, json_encode is the way you should do it. It's a really easy to use function. If you're having trouble with it, there'll be an underlying reason.

    Try these to find out what your data looks like:

    var_dump($html);
    var_dump($ret);
    

    From the PHP manual, the value passed to json_encode Can be any type except a resource., so if it's failing I can only assume that you aren't passing it the correct data.

    Post the results of those var_dump calls and I'll edit this with a solution for you.