0

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

4
  • which one you want to get [level] => 30? Commented Aug 22, 2013 at 4:19
  • I would like to pull all the data out of the stdClass object. I'm much more familiar with using arrays with PHP but I would like to learn objects. I guess I'd like to be able to access then by using for example: $data->accountID $data->summonerID ... Commented Aug 22, 2013 at 4:22
  • Did you even try to print_r($yourObject); ? That would actually tell you a lot Commented Aug 22, 2013 at 4:52
  • The data that I posted is actually from print_r(). I'm just stuck on where to go from there. Commented Aug 22, 2013 at 4:54

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.