0

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?

Apolymoxic
  • 730
  • 14
  • 34
  • it's jsonp, you can fetch it with jquery, or simply point a script tag at the url: `` which logs the data. you need to pass it to a function that wants the data by changing `console.log` to your function's name if you want to do more than just log it. – dandavis Feb 24 '16 at 21:04
  • 'Web Crawler' is the phrase you are looking for http://stackoverflow.com/questions/13029811/how-can-i-get-a-web-page-into-a-string-using-javascript – Tim Roberts Feb 24 '16 at 21:04

3 Answers3

2

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.

TeaCoder
  • 1,958
  • 13
  • 13
  • you should put the callback param on the end of the url to use jsonp with jq... ed: oh, you specified the param name; that works too, it's newer and longer that what i was used to. – dandavis Feb 24 '16 at 21:06
  • Thank you for the lightning fast response plus the jsfiddle. This works... and so does the code from @Guillermo Andres Fuentes Moral. I prefer php so I will go with that, but you get the answer because you answered so quickly! – Apolymoxic Feb 24 '16 at 21:22
1

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

json_encode

file_get_contents

0

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));
Sky
  • 4,244
  • 7
  • 54
  • 83