[英]Deriving the city and state from the location Facebook permission in Laravel 4
我試圖從Laravel中的Facebook的位置數組對象中分離出城市和州。 在進行此操作之前,我只是嘗試了類似於Oauth2控制器中嘗試的操作:
$citystate = $user['location'];
$citystate2 = $citystate['name'];
$split = explode(", ", $citystate2);
$city = $split[0];
$state = $split[1];
在提供程序文件Facebook.php中,我具有以下內容:
'location' => $user->location
但是,當我運行此命令時,出現錯誤:
Use of undefined constant name - assumed 'name'
因此,我的問題是,如何訪問位置數組對象的“名稱”部分。 謝謝您的幫助!
首先,您應該像這樣json_decode
(如果它是json字符串)
$userArray = json_decode($user, true);
因此,您可以像使用數組一樣使用它
$citystate = $userArray['location'];
您也可以檢查$user
是否已經是數組或不使用
if(is_array($user) && array_key_exists('location', $user)) {
// ...
$citystate = $user['location'];
// ...
}
然后你會得到一個像這樣的數組
array (size=2)
'id' => string 'xxx' (length=15) // xxx = digits
'name' => string 'Sylhet' (length=6)
更新: $user['location']
是一個對象:
object(stdClass)#180 (2) { ["id"]=> string(15) "114952118516947" ["name"]=> string(25) "San Francisco, California" }
因此,應為:
$citystate = $user['location'];
$citystate->name;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.