dongzhan8001
2016-02-24 20:53
采纳率: 0%
浏览 49
已采纳

获取网页的内容并放在数组或字符串中

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?

图片转代码服务由CSDN问答提供 功能建议

我不确定如何说出这个问题,所以我会尽我所能。

我想知道如何获取网页的内容并将其放在字符串或数组中,以便我可以解析数据。

这是网页: https://campusdata.uark.edu/api/buses?callback=?&routeIds=8

网页返回如下内容: <前> ?([{“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分钟“}]);

我不确定最佳方式 这个... AJAX通过JQuery? 也许是一个PHP电话? 我不知道。

我已经搜索了这个,但就像我说的那样,我不确切地知道如何说出这个问题,所以我的搜索结果充其量只是零星的。 有人可以帮帮我吗?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douan4106 2016-02-24 21:06
    已采纳

    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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongyang7152 2016-02-24 20:58

    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));
    
    评论
    解决 无用
    打赏 举报
  • dongqing483174 2016-02-24 20:59

    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

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题