抓取并返回 JSON 数据 php [关闭]
-
20-12-2019 - |
题
我目前有一个脚本,可以在这里找到: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);但不幸的是失败了。有人知道如何将使用 Simple-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
致电,我将编辑此内容并为您提供解决方案。
不隶属于 StackOverflow