25 replies · 739 views · thread synced · 7 days ago
· View on torn.com
About this thread
Posts archived:26 / 26 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
26
Discussion span
→
Authority score
54 / 100
Historical score
23 / 100
Story score
39 / 100
Engagement score
69 / 100
People posting, likes and official posts are not counted for this thread yet: on threads longer than one page they come from a periodic pass over the archive, which has not covered it.
Just a small tutorial on how you could use Torn's new API system.Firstly you need too get a web host or if your just starting out, you can use WAMP, its a localhost php server.
Step 1: Once you have your chosen type of server up and running you want too go ahead and start by creating a file called "index.php"
Step 2: Once this file has been created you want too start by adding a HTML base. "<html> <head> <title>AAW's API Tutorial</title> </head> <body>"
This is were the php code will be put!!!
</body></html>"
Step 3: add the following code too your HTML base "<?php $json = file_get_contents("API LINK HERE"); $obj = json_decode($json); ?>"
Step 4: Once you've got all that setup it should look a little like this! "<html> <head> <title>Torn Statistics</title> </head> <body> <?php $json = file_get_contents("API LINK HERE"); $obj = json_decode($json); ?> </body> </html>"
Step 5: If you would like too grab a value from the API simply just add below $obj = json_decode($json); echo "Example" . $obj->timestamp; This above will display the current timestamp within Torn.
var API_KEY = "mykey";$.get("http://api.torn.com/user/?key="+API_KEY, function (data) { alert(data.player_id + " is my ID");}); Second up the non jQuery way:
This way may look way more complicated but realistically this one has a lot more purely because I spent a little time making it more tailored to the torn API.
var API_KEY = "mykey";function getResult (obj) { var selections = (obj.hasOwnProperty("selections") ? obj.selections : null); var route = obj.route; var entity = obj.entity; var success = obj.success; var || function (e) { console.error(e); }; if (typeof(selections) != "array" && selections !== null) { selections = [selections]; } if (typeof(entity) == "array") { entity = entity.join(","); } var req = new XMLHttpRequest(); req.open("GET","http://api.torn.com/"+route+"/"+entity+"?key="+API_KEY+(selections != null ? "&selections="+selections.join(",") : "")) req.send(); console.log("Sending request"); req.onerror = onerror; req.onreadystatechange = function () { if (req.readyState == 4) { if (req.status != 200) { onerror(req.statusText); return; } try { var data = JSON.parse(req.responseText); if (data.hasOwnProperty("error")) { onerror(data.error); return; } success(data); } catch (e) { onerror(e); } } }}getResult({ route: "user", success: function (profileInfo) { var myID = profileInfo.player_id; alert(myID); }});
Is there something wrong with my php? I get the following error with aaw and toms example:
Warning: file_get_contents(http://api.torn.com/torn/USERIDHERE.json?selections=stats&key=MYKEYHERE): failed to open stream: Connection refused in /srv/disk6/2003124/www/mydomainhere.org/metzen.php on line 7 Example
That error means that you are trying to convert a class into a string (which it cannot do). I'd recommend doing a var_dump($obj) to see what exactly you are receiving.
In this case you are accessing the energy object but you've not gone far enough down to get to something that can be converted into a string.
Eg. Heres the energy object for myself:
energy: { current: 105, maximum: "150", increment: 5, interval: 600, ticktime: 475, fulltime: 5275} In PHP when using json_decode, it goes through the JSON object and converts into object, array, integer, float or string.. A very quick explanation In JSON {} means "this is an object", [] means this is an array, "anything in quotes" is a string and a number is a number. So for example to get the current energy you have you would use:
how would i get the $json =file_get_contents("http://api.torn.com/user/?selections=Bars,money&key=J2fIt"); to get the key info from a form?? sorry, i'm a noob when it comes to working with forms and totally new to api usage