抓取和返回 JSON 数据 php


Scraping & Returning JSON data php

我目前有一个脚本,可以在这里找到:http://ddelay.co.uk/bus/find_services2.php

使用以下代码:

<?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);
?>

例如,将从 tsy.acislive.com 中提取表(例如:http://ddelay.co.uk/bus/find_services2.php?stop=/web/public_service_stops.asp?service=22?operatorid=36?systemid=30?goingto=Woodhouse)

然后,我希望能够将此表转换为 JSON 数据以在我的应用程序中使用。 不幸的是,我已经尝试了PHP的函数JSON_encode($ret); 但不幸的是失败了。有人知道我如何将使用简单 Dom-Parser php 拉取的这个表转换为 Json 数据吗

如果你想转换为JSON,json_encode是你应该这样做的方式。这是一个非常易于使用的功能。如果您遇到问题,就会有一个根本原因。

请尝试以下操作以了解您的数据的外观:

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

从PHP手册中,该值传递给json_encode Can be any type except a resource.,因此,如果失败,我只能假设您没有向其传递正确的数据。

发布这些var_dump通话的结果,我将为您编辑解决方案。