2010-07-20 43 views
0

這只是正常PHP的echo價值將無法正常工作

<?php echo $response->user->last_game; ?> 

但這並不

$oscore=$response->user->current_game; 

我怎樣才能獲得$ oscore等於從這個腳本拉到該值:

private function clean_dirty_response($object) 
    { 
    // The final object 
    $final_object = new StdClass(); 

    // Convert the XML to an object 
    $object = simplexml_load_string($object); 

    // Now that we've converted the XML to an object we can start shifting things around 
    $final_object->user->membership_status = (string)$object->AccountStatus; 
    $final_object->user->last_game   = str_replace(' ',' ', (string)$object->PresenceInfo->Info); // For some reason there's some extra whitespace that needs to be removed 
    $final_object->user->current_game   = str_replace(' ',' ', (string)$object->PresenceInfo->Info2); // For some reason there's some extra whitespace that needs to be removed 
    $final_object->user->last_seen   = (string)$object->PresenceInfo->LastSeen; 
    $final_object->user->online    = (string)$object->PresenceInfo->Online; 
    $final_object->user->status_text  = (string)$object->PresenceInfo->StatusText; 
    $final_object->user->title    = (string)$object->PresenceInfo->Title; 
    $final_object->user->gamertag   = (string)$object->Gamertag; 
    $final_object->user->profile_url  = (string)$object->ProfileUrl; 
    // Jezus christ, why capitalize every freaking word? Just use lowercase next time you damn API! 
    $final_object->user->profile_picture = (string)$object->TileUrl; 
    $final_object->user->avatar    = 'http://avatar.xboxlive.com/avatar/' . str_replace(' ', '%20', $final_object->user->gamertag) . '/avatar-body.png'; 
    $final_object->user->country   = (string)$object->Country; 
    $final_object->user->reputation   = (int)$object->Reputation; 
    $final_object->user->bio    = (string)$object->Bio; 
    $final_object->user->location   = (string)$object->Location; 
    $final_object->user->reputation_image = (string)$object->ReputationImageUrl; 
    $final_object->user->gamerscore   = (string)$object->GamerScore; 
    $final_object->user->zone    = (string)$object->Zone; 

    // Now it's time to clean the RecentGames part 
    $final_object->recent_games    = array(); 
    $i          = 0; 

    // Loop through each game and clean it up 
    foreach ($object->RecentGames->XboxUserGameInfo as $recent_game) 
    { 
     $obj = new stdClass(); 

     $obj->name     = (string)$recent_game->Game->Name; 
     $obj->achievements   = (int)$recent_game->Achievements; 
     $obj->total_achievements = (int)$recent_game->Game->TotalAchievements; 
     $obj->gamerscore   = (int)$recent_game->GamerScore; 
     $obj->total_gamerscore  = (int)$recent_game->Game->TotalGamerScore; 
     $obj->thumb_32    = (string)$recent_game->Game->Image32Url; 
     $obj->thumb_64    = (string)$recent_game->Game->Image64Url; 

     // Format the date 
     $raw_date   = (string)$recent_game->LastPlayed; 
     $raw_date   = explode('T', $raw_date); 
     $date    = $raw_date[0]; 
     // Time 
     $raw_time   = $raw_date[1]; 
     $raw_time   = explode('+', $raw_time); 
     $time    = $raw_time[0]; 
     // Offset 
     $offset    = $raw_time[1]; 
     $obj->last_played = array('date' => $date, 'time' => $time, 'offset' => $offset); 
     $obj->details_url = (string)$recent_game->DetailsURL; 

     $final_object->recent_games[$i] = $obj; 

     // counter + 1 
     ++$i; 
    } 

    return $final_object; 
} 
+0

沒有看到更多的代碼,這是相當難以給出一個答案。 – kiamlaluno 2010-07-20 16:21:33

+0

完整代碼添加 – 2010-07-20 16:23:26

+0

您應該初始化'$ final_object-> user',然後才能使用它。 '$ final_object-> user = new stdClass()'應該足夠了。 – kiamlaluno 2010-07-20 16:26:04

回答

1

嘗試值轉換爲字符串,像這樣:

$oscore = strval($response->user->current_game);

0

嘗試使用var_dump()而不是echo。你可能有一個由所有空白字符組成的字符串,這個回聲不應該,但var_dump()將會變得非常清晰。

如果你還沒有看到任何東西,那麼確保它實際上通過你的功能。做var_dump((string)$object->PresenceInfo->Info2);var_dump($object->PresenceInfo->Info2);(有和沒有演員)。

乾杯。

+0

感謝您的答覆兄弟。回聲工作得很好,它試圖讓$ oscore等於什麼回聲不起作用的價值。 – 2010-07-20 16:50:03

+0

終於有了,只需將它轉換爲一個字符串; $ oscore = strval($ response-> user-> last_game); – 2010-07-20 18:26:06