본문 바로가기
웹 관련 기록/PHP

Sign in through Steam! 스팀 웹 API를 이용하여 json으로 정보를 받아오기

by Planetis 2013. 12. 18.
스팀 웹 API를 이용하여 json으로 정보를 받아오기
-> 원본글의 file_get_contents() 기능이 제가 사용하는 dothome 에선 막혀있어서 curl 이란 함수를 찾아다 교체했습니다.

Openid 소스 받기: https://gitorious.org/lightopenid/

우선, 스팀 API키를 발급받아야 하고, Openid 코드들 중 MIT 라이선스를 사용하는 LightOpenID를 본인의 웹서버에 업로드 해두어야 합니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
    error_reporting(E_ERROR | E_PARSE | E_WARNING);
    $user = new user;
    $user->apikey = "xxxxxxxxxxxxxxxxxxxxx"// 발급받은 API Key값.
    $user->domain = "domain.com"// 발급 받을때 등록한 도메인.
    function curl($url) {
        // file_get_contents 역할 대체용.
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $g = curl_exec($ch);
        curl_close($ch);
        return $g;
    }
 
    class user
    {
        public static $apikey;
        public static $domain;
 
        public function GetPlayerSummaries ($steamid)
        {
            $response = curl('http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=' . $this->apikey . '&steamids=' . $steamid);
            $json = json_decode($response);
            return $json->response->players[0];
        }
 
        public function signIn ()
        {
            require_once 'openid.php';
            $openid = new LightOpenID($this->domain);
            if(!$openid->mode)
            {
                $openid->identity = 'http://steamcommunity.com/openid';
                header('Location: ' . $openid->authUrl());
            }
            elseif($openid->mode == 'cancel')
            {
                print ('User has canceled authentication!');
            }
            else
            {
                if($openid->validate())
                {
                    preg_match("/^http:\/\/steamcommunity\.com\/openid\/id\/(7[0-9]{15,25}+)$/"$openid->identity, $matches); // steamID: $matches[1]
                    setcookie('steamID'$matches[1], time()+(60*60*24*7), '/'); // 1 week
                    header('Location: /');
                    exit;
                }
                else
                {
                    print ('fail');
                }
            }
        }
    }
    if(isset($_GET['login']))
    {
        $user->signIn();
    }
    if (array_key_exists( 'logout'$_POST ))
    {
        setcookie('steamID''', -1, '/');
        header('Location: /');
    }
    if(!$_COOKIE['steamID'])
    {
        print ('<form action="?login" method="post">
        <input type="image" src="http://cdn.steamcommunity.com/public/images/signinthroughsteam/sits_large_border.png"/>
        </form>');
    }
    else
    {
        print('<form method="post"><button title="Logout" name="logout">Logout</button></form>');
        echo $user->GetPlayerSummaries($_COOKIE['steamID'])->personaname;
        print('<img src="'.$user->GetPlayerSummaries($_COOKIE['steamID'])->avatar.'"/>');
    }
?>


320x100

댓글