PDA

View Full Version : Grabbing value from an API (existing code)


Skyrider
05-28-2015, 08:18 AM
I know I'm asking a lot lately, but I'm still learning. Currently, I'm using this code (in attempts) to get the proper value: (inside a *.PHP file)


$url = sprintf("http://api.steampowered.com/ISteamUser/GetPlayerBans/v1/?key=%s&steamid=%s",
$api_key,
$steam_id);

$curl_time = microtime(1);
// Needs curl to actually be able to pull data from steam api because of the special response-behaviour.
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
@curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_MAXREDIRS, 5 );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $vbulletin->options['stc_curl_timeout'] ); #rc1: helps with page delay if steam is down
$tradeban = curl_exec( $ch );
$http_response_header = curl_getinfo( $ch );
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
curl_close($ch);

$tradeban = json_decode($tradeban, 1);


With:
$steam_info['EconomyBan'] = is_array($tradeban) && isset($tradeban['players'], $tradeban['players']['EconomyBan']) ? $tradeban['players']['EconomyBan'] : -1;

To get the value.. however, once I check whatever it grabs, it shows this:

"EconomyBan";i:-1;}

Here's an example of an API result when going to: http://api.steampowered.com/ISteamUser/GetPlayerBans/v1/?key=xx&steamid=xx (<- key removed for obvious reasons)

{
"players": [
{
"SteamId": "76561198024651743",
"CommunityBanned": false,
"VACBanned": false,
"NumberOfVACBans": 0,
"DaysSinceLastBan": 0,
"NumberOfGameBans": 0,
"EconomyBan": "banned"
}
]

}

I need to get the value of the EconomyBan which in this case is "banned". Any help would be appreciated, been trying for hours now. I assume the -1 only works for numbers rather than letters?

Forgot to mention. EconomyBan is already filled in in the steam options plugin (see attachment), so the EconomyBan field should be called. You can ignore the rest of the text on the attachment provided below, the author already has created this steam_level check for me:

$url = sprintf("http://api.steampowered.com/IPlayerService/GetSteamLevel/v0001/?key=%s&steamid=%s",
$api_key,
$steam_id);

$curl_time = microtime(1);
// Needs curl to actually be able to pull data from steam api because of the special response-behaviour.
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
@curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_MAXREDIRS, 5 );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $vbulletin->options['stc_curl_timeout'] ); #rc1: helps with page delay if steam is down
$steamlevel = curl_exec( $ch );
$http_response_header = curl_getinfo( $ch );
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
curl_close($ch);

$steamlevel = json_decode($steamlevel, 1);

with:

$steam_info['player_level'] = is_array($steamlevel) && isset($steamlevel['response'], $steamlevel['response']['player_level']) ? $steamlevel['response']['player_level'] : -1;

Which is working perfectly. I'm merely trying to duplicate the code, and grab the information from a different API instead.

Dave
05-28-2015, 09:36 AM
What do you get when you var_dump or print_r the json_decoded string?

Skyrider
05-28-2015, 09:47 AM
What do you get when you var_dump or print_r the json_decoded string?
Mind sharing an example? I'm aware of those codes, just unsure if I should put it inside the entire:

$tradeban = json_decode($tradeban, 1);

Would this work?

$tradeban = var_dump(json_decode($tradeban, 1));

Still a newbie at this, learning on the go.

Forgot to mention, all these changes are within a *.PHP file, rather than a plugin. Might be good to know as well where the var is being dumped. In debug mode, under messages? Tried that with above code (if thats correct), but shows no new info in debug mode -> messages.

cellarius
05-28-2015, 10:17 AM
That's basic PHP, it has nothing to do with vb's debug system.

See: http://php.net/manual/en/function.var-dump.php

Your second code would not work. var_dump or print_r print the content of the variable or array directly to the screen. The output can't be saved to a variable. It's just
$tradeban = json_decode($tradeban, 1);
var_dump($tradeban);

Skyrider
05-28-2015, 12:09 PM
Here's the entire PHP file just for the sake of it:

http://pastebin.com/8NeTPHZi

See line 128 to 150 and 207

I've created a PHP code specially designed to check 2 variables:

require_once(DIR . '/includes/functions_steamconnect.php');

// Steam Level Information API
$steam_id = get_user_steamid($stc_userinfo);
$steam_info = fetch_steam_info($steam_id, $vbulletin->options['stc_apikey']);

var_dump($steam_info['player_level']);
var_dump($steam_info['tradeban']);

This is the result at the top of the page:

int(36) NULL

The first, showing my steam level, 36. Which is correct. The steam_level API always has worked.. so I copy/pasted the code to see if I can get it to work for tradeban API as well. However, the second tradeban var_dump shows NULL, thus no information was grabbed.

Using:

var_dump($steam_info['EconomyBan'])
Result in: int(-1)

It 'might' be possible its giving NULL results as the results are being cached within files (for each user) that is defined in another PHP file:

http://pastebin.com/xzCfTjnF

^- Striped above, I've edited this post as I've posted the var_dump results on a separated plugin calling the right information. Left the edit intact, just in case.

---EDIT:

Does this have something to do with it? Seeing -1 is mentioned here, as a matching value.

line 184 to 200 (first pastebin)

$values = $match[1];
foreach ($values AS $val) {
$match = array();
preg_match("/\"(.+)\": ([^,]*),?/", $val, $match);
if (is_array($match) && count($match) == 3) {

$name = $match[1];
$value = trim($match[2]);
if (!in_array($name, $whitelist)) continue; // Not a setting we're looking for. skip..

// Strip quotes and slashes
if ($value{0} == '"' && $value{strlen($value)-1} == '"')
$value = stripslashes(substr($value, 1, -1));

$steam_info[$name] = $value;
}
}

I'm unsure, still checking into the code why I'm not getting the 'banned' value back.

Really would like to know why.

----SOLVED IT-----

Wanted to share, got it to work with some help.

First, URL near the end had to be "steamids" rather than "steamid"

Second:
$steam_info['EconomyBan'] = is_array($tradeban) && isset($tradeban['players'], $tradeban['players']['EconomyBan']) ? $tradeban['players']['EconomyBan'] : -1;
Had to be:
$steam_info['EconomyBan'] = is_array($tradeban) && isset($tradeban['players'], $tradeban['players'][0]['EconomyBan']) ? $tradeban['players'][0]['EconomyBan'] : -1;

I appreciate the replies however! :D