获取网页的内容并放在数组或字符串中
I am not really sure how to phrase this question, so I will just ask the best I can.
I would like to know how to grab the contents of a webpage and place it in a string or array so I can parse the data.
This is the webpage: https://campusdata.uark.edu/api/buses?callback=?&routeIds=8
The webpage returns something like this:
?([{"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"}]);
I am not sure the best way to go about this... AJAX through JQuery? Maybe a php call? I don't know.
I have searched for this, but like I said, I don't know exactly how to phrase the question, so my search results have been sporadic at best. Can someone help me please?
我不确定如何说出这个问题,所以我会尽我所能。 p>
我想知道如何获取网页的内容并将其放在字符串或数组中,以便我可以解析数据。 p>
这是网页: https://campusdata.uark.edu/api/buses?callback=?&routeIds=8 p>
网页返回如下内容: p>
我不确定最佳方式 这个... AJAX通过JQuery? 也许是一个PHP电话? 我不知道。 p>
我已经搜索了这个,但就像我说的那样,我不确切地知道如何说出这个问题,所以我的搜索结果充其量只是零星的。
有人可以帮帮我吗? p>
div>?([{“id”:25,“fleet”:15,“name”:“Fleet 15”,“description”:“”,“zonarId”:9,“gpsId”: “8061088”, “纬度”: “36.0680039”, “东经”: “ - 94.1758039”, “速度”:0.000, “标题”:89.700, “权力”:真实的, “日期”:“\ /日期(1456339080000) \ /“,”color“:”#0090ff“,”routeName“:”Blue“,”routeId“:8,”distance“:9999999999,”nextStop“:”Garland Center“,”nextArrival“:”8分钟“ },{“id”:33,“fleet”:6,“name”:“Fleet 6”,“description”:“”,“zonarId”:13,“gpsId”:“8061090”,“纬度”:“ 36.0818423" , “东经”: “ - 94.1707598”, “速度”:0.000, “标题”:181.700, “权力”:真实的, “日期”: “\ /日期(1456339200000)\ /”, “色”:” #0090ff“,”routeName“:”Blue“,”routeId“:8,”distance“:2.31887983012931,”nextStop“:”South Creekside“,”nextArrival“:”1分钟“}]);
code > pre>
Seems like a JSONP call. You can use jQuery to easily fetch the data from the API end point. Please see the example below:
$.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
}
});
Here is a jsfiddle with working example. Please make sure to include jquery in the page before trying this.
Get the page content with file_get_contents
function. Remove illegal character. Convert the json format to PHP array:
<?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));
Your can use this in PHP
$site = file_get_contents("http://campusdata.uark.edu/api/buses?callback=&routeIds=8");
print_r(json_decode($site));
Reference