Skip to content

Commit 311f0cf

Browse files
committed
world of warcraft game data apis access tokens
1 parent 209908c commit 311f0cf

3 files changed

Lines changed: 87 additions & 0 deletions

File tree

wow_game_data_api/defines.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
// api creds
3+
define( 'CLIENT_ID', 'YOUR-CLIENT-ID' );
4+
define( 'CLIENT_SECRET', 'YOUR-CLIENT-SECRET' );
5+
6+
// db creds
7+
define( 'DB_HOST', 'localhost' );
8+
define( 'DB_NAME', 'wow_game_data_api' );
9+
define( 'DB_USER', 'root' );
10+
define( 'DB_PASS', '' );

wow_game_data_api/functions.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
include 'defines.php';
3+
4+
function getDatabaseConnection() {
5+
try {
6+
$conn = new PDO( 'mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASS );
7+
return $conn;
8+
} catch ( PDOException $e ) {
9+
die( $e->getMessage() );
10+
}
11+
}
12+
13+
function updateAccessToken( $accessToken ) {
14+
$databaseConnection = getDatabaseConnection();
15+
16+
$statement = $databaseConnection->prepare( '
17+
UPDATE
18+
config
19+
SET
20+
value = :value
21+
WHERE
22+
id = :id
23+
' );
24+
25+
$params = array(
26+
'id' => 'access_token',
27+
'value' => $accessToken
28+
);
29+
30+
$statement->execute( $params );
31+
}
32+
33+
function generateAccessToken() {
34+
$params = array(
35+
'grant_type' => 'client_credentials'
36+
);
37+
$tokenUri = 'https://us.battle.net/oauth/token';
38+
39+
$ch = curl_init();
40+
41+
curl_setopt( $ch, CURLOPT_USERPWD, CLIENT_ID . ":" . CLIENT_SECRET );
42+
curl_setopt( $ch, CURLOPT_URL, $tokenUri );
43+
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $params ) );
44+
curl_setopt( $ch, CURLOPT_POST, 1 );
45+
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false );
46+
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
47+
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
48+
49+
$response = curl_exec( $ch );
50+
curl_close( $ch );
51+
52+
$accessTokenResponse = json_decode( $response, true );
53+
54+
if ( isset( $accessTokenResponse['access_token'] ) ) { // we have access token
55+
$status = 'ok';
56+
$message = 'New access token save and ready for use.';
57+
58+
updateAccessToken( $accessTokenResponse['access_token'] );
59+
} else { // no access token
60+
$status = 'fail';
61+
$message = 'Something went wrong trying to get an access token.';
62+
}
63+
64+
return array(
65+
'status' => $status,
66+
'message' => $message,
67+
'raw_response' => $accessTokenResponse
68+
);
69+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
include 'functions.php';
3+
4+
$accessToken = generateAccessToken();
5+
6+
echo '<pre>';
7+
print_r( $accessToken );
8+
die();

0 commit comments

Comments
 (0)