【问题标题】:How to interact with XBox API using PHP and cURL如何使用 PHP 和 cURL 与 Xbox API 交互
【发布时间】:2016-07-28 05:26:34
【问题描述】:

我正在尝试学习如何与非官方的 xbox api (xboxapi.com) 进行交互,但我似乎无法弄清楚如何使用它。文档非常稀缺。这是我最近(也是我认为最好的)尝试。

<?php
$gamertag = rawurlencode("Major Nelson");

$ch = curl_init("http://www.xboxapi.com/v2/xuid/" . $gamertag);

$headers = array('X-Auth: InsertAuthCodeHere', 'Content-Type: application/json');

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 ); # return into a variable
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers ); # custom headers, see above
$xuid = curl_exec( $ch ); # run!
curl_close($ch);

echo $xuid;


?>

运行上述内容后,我得到“301 永久移动”。谁能看到我做错了什么?谢谢。

【问题讨论】:

  • 所有示例似乎都是https
  • @RamRaider 这是一个很好的观察结果,但即使将其更改为 https 后仍然无法正常工作。
  • 你试过https://xboxapi.com/v2/accountXuid/而不是http://www.xboxapi.com/v2/xuid/吗?似乎最接近
  • @RamRaider accountXuid 用于返回我已链接到 API 的个人 Xbox 帐户的 XUID。
  • 哦,是的 - 我现在在文档中看到了端点。使用https://xboxapi.com/v2/xuid/Major%20Nelson 给了我一个关于No API Key provided or invalid API Key 的错误

标签: php curl


【解决方案1】:

您需要将 xuid 替换为您的实际 Xbox 配置文件用户 ID。 此外,将 InsertAuthCodeHere 替换为您的 API 身份验证代码。 登录 Xbox Live 后,您可以在您的 xboxapi 帐户资料中找到两者。

见:https://xboxapi.com/v2/2533274813081462/xboxonegames


更新 - Guzzle

我能够让它与 Guzzle 一起使用,与 httphttps 一起使用

require __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/config.php'; //defines XboxAPI_Key
$gamertag = isset($_GET['gamertag']) ? urldecode($_GET['gamertag']) : 'Major Nelson';
$url = 'http://xboxapi.com/v2/xuid/' . rawurlencode($gamertag);
$guzzle = new GuzzleHttp\Client();
$response = $guzzle->get($url, [
    'headers' => [
        'X-Auth' => XboxAPI_Key,
        'Content-Type' => 'application/json'
    ],
]);
echo $response->getBody(); //2584878536129841

更新 2 - cURL

该问题与通过 CURLOPT_SSL_VERIFYPEER =&gt; false 验证 SSL 证书以及使用 CURLOPT_FOLLOWLOCATION =&gt; true 启用的从 http://www.https:// 的重定向有关

require_once __DIR__ . '/config.php';
$gamertag = isset($_GET['gamertag']) ? urldecode($_GET['gamertag']) : 'Major Nelson';
$url = 'http://www.xboxapi.com/v2/xuid/' . rawurlencode($gamertag);
/**
 * proper url for no redirects
 * $url = 'https://xboxapi.com/v2/xuid/' . rawurlencode($gamertag);
 */
$options = [
    CURLOPT_RETURNTRANSFER => true, // return variable
    CURLOPT_FOLLOWLOCATION => true, // follow redirects
    CURLOPT_AUTOREFERER => true, // set referrer on redirect
    CURLOPT_SSL_VERIFYPEER => false, //do not verify SSL cert
    CURLOPT_HTTPHEADER => [
        'X-Auth: ' . XboxAPI_Key
    ]
]; 
$ch = curl_init($url);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
echo $content;  //2584878536129841

【讨论】:

  • 当我只知道他们的玩家代号时,我正在尝试获取用户的 XUID。
  • 知道了,那么您需要将 InsertAuthCodeHere 替换为您个人资料中的 API 密钥。
  • 我有。我只是将其从帖子中删除以保持私密。
  • 因为这是一个 XenForo 插件,在我无法控制的网络主机上,我认为我不能使用 Guzzle。除非我误会了。
  • 不确定 XenForo 的范围或您将如何使用它部署应用程序。我正在调查您的 cUrl 请求的具体问题,除了 GuzzlecUrl
【解决方案2】:

我得到了答案。我们缺少必需的花括号。工作代码:

$gamertag = rawurlencode("Major Nelson");

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://xboxapi.com/v2/xuid/{$gamertag}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "X-Auth: InsertAuthCode",
]);

$output = curl_exec($ch);

curl_close ($ch);

print $output;

【讨论】:

  • 大括号不会影响url的结果。正如我在回答中指出的那样,没有 www.... 的 URL 是您的问题。 EG:"a" . $gamertag"a$gamertag" 相同,"a{$gamertag}" 相同参见:ideone.com/rNeinC
猜你喜欢
  • 2012-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-05
  • 2020-10-08
  • 2020-07-18
  • 2015-09-22
  • 2017-07-24
相关资源
最近更新 更多