What Hey Manga What Hey Manga - 3 months ago 13
PHP Question

Parsing PHP object value

I have the following

Imgur\Api\Model\Basic Object
(
[data:Imgur\Api\Model\Basic:private] => Array
(
[id] => 1XgbfFV
[title] => PIC 2 TITLE
[description] => PIC 2 DESC
[datetime] => 1472495069
[type] => image/jpeg
[animated] =>
[width] => 590
[height] => 1382
[size] => 35307
[views] => 0
[bandwidth] => 0
[vote] =>
[favorite] =>
[nsfw] =>
[section] =>
[account_url] =>
[account_id] => 0
[in_gallery] =>
[deletehash] => tZUGIGuV9Bfv6lV
[name] => PIC 2 NAME
[link] => http://i.imgur.com/1XgbfFV.jpg
[is_ad] =>
)

[success:Imgur\Api\Model\Basic:private] => 1
[status:Imgur\Api\Model\Basic:private] => 200
)


I trying get the [id] so I tried something like

$basic = $client->api('image')->upload($imageData);
$data = $basic->id;
print_r($data);


but value is empty. what should I do to get the value of [id] into $data

Thanks!

Answer

All items in the data object are private. What you should do is in your Imgur class, create a new method getData which returns the data object.

public function getData() 
{
    return $this->data;
}

You can also expand this further by creating a magic get method.

public function __get($property)
{
    if( isset($this->data[$property] ) {
        return $this->data[$property];
    }

    return null;
}

If doing it the magic method way, you can then just run, $basic->id and it should respond with 1XgbfFV because of the magic getter.

If this is not a class that you have created, you will need to look at the API from Imgur package to determine if there is an accessor for the data array.