简体   繁体   English

XBox PHP API调用

[英]XBox PHP API Call

Here is a link to the demo: http://davidwalsh.name/xbox-api 以下是该演示的链接: http//davidwalsh.name/xbox-api

I created a php file with the following content.. 我创建了一个包含以下内容的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;
?>    

Here is what its supposed to look like when i load the file (except with my gamertag data) 这是我加载文件时应该看起来的样子(除了我的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"
  }
}

But it is not displaying anything, what am i doing wrong? 但它没有显示任何东西,我做错了什么?

When I go to http://www.xboxleaders.com/api/profile/ryanfabbro.json , it displays fine. 当我访问http://www.xboxleaders.com/api/profile/ryanfabbro.json时 ,它显示正常。

Uodate* Uodate *

i tryd doing this in a php file 我尝试在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" />

this resulted in the page still being blank so when i viewed the source all it returned was 这导致页面仍然是空白,所以当我查看源它返回的全部是

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

update 2 @ae14 & 2g 更新2 @ ae14&2g

i also tried (all in 1 php file) 我也试过(所有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 ?>

which still resulted in a blank page, is that what you were meaning i should do? 这仍然是一个空白页面,是你应该做的意思? i also tried this same way with 2g suggestion to no avail 我也尝试过同样的方式,2g的建议无济于事

It looks like you need to use 看起来你需要使用它

$user = $json->Data

to get the user information 获取用户信息

This is my PHP file : 这是我的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;

?>

I've to use urlEncode beacause my gamertag had a spaces inside. 我要使用urlEncode,因为我的gamertag内部有空格。 I suggest you to test the final url on the browser before start the php file. 我建议你在启动php文件之前在浏览器上测试最终的url。

For test use $user->Gamertag because Name's property it's always blank.. I don't know why. 对于测试使用$ user-> Gamertag因为Name的属性它总是空白的..我不知道为什么。

This is JSON Data returned from the service 这是从服务返回的JSON数据

 "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

You can access using the same method for the Gamertag... simpy call 您可以使用相同的方法访问Gamertag ...简单的呼叫

$user->AvatarSmall

I don't had php installed on my machine so first I've downloaded the PHP 5.4.8 for Windows and I've used the Built-in web server of this release Here more info. 我没有在我的机器上安装php,所以首先我已经下载了适用于Windows的PHP 5.4.8并且我使用了此版本的内置Web服务器这里有更多信息。

Hope can help you 希望可以帮到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM