我正在使用API來獲取一些值。當我打印我的變量從API返回一個數組,它返回stdObject Class
在php中獲取stdObject的值
這是我的請求數據:
$graphObject = (new FacebookRequest($session, 'GET', '/me?fields=id,first_name,last_name,tagged_places'))->execute()->getGraphObject()->asArray();
這是怎麼了,我打印出來:
echo '<pre>' . print_r($graphObject, 1) . '</pre>';
這是什麼它返回:
Array
(
[id] => 111111
[first_name] => Bob
[last_name] => Stevensen
[tagged_places] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[id] => 111111
[created_time] => 1905-01-01T08:00:00+0000
[place] => stdClass Object
(
[id] => 1111111
[location] => stdClass Object
(
[latitude] => 36.1313
[longitude] => -95.9373
)
[name] => Tulsa, Oklahoma
)
)
[1] => stdClass Object
(
[id] => 11111
[created_time] => 2014-05-30T21:41:11+0000
[place] => stdClass Object
(
[id] => 111111
[location] => stdClass Object
(
[city] => Okmulgee
[country] => United States
[latitude] => 35.623012460758
[longitude] => -95.972782756346
[state] => OK
[street] => 104 S Morton Ave
[zip] => 74447-5022
)
[name] => Ike's Downtown Pub & Eatery
)
)
我發現了一些這樣的例子說明的方式得到一個值超出這個類的對象會是這樣,
echo $graphObject->{'$STDCLASS'}->{'$FIELD I'M TRYING TO GET'};
我換出這些價值觀,試圖找出對象的名稱是什麼,但我得到這個錯誤:
Notice: Trying to get property of non-object in /file/path
從這個stdClass Object
獲取值的正確方法是什麼我想也許我只是不理解Object
的名稱是否可以從我在此發佈的代碼中知道?
你不要通過字面'stdClass','$ graphObject [0] - > id'來訪問它 – user1978142
就檢索數據而言,對象就像一個數組,除了你需要使用'$ object->值'而不是'$ object ['value']''。你想要檢索什麼數據? – Dan
我想將每個值保存到數據庫中。所以在這種情況下,id創建時間的地方等 – wuno