如何使用PHP和cURL与XBox API交互 [英] How to interact with XBox API using PHP and cURL

查看:222
本文介绍了如何使用PHP和cURL与XBox API交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图学习如何与非官方的xbox api(xboxapi.com)交互,但我似乎不知道如何使用它。文档非常缺乏。这是我最近的(我认为最好的)尝试。

I am trying to learn how to interact with the unofficial xbox api (xboxapi.com) but I can't seem to figure out how to use it. The documentation is very scarce. This is my most recent (and what i thought best) attempt.

<?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 Moved Permanently。任何人都可以看到我做错了吗?感谢。

Upon running the above I get "301 Moved Permanently". Can anyone see what i am doing wrong? Thanks.

推荐答案

您需要将 xuid 替换为实际的xbox配置文件用户名。
另外用您的API验证码替换 InsertAuthCodeHere
登录Xbox Live后,您可以在您的xboxapi帐户配置文件中找到。

You need to replace xuid with your actual xbox profile user id. Additionally replace InsertAuthCodeHere with your API auth code. You can find both on your xboxapi account profile after logging into xbox live.

请参阅: https://xboxapi.com/v2/2533274813081462/xboxonegames

更新 - Guzzle

我可以使用 Guzzle ,使用 http https

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 =>验证SSL证书有关。 false 以及从 http:// www。 https:// 其中启用 CURLOPT_FOLLOWLOCATION => true

The issue is related to validating the SSL certificate via CURLOPT_SSL_VERIFYPEER => false and the redirect from http://www. to https:// occurring which is enabled with CURLOPT_FOLLOWLOCATION => true

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

这篇关于如何使用PHP和cURL与XBox API交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆