Jump to content
Larry Ullman's Book Forums

How To Grab Data From An Api And Store It As A Variable?


Recommended Posts

Hey Larry,

 

I've just started with PHP and have tried learning with a few books (Missing Manual, Dummies, etc) and yours was by far the easiest/ straight forward to understand.

 

That said I would like to practise my PHP skills by starting this project:

 

Basically it's a calculator for a game called RuneScape, and what it will do is calculate the chances of you winning against a match to another opponent, through their skill levels such as Attack, Strength and Defence. The game has their own API that allows you to search any player's skill levels on the High Scores here:

 

http://services.runescape.com/m=hiscore_oldschool/index_lite.ws?player=magmoz

(Replace "magmoz" with any player").

 

Basically I want users to be able to enter their username, hit a button and a variable will store the data. Then enter their opponent's username, hit a button and the variable will store their data. The script will then use both data to calculate the chances of winning.

 

The issue is I don't understand how to grab the data from the API, onto my script. I also hope to calculate the results without reloading the page. An example of this is here:

 

http://www.tip.it/runescape/pages/view/combat_calc.htm

(Just type "torzilla" and hit enter or click anywhere on the screen and it will automatically store the data and calculate the results. Change any of the skill levels, and it will calculate it once again).

 

This is a project I am dedicated to learn, please help me with this, it'd be a lifesaver!

 

Thanks!

Link to comment
Share on other sites

Andy, you can use PHP cURL to make a request to another page and return the data to your script. From there, you can do whatever you want with the data.

 

Here's a simple request that should run fine in a standalone script:

 

<?php
  
  $api_request_url = 'http://services.runescape.com/m=hiscore_oldschool/index_lite.ws?player=';
  
  $player_name = 'magmoz';
  
  $ch = curl_init($api_request_url . $player_name);
  
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  
  $api_str_data = curl_exec($ch);
  
  curl_close($ch);
  
  echo $api_str_data;
  
  echo '<pre>';
  
  print_r(explode("\n", $api_str_data));
  
  echo '</pre>';
 
I think you should be able to modify this as need be to get what you want.
Let us know if you need anymore help.
Thanks.
  • Upvote 2
Link to comment
Share on other sites

Hmm, so I'm a little bit confused.

 

With the code you provided me, I placed at the very top of the script. What I want to do is allow users to enter their username and have the script calculate instantly once the user hits enter or clicks elsewhere the text box. I'm not very familiar with the other functions but what variables do I use to define the script?

 

For instance how would I use the Defence Level of a player, with something like:

$def = #defence level of player, grabbed from the API.

How would I be able to grab let's say, the 10th number separated by commas?

 

Basically I need to know the values of the levels in order to add formulas to them and make a calculator. I'm sure the script knows, but I don't!

 

Thanks!

Link to comment
Share on other sites

Well, the script I provided was a very basic starter script that you can build off of.

All the script does is grab a string of data from the API based on the entered user name and then displayed that string.

To make the string data more readable, I also split the string on newlines, and displayed the resulting array as well.

 

You will probably have to reference the API documentation on the game site, but somewhere, there should be an explanation of what all the values in the API response string mean.

 

Once you know what value(s) you want, simply use the explode function (or whatever) to split the string on newlines (and maybe commas too) and then grab the value you need from the resulting array.

 

Does that make sense?

Link to comment
Share on other sites

Well, the script I provided was a very basic starter script that you can build off of.

All the script does is grab a string of data from the API based on the entered user name and then displayed that string.

To make the string data more readable, I also split the string on newlines, and displayed the resulting array as well.

 

You will probably have to reference the API documentation on the game site, but somewhere, there should be an explanation of what all the values in the API response string mean.

 

Once you know what value(s) you want, simply use the explode function (or whatever) to split the string on newlines (and maybe commas too) and then grab the value you need from the resulting array.

 

Does that make sense?

 

I'm sorry but I still don't understand. Could you give me an example which does the following:

 

  1. User enters username
  2. PHP will automatically search the API for the skill levels for that username
  3. How will I reference that number exactly? For instance, the 10th number

Thanks!

Link to comment
Share on other sites

In order for the user to enter a user name, all you have to do is provide an HTML form to the user with a standard text input. Upon the user posting the form, you can use the $_POST superglobal to grab the value and plug that into the URL for the API call.

 

As for the API, I can't really help you unless you can get me more information about the API.

Please find the documentation for the API, and I will try to help.

Thanks.

Link to comment
Share on other sites

There's no documentation for the API. I'm still a little bit confused, could you explain to me in layman terms step by step?

 

I really apologize for this but I have trouble understanding how to store the numbers in a variable.

Link to comment
Share on other sites

You NEED some sort of API documentation to be able to use it. For us, these numbers means nothing. You must find out what the numbers represent as stats, and then maybe build an array with those values.

$player_name = $_POST['player_name']; // Or use GET
$api_request_url = 'http://services.runescape.com/m=hiscore_oldschool/index_lite.ws?player=';  
  
$ch = curl_init($api_request_url . $player_name);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$api_str_data = curl_exec($ch);
curl_close($ch);

$stats = explode(",", $api_str_data); // Split data on String

// ...Assign directly to new variables
$strength = $stats[0];
$health = $stats[1];

// ...or build associative array with named key => value pairts
$data = array(
   "strength" => $stats[0];
   "health" => $stats[1];
);

I have no idea what the values actually represent, so they are completely random.

 

Also, I found this for you. It seems to be some other part of the API. That's the kind of documentation you need to work with the API: http://services.runescape.com/m=rswiki/en/Grand_Exchange_APIs

 

Edit: Also found this PHP API for you: https://github.com/ArminSupuk/Runescape-API

Link to comment
Share on other sites

 Share

×
×
  • Create New...