2012-10-30 37 views
1

這裏是演示的鏈接:http://davidwalsh.name/xbox-api的Xbox PHP API調用

我創建了包含以下內容的PHP文件..

<?php 
// Settings 
$gamertag = 'RyanFabbro'; 
$profileUrl = 'http://www.xboxleaders.com/api/profile/'.$gamertag.'.json'; 

// Get information about me 
$info = file_get_contents($profileUrl); 

// To JSON 
$json = json_decode($info); 
$user = $json->user; 
?>  

以下是其應該看起來像當我加載文件(除了我的gamertag數據)

{ 
    "status": { 
    "is_valid": "yes", 
    "is_cheater": "no", 
    "tier": "gold" 
    }, 
    "profile": { 
    "gamertag": "dwalsh83", 
    "gamerscore": 300, 
    "reputation": 20, 
    "gender": "male", 
    "motto": "Watch your head.", 
    "name": "David Walsh", 
    "location": "Madison, WI, US", 
    "bio": "There is, and only can be, Call of Duty.", 
    "url": "http:\/\/live.xbox.com\/en-US\/Profile?gamertag=dwalsh83", 
    "avatar_tile": "http:\/\/image.xboxlive.com\/global\/t.fffe07d1\/tile\/0\/2000b", 
    "avatar_small": "http:\/\/avatar.xboxlive.com\/avatar\/dwalsh83\/avatarpic-s.png", 
    "avatar_large": "http:\/\/avatar.xboxlive.com\/avatar\/dwalsh83\/avatarpic-l.png", 
    "avatar_body": "http:\/\/avatar.xboxlive.com\/avatar\/dwalsh83\/avatar-body.png", 
    "launch_team_xbl": "no", 
    "launch_team_nxe": "no", 
    "launch_team_kin": "no" 
    } 
} 

但它沒有顯示任何東西,我在做什麼錯?

當我去http://www.xboxleaders.com/api/profile/ryanfabbro.json,它顯示正常。

Uodate *

我tryd在PHP文件

<?php 
// Settings 
$gamertag = 'RyanFabbro'; 
$profileUrl = 'http://www.xboxleaders.com/api/profile/'.$gamertag.'.json'; 

// Get information about me 
$info = file_get_contents($profileUrl); 

// To JSON 
$json = json_decode($info); 
$user = $json->user; 
$user = $json->profile; 
$user = $json->data; 
$profile = $json->data; 
?> 

<img src="<?php echo $profile->avatar_body; ?>" alt="<?php echo $profile->gamertag; ?>" class="avatar" /> 

這導致頁面仍然是空白的,所以當我看到源做的這一切,它返回是

<img src="" alt="" class="avatar" /> 

更新2 @ ae14 & 2g

我也嘗試過(所有我n 1 php文件)

<?php 
// Settings 
$gamertag = 'RyanFabbro'; 
$profileUrl = 'http://www.xboxleaders.com/api/profile/'.$gamertag.'.json'; 

// Get information about me 
$info = file_get_contents($profileUrl); 

// To JSON 
$json = json_decode($info); 
$user = $json->data; 
?> 

<?php echo $user->name ?> 

這仍然導致一個空白頁,是你應該怎麼做?我也嘗試搭配2G的建議同樣的方式無濟於事

+1

除「但它只是空白的?」,這是一個聲明 - 你告訴我們,它是空白,而不是問我們 - 因此不應該實際上有一個問號,你根本就沒有問過這個問題。 –

+0

@RyanFabbro - 作爲警告,請不要侮辱人們評論你的問題。我已刪除了違規評論,但請在將來避免此事。 –

+0

你可以發佈你的頁面的一些HTML代碼? – 2GDev

回答

0

這是我的PHP文件:

<?php 
// Settings 

$gamertag = urlencode('yourgamertagwithspaceetcetc..'); 

$profileUrl = 'http://www.xboxleaders.com/api/profile/'.$gamertag; 

// Get information about me 
$info = file_get_contents($profileUrl); 

echo $info; 

// To JSON 
$json = json_decode($info); 
$user = $json->Data; 

echo $user->Gamertag; 

?> 

我必須使用urlEncode,因爲我的gamertag裏面有空格。我建議你在啓動php文件之前測試瀏覽器上的最終url。

用於測試使用$ user-> Gamertag,因爲名稱的屬性它總是空白..我不知道爲什麼。

這是

"Data": { 
    "Tier": "gold", 
    "IsValid": 1, 
    "IsCheater": 0, 
    "IsOnline": 0, 
    "OnlineStatus": "Last seen 11\/10\/2012 playing Modern Warfare&#174; 3", 
    "XBLLaunchTeam": 0, 
    "NXELaunchTeam": 0, 
    "KinectLaunchTeam": 0, 
    "AvatarTile": "https:\/\/avatar-ssl.xboxlive.com\/avatar\/xxxxxxxx\/avatarpic-l.png", 
    "AvatarSmall": "http:\/\/avatar.xboxlive.com\/avatar\/xxxxxx\/avatarpic-s.png", 
    "AvatarLarge": "http:\/\/avatar.xboxlive.com\/avatar\/xxxxxx\/avatarpic-l.png", 
    "AvatarBody": "http:\/\/avatar.xboxlive.com\/avatar\/xxxxxxx\/avatar-body.png", 
    "Gamertag": "xxxxxxxxxxxxxxxxx", 
    "GamerScore": 4165, 
    "Reputation": 20, 
    "Name": "", 
    "Motto": "", 
    "Location": "", 
    "Bio": "" 
    }, 
    "Stat": "ok", 
    "In": 2.273, 
    "Authed": "false", 
    "AuthedAs": null 

您可以訪問使用的玩家代號相同的方法...的simpy調用

$user->AvatarSmall 

我不上了PHP的安裝服務JSON數據返回我的我首先下載了PHP 5.4.8 for Windows,並且我已經使用了此版本的內置web服務器Here更多信息。

希望能幫助你

+0

,所以在閱讀所有這些帖子之後,看起來我應該添加回顯在html中,我是否在php文件或單獨的文件上執行此操作?任何人都可以告訴我一個例子 – RyanFabbro

+0

也是你提供的鏈接,不是用於從文件(已創建)調用json或至少我看到的,所以我有點讓我更困惑lol – RyanFabbro

+0

我編輯了我的答案並在我的機器上測試的例子 – 2GDev

3

它看起來像你需要使用

$user = $json->Data 

來獲取用戶信息

+0

謝謝你我試過,但它仍然顯示空白,它應該抓住的Xbox數據,所以我可以編輯和定製它的CSS,但我不能讓PHP顯示文本 – RyanFabbro

+0

我不太瞭解你的帖子中的JSON與你提供的鏈接不同。如果我直接點擊http://www.xboxleaders.com/api/profile/ryanfabbro.json,我看不到將用戶信息分組到「配置文件」對象中。無論哪種方式,它看起來像你在PHP中使用的變量名,然後在HTML中內聯不匹配。嘗試在你的PHP中使用$ user = $ json-> profile然後<?php echo $ user-> name; ?>在您的HTML – ae14

+0

中仔細看看您提供的Demo頁面,它看起來像自創建教程以來JSON的格式發生了變化。使用$ user = $ json-> Data和<?php echo $ user-> name?>。這應該工作 – ae14