dongqiang1894 2013-08-22 04:06
浏览 41
已采纳

从API调用访问数据(PHP)

I recently started working with an API from https://www.mashape.com/ and I believe that I've gotten the data that I need but I'm not quite sure exactly how I can use it. It returns (I believe) an object which I tried typecasting to an array but I still haven't been successful in pulling out the data I need. That object looks like:

 Unirest\HttpResponse Object
(
    [code:Unirest\HttpResponse:private] => 200
[raw_body:Unirest\HttpResponse:private] => {
"_": {
    "APP_ID": "server_tracked"
},
"success": true,
"requestTime": "2013-08-21T21:02:59-07:00",
"shard": "North_America:YjNmMjE4YmVhZjgxN2M0ZGI0ZTU1YzQ0MWZiMzQ5MGJkMjFhMGRmOA",
"data": {
    "accountId": 37774341,
    "summonerId": 23638303,
    "name": "Naughtlok",
    "icon": 550,
    "internalName": "naughtlok",
    "level": 30
}
}
[body:Unirest\HttpResponse:private] => stdClass Object
    (
        [_] => stdClass Object
            (
                [APP_ID] => server_tracked
            )

        [success] => 1
        [requestTime] => 2013-08-21T21:02:59-07:00
        [shard] => North_America:YjNmMjE4YmVhZjgxN2M0ZGI0ZTU1YzQ0MWZiMzQ5MGJkMjFhMGRmOA
        [data] => stdClass Object
            (
                [accountId] => 37774341
                [summonerId] => 23638303
                [name] => Naughtlok
                [icon] => 550
                [internalName] => naughtlok
                [level] => 30
            )

    )

[headers:Unirest\HttpResponse:private] => Array
    (
        [content-type] => application/json; charset=utf-8
        [date] => Thu, 22 Aug 2013 04:02:59 GMT
        [server] => Apache-Coyote/1.1
        [x-api-calls-remaining] => -1
        [X-Mashape-Proxy-Response] => false
        [X-Mashape-Version] => 3.1.1
        [transfer-encoding] => chunked
        [Connection] => keep-alive
    )

)

Any pointers on I would be able to get for example "Level" out of "Data"?

  • 写回答

3条回答 默认 最新

  • douyi4544 2013-08-23 07:28
    关注

    Mashape sends back a response object, not an array. To access parts of the object you need to point at the object keys with php - here's the relevant section on unirest.io:

    Response Reference

    Upon recieving a response Unirest returns the result in the form of an Object, this object >should always have the same keys for each language regarding to the response details.

    'code' - HTTP Response Status Code (Example 200)
    'headers' - HTTP Response Headers
    'body' - Parsed response body where applicable, for example JSON responses are parsed to Objects / Associative Arrays.
    'raw_body' - Un-parsed response body

    So if you're doing something like print_r($response); to give us this, do echo $response -> raw_body; instead, then parse that as JSON (or get the parsed 'body' key).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan