为什么我不能使用php curl获取Steam用户配置文件以及如何获取Steam徽章图像?

时间:2016-03-31 08:43:41

标签: php curl steam badge

我目前正在使用php开发一个关于Steam的网站,可以获取用户的个人资料并制作玩家卡等等。我正在使用Steam Web Api来帮助我获取所需的信息。问题是,当我打算获得用户的徽章时,我会使用" IPlayerService / GetBadges / v1" api,它返回一个像这样的json数据:

{
    "response": {
        "badges": [
            {
                "badgeid": 23,
                "level": 300,
                "completion_time": 1434209651,
                "xp": 125,
                "scarcity": 0
            },
            {
                "badgeid": 18,
                "level": 15,
                "completion_time": 1404061200,
                "xp": 100,
                "scarcity": 102167
            },
            {
                "badgeid": 2,
                "level": 3,
                "completion_time": 1457872741,
                "xp": 500,
                "scarcity": 1334304
            },
            {
                "badgeid": 13,
                "level": 57,
                "completion_time": 1356099851,
                "xp": 260,
                "scarcity": 5832743
            },
            {
                "badgeid": 1,
                "level": 3,
                "completion_time": 1356099851,
                "xp": 150,
                "scarcity": 76725893
            },
            {
                "badgeid": 1,
                "appid": 231430,
                "level": 3,
                "completion_time": 1455540973,
                "xp": 300,
                "communityitemid": "817099833",
                "border_color": 0,
                "scarcity": 20438
            },
            {
                "badgeid": 1,
                "appid": 24010,
                "level": 1,
                "completion_time": 1403487961,
                "xp": 100,
                "communityitemid": "828384582",
                "border_color": 0,
                "scarcity": 15404
            },
            {
                "badgeid": 1,
                "appid": 35450,
                "level": 1,
                "completion_time": 1403573788,
                "xp": 100,
                "communityitemid": "842044372",
                "border_color": 0,
                "scarcity": 122236
            },
            {
                "badgeid": 1,
                "appid": 63380,
                "level": 1,
                "completion_time": 1403574599,
                "xp": 100,
                "communityitemid": "842169975",
                "border_color": 0,
                "scarcity": 222048
            },
            {
                "badgeid": 1,
                "appid": 303700,
                "level": 1,
                "completion_time": 1403743093,
                "xp": 100,
                "communityitemid": "866824198",
                "border_color": 0,
                "scarcity": 998614
            },
            {
                "badgeid": 17,
                "appid": 335590,
                "level": 1,
                "completion_time": 1420075581,
                "xp": 100,
                "communityitemid": "1438441936",
                "border_color": 0,
                "scarcity": 858393
            },
            {
                "badgeid": 1,
                "appid": 105450,
                "level": 1,
                "completion_time": 1451207235,
                "xp": 100,
                "communityitemid": "2454848086",
                "border_color": 0,
                "scarcity": 49755
            },
            {
                "badgeid": 1,
                "appid": 220200,
                "level": 1,
                "completion_time": 1451207275,
                "xp": 100,
                "communityitemid": "2454853894",
                "border_color": 0,
                "scarcity": 100821
            },
            {
                "badgeid": 2,
                "appid": 425280,
                "level": 3,
                "completion_time": 1451805075,
                "xp": 300,
                "communityitemid": "2483562201",
                "border_color": 0,
                "scarcity": 297565
            },
            {
                "badgeid": 1,
                "appid": 206440,
                "level": 1,
                "completion_time": 1451805331,
                "xp": 100,
                "communityitemid": "2549522337",
                "border_color": 0,
                "scarcity": 43807
            },
            {
                "badgeid": 1,
                "appid": 391220,
                "level": 1,
                "completion_time": 1455372224,
                "xp": 100,
                "communityitemid": "2699515873",
                "border_color": 0,
                "scarcity": 16843
            },
            {
                "badgeid": 1,
                "appid": 287390,
                "level": 1,
                "completion_time": 1457355907,
                "xp": 100,
                "communityitemid": "2764853882",
                "border_color": 0,
                "scarcity": 35158
            }
        ]
        ,
        "player_xp": 2735,
        "player_level": 18,
        "player_xp_needed_to_level_up": 65,
        "player_xp_needed_current_level": 2600
    }
}

此返回的数据仅告诉我有关badgeid和appid的信息,并且没有说明徽章图片的网址。我在Steam上查看了徽章图片,它的网址就像http://cdn.akamai.steamstatic.com/steamcommunity/public/images/items/105450/ 472be31927c4e77721fcba33bbd04b1331272e7e .png

因此我无法通过appid获取网址。 由于steam web api无法实现我的意图,我尝试使用php curl请求steam用户的个人资料页面和徽章页面来获取我需要的信息,我的代码是:

$url='http://steamcommunity.com/profile/76561198078931851';
$postFields='';
$cookie='';
$header=array('Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*//*;q=0.8', 'Accept-Encoding:gzip, deflate, sdch', 'Accept-Language:zh-CN,zh;q=0.8', 'Cache-Control:max-age=0', 'Connection:keep-alive','Host:steamcommunity.com', 'Upgrade-Insecure-Requests: 1','User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36');

$btch=curl_init($url);

curl_setopt($btch, CURLOPT_FAILONERROR, 0);
curl_setopt($btch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($btch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($btch, CURLOPT_TIMEOUT, 1);
curl_setopt($btch, CURLOPT_ENCODING, 'gzip,deflate,sdch');          //  防止乱码
curl_setopt($btch, CURLOPT_HEADER, 1);
curl_setopt($btch, CURLOPT_COOKIE, $cookie);
curl_setopt($btch, CURLOPT_HTTPHEADER, $header);
curl_setopt($btch, CURLOPT_HTTPGET, 1);

try{       
       $result=curl_exec($btch);
}            
catch(exception $e)  
{              
       echo $e->getMessage();
} 
if(!$result)
{
    echo 'false!';
}
echo '<pre>';
print_r($result);
echo '</pre>';

curl_close($btch);

卷曲确实有效,但是蒸汽永远不会返回用户的个人资料页面,而是总是将请求重定向到Steam社区的主页并要求我登录。因此我无法继续获取用户个人资料。 我读了以下相关问题: Steam Web API get Badge Image URL 有没有人知道如何解决这个问题,你能给我一些帮助吗?非常感谢提前!

1 个答案:

答案 0 :(得分:0)

我似乎找到了解决问题的方法......只是使用 file_get_contents()而非curl请求Steam用户个人资料页面。 :)