首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XBox PHP调用

XBox PHP调用
EN

Stack Overflow用户
提问于 2012-10-30 23:21:32
回答 2查看 2.2K关注 0票数 1

以下是演示程序的链接:http://davidwalsh.name/xbox-api

我创建了一个包含以下内容的php文件。

代码语言:javascript
复制
<?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数据)

代码语言:javascript
复制
{
  "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*

我试着在一个php文件中这样做。

代码语言:javascript
复制
<?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" />

这导致页面仍然是空白的,所以当我查看源代码时,它返回的所有内容都是

代码语言:javascript
复制
<img src="" alt="" class="avatar" />

更新2 @ae14 & 2g

我也试过了(都在一个php文件中)

代码语言:javascript
复制
<?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建议也没有用。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-11-01 00:02:49

这是我的PHP文件:

代码语言:javascript
复制
<?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,因为我的玩家标签里面有一个空格。我建议你在打开php文件之前在浏览器上测试最终的url。

对于测试,使用$user->Gamertag,因为Name的属性始终为空。我也不知道原因。

这是从服务返回的JSON数据

代码语言:javascript
复制
 "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

您可以使用与Gamertag相同的方法访问...简单呼叫

代码语言:javascript
复制
$user->AvatarSmall

我没有在我的机器上安装php,所以首先我已经下载了PHP5.4.8for Windows,并且我已经使用了这个版本的内置web服务器Here更多信息。

希望能帮到你

票数 1
EN

Stack Overflow用户

发布于 2012-10-30 23:25:16

看起来你需要使用

代码语言:javascript
复制
$user = $json->Data

获取用户信息

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13142179

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档