XboxApi.com身份验证和JSON请求

时间:2014-06-25 09:42:26

标签: json curl jsonp xbox

我试图通过PHP中的curl与xboxapi.com进行身份验证,并且我一直在获取未经授权的401.

要连接到API,我们需要一个身份验证标头。这是以X-AUTH发送的 https://xboxapi.com/documentation

我使用以下代码进行身份验证:

$headerArr = array();
$headerArr[] = "X-AUTH: here i put my api key";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://xboxapi.com/v2/accountXuid');   
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArr);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);

我无法弄清楚代码有什么问题......

我遇到的第二个问题是当我试图从api获取数据时(我已经登录进行检查)

我正在使用:

<script>
$(document).ready(function() {

 $.ajax({
        url: 'https://xboxapi.com/v2/accountXuid',
        dataType: 'jsonp',
        jsonpCallback: 'callback',
        jsonp: false,
    });
});
</script>

在firebug控制台中我得到了:

SyntaxError: missing ; before statement.

好吧,我得到它,它是JSON而不是JSONP但是在网络响应中(在firebug中)我看到了JSON。 有没有办法加载JSON而不是JSONP?如何在屏幕上看到结果?

欢迎任何其他解决方案! 谢谢。

2 个答案:

答案 0 :(得分:0)

这最好是作为SO的两个职位;让我们首先处理PHP方面的问题。设置一个能够做到这一点的函数,暂时消除AJAX,这样我们就不会添加超出我们需要的变量。

接下来,我会尽可能多地从该卷曲调用中删除。以下内容返回JSON格式的字符串,如我所料:

$url     = 'https://xboxapi.com/v2/accountXuid';
$headers = array(
    'X-AUTH: API_KEY'
);

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);

print_r($result);

如果/当你得到那么多工作,回复,我们可以淘汰AJAX方面。

答案 1 :(得分:0)

我遇到了同样的问题并提出了这个解决方案。此示例从xboxapi.com API的v2调用配置文件端点。

首先,我们设置curl变量。请记住用我自己的xuid和auth键替换我插入的#xuid#和#auth_key#变量(省略#&#39; s)。

$profileServiceUrl = "https://xboxapi.com/v2/#xuid#/profile";
$contentType = 'text/xml';          //probably not needed
$method = 'POST';                   //probably not needed
$auth = 'X-AUTH: #auth_key#';    //API Key

接下来,我们进行卷曲

$profileCurl = curl_init();
curl_setopt($profileCurl, CURLOPT_URL, $profileServiceUrl);
curl_setopt($profileCurl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($profileCurl, CURLINFO_HEADER_OUT, true);
curl_setopt($profileCurl, CURLOPT_HTTPHEADER, Array('Content-type: ' . $contentType . '; auth=' . $auth));
curl_setopt($profileCurl, CURLOPT_HTTPHEADER, Array($auth));

最后我们抓取并解码我们从API调用中收到的Json。

$profileJson = curl_exec($profileCurl);
$profileData = json_decode($profileJson);

总之,它应该是这样的。

$profileServiceUrl = "https://xboxapi.com/v2/#xuid#/profile";
$contentType = 'text/xml';          //probably not needed
$method = 'POST';                   //probably not needed
$auth = 'X-AUTH: #auth_key#';    //API Key
$profileCurl = curl_init();
curl_setopt($profileCurl, CURLOPT_URL, $profileServiceUrl);
curl_setopt($profileCurl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($profileCurl, CURLINFO_HEADER_OUT, true);
curl_setopt($profileCurl, CURLOPT_HTTPHEADER, Array('Content-type: ' . $contentType . '; auth=' . $auth));
curl_setopt($profileCurl, CURLOPT_HTTPHEADER, Array($auth));
$profileJson = curl_exec($profileCurl);
$profileData = json_decode($profileJson);

如果您在抓取Json数据时遇到问题,它应该看起来像这样。

$gamerTag = $profileData->Gamertag;

我知道这个回复很晚,希望你看到这个并且有所帮助!