Skip to content
TORNLIFE More

API Support

Started by IceBlueFire [776] Officer on in API Development.

9 replies · 301 views · thread synced · 6 days ago · View on torn.com
About this thread

Posts archived: 10 / 10 posts (100%) · the total is Torn's reply count + the opening post at the last fetch

Counted by TornLife from the archived posts.

Archived posts
10
Discussion span
→
People posting
5
Likes on archived posts
6
Posts by staff, officers and moderators
2
Authority score
77 / 100
Historical score
44 / 100
Story score
36 / 100
Engagement score
57 / 100
IceBlueFire [776] Officer Officer

Hey Guys, since everyone wants to learn how to build and use the API, i figured i'd create a collective question/answer thread. My goal is for developers to help people by giving code samples or explanations of how to make something work. I know we all have big dreams of what we want to accomplish, so let's put our heads together and make some truly awesome things!

Couple things to note:
1) Please include which language you're coding in. PHP, Python, mSL, whatever it may be.
2) If you're having issues with code, make sure to include the code you're using, as well as what you're trying to accomplish.
3) Never include your API key in the post.

Good luck guys! I'll continue to be active here and in the sub-forum as much as i can to help you guys along.

A basic example of how to use the API:
<html> <head> <title>Simple Form and API Tutorial</title> </head> <body> <form method="POST" action="<?php echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']; ?>"> <p>ID: <input type="text" id="user_id" name="user_id"></p> <p><input type="submit" name="Submit" value="Submit"></p> </form> <?php if(isset($_POST['user_id'])) { $apikey = ''; // Your API key $user_id = $_POST['user_id']; $url = "http://api.torn.com/user/$user_id?selections=profile&key=$apikey"; // URL to get the user's profile contents $json = file_get_contents($url); // Read the JSON response from the server $data = json_decode($json, true); // Convert JSON into a PHP array } ?> <pre> <?php print_r($data); ?> </pre> Name: <?php echo $data['name']; ?> </body></html>
Meme_Raven [1891116]

Working in both PHP and Python, Python code will come later but here's some semi-convenient stuff from PHP

PHP - taken from my profile page:
To start:

$apiKey ="YOUR_API";
$apibase = "http://api.torn.com/user/?selections=profile,basic,icons,networth,bazaar,display&key=".$apiKey;
$response = file_get_contents($apibase);
$outputPublic = json_decode($response, true);

Recursive search of your bazaar with items Qty and price:
<?php foreach($outputPublic['bazaar'] as $key=>$val){ echo ($val['quantity']."x ".$val['name']." @ $". $val['price'].", (M: $".$val['market_price'].")<br>");}?>" href="http://www.torn.com/bazaar.php?userID=<?php echo($outputPublic["player_id"].'">'.$bazaarCounted. ' items in Bazaar</a>');?>

Etc variables:

$uUsername = $outputPublic["name"];
$uLevel = $outputPublic["level"];
$uAge = $outputPublic["age"];
$uRank = $outputPublic["rank"];
$uStatus = $outputPublic["status"];
$uProperty = $outputPublic["property"];
$uAwards = $outputPublic["awards"];
$uFreinds = $outputPublic["friends"];
$uBlacklist = $outputPublic["enemies"];
$uMail = "https://www.torn.com/messages.php#/p=compose&XID=".$outputPublic["player_id"];
$uMarried = $outputPublic["married"];
ManuWinters [784168]

uhms okay, I asked bc if it runs with django, seems logic to me to run in Ruby Rails... (easier to me to program on ruby.... but I will not reject to learn another language)
IceBlueFire [776] Officer Officer

I'm not familiar with Ruby at all, but if it has a way to interpret JSON, then you'll be fine to build w/e you want in Ruby. If it doesn't have a way to interpret JSON, then you're going to have to find someone who did the work of building a library for you, or make you're own. Either way, JSON is pretty universal and can be used by most programming languages in one way or another.