我不确定如何表达这个问题,所以我会尽我所能。
我想知道如何获取网页的内容并将其放在字符串或数组中,以便我可以解析数据。
这是网页:https://campusdata.uark.edu/api/buses?callback=?&routeIds=8
网页返回如下内容:
?([{"id":25,"fleet":15,"name":"Fleet 15","description":"","zonarId":9,"gpsId":"8061088","latitude":"36.0680039","longitude":"-94.1758039","speed":0.000,"heading":89.700,"power":true,"date":"\/Date(1456339080000)\/","color":"#0090ff","routeName":"Blue","routeId":8,"distance":9999999999,"nextStop":"Garland Center","nextArrival":"8 mins"},{"id":33,"fleet":6,"name":"Fleet 6 ","description":"","zonarId":13,"gpsId":"8061090","latitude":"36.0818423","longitude":"-94.1707598","speed":0.000,"heading":181.700,"power":true,"date":"\/Date(1456339200000)\/","color":"#0090ff","routeName":"Blue","routeId":8,"distance":2.31887983012931,"nextStop":"South Creekside","nextArrival":"1 mins"}]);
我不确定最好的方法...通过JQuery的AJAX?也许是一个PHP电话?我不知道。
我已经搜索了这个,但就像我说的那样,我不确切地知道如何说出这个问题,所以我的搜索结果充其量只是零星的。 有人能帮帮我吗?
答案 0 :(得分:2)
好像是一个JSONP调用。您可以使用jQuery轻松地从API端点获取数据。请参阅以下示例:
$.ajax({
url: "https://campusdata.uark.edu/api/buses?callback=?&routeIds=8",
// The name of the callback parameter
jsonp: "callback",
// Tell jQuery we're expecting JSONP
dataType: "jsonp",
data: {},
// Work with the response
success: function( response ) {
console.log( response ); // server response
}
});
这是一个带有工作示例的jsfiddle。 在尝试此操作之前,请确保在页面中包含jquery。
答案 1 :(得分:1)
你可以在PHP中使用它
$site = file_get_contents("http://campusdata.uark.edu/api/buses?callback=&routeIds=8");
print_r(json_decode($site));
<强>参考强>
答案 2 :(得分:0)
使用file_get_contents
功能获取页面内容。删除非法字符。将json格式转换为PHP数组:
<?php
$page = file_get_contents('https://campusdata.uark.edu/api/buses?callback=?&routeIds=8');
$page = substr($page, 0, -2);
$page = substr($page, 2);
var_dump (json_decode($page));