質問

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"?

役に立ちましたか?

解決

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).

他のヒント

use print_r($response); print object & see what is the response eg:

<?php
require_once 'lib/Unirest.php';
// These code snippets use an open-source library. http://unirest.io/php
$response = Unirest::get("Your_URL",

//echo $response;
print_r($response); 

The release of Unirest 2.0 had many improvements including ability to set custom JSON decode flags

this gives you more control over the response body type parsing method (json_decode)

Disclaimer: I'm the author of unirest-php and I work at Mashape.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top