Jump to content

Filter


absorr

Recommended Posts

Ok so I'm using this API and whenever I use a method, it gives me more than I want. For example, one function gives me: array(3) { ["result"]=> string(7) "success" ["source"]=> string(14) "getPlayerLimit" ["success"]=> int(20)} when all i want is the number at the bottom by intHow do I make it so that it will only show that number?

Link to comment
Share on other sites

You said that this is returned from a function? Call the function and store the result in a variable first:

$result = my_function();echo $result['success'];

Link to comment
Share on other sites

You said that this is returned from a function? Call the function and store the result in a variable first:
$result = my_function();echo $result['success'];

I realize i may have made an aerror than. i did not mean a PHP function. I meant a method for the API, which in the above example is getPlayerLimit
Link to comment
Share on other sites

You get the array however you want to. Once you have the array, instead of using print_r, you can access the value using $array['success']. If you didn't name the array "$array", then substitute whatever name you used. It's the same array you're using with print_r.

Link to comment
Share on other sites

You said that this is returned from a function? Call the function and store the result in a variable first:
$result = my_function();echo $result['success'];

Yeah that did not work. I'm sorry if my error confused you but I didn't mean that I got it through function. Here is JSONAPI.php which is used by it
<?php/*** A PHP class for access Minecraft servers that have Bukkit with the {@link http://github.com/alecgorge/JSONAPI JSONAPI} plugin installed.** This class handles everything from key creation to URL creation to actually returning the decoded JSON as an associative array.** @author Alec Gorge <alecgorge@gmail.com>* @version Alpha 5* @link http://github.com/alecgorge/JSONAPI* @package JSONAPI* @since Alpha 5*/class JSONAPI {public $host;public $port;public $salt;public $username;public $password;private $urlFormats = array(  "call" => "http://%s:%s/api/call?method=%s&args=%s&key=%s",  "callMultiple" => "http://%s:%s/api/call-multiple?method=%s&args=%s&key=%s");/**  * Creates a new JSONAPI instance.  */public function __construct ($host, $port, $uname, $pword, $salt) {  $this->host = $host;  $this->port = $port;  $this->username = $uname;  $this->password = $pword;  $this->salt = $salt;   if(!extension_loaded("cURL")) {   throw new Exception("JSONAPI requires cURL extension in order to work.");  }}/**  * Generates the proper SHA256 based key from the given method suitable for use as the key GET parameter in a JSONAPI API call.  *  * @param string $method The name of the JSONAPI API method to generate the key for.  * @return string The SHA256 key suitable for use as the key GET parameter in a JSONAPI API call.  */public function createKey($method) {  if(is_array($method)) {   $method = json_encode($method);  }  return hash('sha256', $this->username . $method . $this->password . $this->salt);}/**  * Generates the proper URL for a standard API call the given method and arguments.  *  * @param string $method The name of the JSONAPI API method to generate the URL for.  * @param array $args An array of arguments that are to be passed in the URL.  * @return string A proper standard JSONAPI API call URL. Example: "http://localhost:20059/api/call?method=methodName&args=jsonEncodedArgsArray&key=validKey".  */public function makeURL($method, array $args) {  return sprintf($this->urlFormats["call"], $this->host, $this->port, rawurlencode($method), rawurlencode(json_encode($args)), $this->createKey($method));}/**  * Generates the proper URL for a multiple API call the given method and arguments.  *  * @param array $methods An array of strings, where each string is the name of the JSONAPI API method to generate the URL for.  * @param array $args An array of arrays, where each array contains the arguments that are to be passed in the URL.  * @return string A proper multiple JSONAPI API call URL. Example: "http://localhost:20059/api/call-multiple?method=[methodName,methodName2]&args=jsonEncodedArrayOfArgsArrays&key=validKey".  */public function makeURLMultiple(array $methods, array $args) {  return sprintf($this->urlFormats["callMultiple"], $this->host, $this->port, rawurlencode(json_encode($methods)), rawurlencode(json_encode($args)), $this->createKey($methods));}/**  * Calls the single given JSONAPI API method with the given args.  *  * @param string $method The name of the JSONAPI API method to call.  * @param array $args An array of arguments that are to be passed.  * @return array An associative array representing the JSON that was returned.  */public function call($method, array $args = array()) {  if(is_array($method)) {   return $this->callMultiple($method, $args);  }   $url = $this->makeURL($method, $args);  return json_decode($this->curl($url), true);}private function curl($url) {  $c = curl_init($url);  curl_setopt($c, CURLOPT_PORT, $this->port);  curl_setopt($c, CURLOPT_HEADER, false);  curl_setopt($c, CURLOPT_RETURNTRANSFER, true);  curl_setopt($c, CURLOPT_TIMEOUT, 10);   $result = curl_exec($c);  curl_close($c);  return $result;}/**  * Calls the given JSONAPI API methods with the given args.  *  * @param array $methods An array strings, where each string is the name of a JSONAPI API method to call.  * @param array $args An array of arrays of arguments that are to be passed.  * @throws Exception When the length of the $methods array and the $args array are different, an exception is thrown.  * @return array An array of associative arrays representing the JSON that was returned.  */public function callMultiple(array $methods, array $args = array()) {  if(count($methods) !== count($args)) {   throw new Exception("The length of the arrays \$methods and \$args are different! You need an array of arguments for each method!");  }   $url = $this->makeURLMultiple($methods, $args);  return json_decode($this->curl($url), true);}}

and here is broadcastWithName.php with the login information censored with ***. This is what takes the input from an HTML text field and sends it to JSONAPI.php (the one above). This does work but again the reason I'm posting is to try to filter out the results it gives me to just one part.

<?php require('JSONAPI.php'); // get this file at: [url="https://github.com/alecgorge/jsonapi/raw/master/sdk/php/JSONAPI.php"]https://github.com/a...php/JSONAPI.php[/url] $api = new JSONAPI("***", ***, "***", "***", "***"); var_dump($api->call("broadcastWithName", array($_POST["msg"], $_POST["name"])));?>

Link to comment
Share on other sites

Just like how Ingolme showed, except instead of "my_function" you use the function call that you're actually using. $result = $api->call("broadcastWithName", array($_POST["msg"], $_POST["name"])); And it helps because then you can actually use the result instead of only printing the data structure on the page.

Link to comment
Share on other sites

Just like how Ingolme showed, except instead of "my_function" you use the function call that you're actually using. $result = $api->call("broadcastWithName", array($_POST["msg"], $_POST["name"])); And it helps because then you can actually use the result instead of only printing the data structure on the page.
THANK YOU! That worked perfectly! You 2 are awesome! This has unlocked a lot for me in the web application I am making with this.
Link to comment
Share on other sites

ummmm when I do it with getPlayer I just get the word Array. When i do non-filtering, I get this: array(3) { ["result"]=> string(7) "success" ["source"]=> string(9) "getPlayer" ["success"]=> array(18) { ["worldInfo"]=> array(7) { ["remainingWeatherTicks"]=> int(131354) ["hasStorm"]=> bool(false) ["time"]=> int(13278) ["environment"]=> string(6) "normal" ["isThundering"]=> bool(false) ["name"]=> string(9) "adventure" ["fullTime"]=> int(4957278) } ["op"]=> bool(true) ["location"]=> array(5) { ["yaw"]=> float(91.047104) ["pitch"]=> float(22.049994) ["z"]=> float(1152.71875) ["y"]=> float(64) ["x"]=> float(862.90625) } ["exhaustion"]=> float(3.6588554) ["sleeping"]=> bool(false) ["health"]=> int(16) ["ip"]=> string(18) "/192.168.1.1:52644" ["gameMode"]=> int(0) ["inVehicle"]=> bool(false) ["level"]=> int(2) ["inventory"]=> array(3) { ["hand"]=> array(4) { ["enchantments"]=> array(0) { } ["amount"]=> int(1) ["durability"]=> int(1) ["type"]=> int(267) } ["inventory"]=> array(36) { [0]=> array(4) { ["enchantments"]=> array(0) { } ["amount"]=> int(1) ["durability"]=> int(1) ["type"]=> int(267) } [1]=> array(4) { ["enchantments"]=> array(0) { } ["amount"]=> int(1) ["durability"]=> int(0) ["type"]=> int(344) } [2]=> array(4) { ["enchantments"]=> array(0) { } ["amount"]=> int(1) ["durability"]=> int(31) ["type"]=> int(346) } [3]=> array(4) { ["enchantments"]=> array(0) { } ["amount"]=> int(19) ["durability"]=> int(0) ["type"]=> int(297) } [4]=> array(4) { ["enchantments"]=> array(0) { } ["amount"]=> int(64) ["durability"]=> int(0) ["type"]=> int(295) } [5]=> array(4) { ["enchantments"]=> array(0) { } ["amount"]=> int(21) ["durability"]=> int(0) ["type"]=> int(295) } [6]=> NULL [7]=> NULL [8]=> NULL [9]=> NULL [10]=> NULL [11]=> NULL [12]=> NULL [13]=> NULL [14]=> NULL [15]=> NULL [16]=> NULL [17]=> NULL [18]=> NULL [19]=> NULL [20]=> NULL [21]=> NULL [22]=> NULL [23]=> NULL [24]=> NULL [25]=> NULL [26]=> NULL [27]=> NULL [28]=> NULL [29]=> NULL [30]=> NULL [31]=> NULL [32]=> NULL [33]=> NULL [34]=> NULL [35]=> NULL } ["armor"]=> array(4) { ["helmet"]=> array(4) { ["enchantments"]=> array(0) { } ["amount"]=> int(0) ["durability"]=> int(-1) ["type"]=> int(0) } ["boots"]=> array(4) { ["enchantments"]=> array(0) { } ["amount"]=> int(0) ["durability"]=> int(-1) ["type"]=> int(0) } ["leggings"]=> array(4) { ["enchantments"]=> array(0) { } ["amount"]=> int(0) ["durability"]=> int(-1) ["type"]=> int(0) } ["chestplate"]=> array(4) { ["enchantments"]=> array(0) { } ["amount"]=> int(0) ["durability"]=> int(-1) ["type"]=> int(0) } } } ["name"]=> string(6) "absorr" ["foodLevel"]=> int(8) ["experience"]=> int(25) ["itemInHand"]=> array(4) { ["enchantments"]=> array(0) { } ["amount"]=> int(1) ["durability"]=> int(1) ["type"]=> int(267) } ["sneaking"]=> bool(false) ["world"]=> int(3) ["sprinting"]=> bool(false) } } What I want is to have each bit of information organized. If you look through there are things like the world the player is in, the things in their inventory, all sorts of stuff, but how do I make each individual part show? I plan to make it so that like the health will be represented by a different picture for each amount using if and ifelse, show a picture of the players inventory based on the item ids, etc. All I need is the individual parts that I actually want

Link to comment
Share on other sites

If you echo array it will always show you 'Array' Use this to see how is your array is structured.

echo "<pre>".print_r($getPlayer,TRUE)."</pre>";

After that you can access its elemnts by sepcifying index name. There could be many dimension of your array. How to access elements of two dimensional or multi dimensional array you can check here http://w3schools.com/php/php_array.asp

Link to comment
Share on other sites

You can use foreach() to go through each element and use its data as you like. Here's an example of putting the data into a table:

echo '<table>';echo '<tbody>';foreach($array as $property=>$value) {  echo '<tr>';  echo "<td>$property</td><td>$value</td>";  echo '</tr>';}echo '</tbody>';echo '<thead><tr><th>Property</th><th>Value</th></tr></thead>';echo '</table>';

Link to comment
Share on other sites

You can use foreach() to go through each element and use its data as you like. Here's an example of putting the data into a table:
echo '<table>';echo '<tbody>';foreach($array as $property=>$value) {  echo '<tr>';  echo "<td>$property</td><td>$value</td>";  echo '</tr>';}echo '</tbody>';echo '<thead><tr><th>Property</th><th>Value</th></tr></thead>';echo '</table>';

I put that in and it only gave me the headers
Link to comment
Share on other sites

If you echo array it will always show you 'Array' Use this to see how is your array is structured.
echo "<pre>".print_r($getPlayer,TRUE)."</pre>";

After that you can access its elemnts by sepcifying index name. There could be many dimension of your array. How to access elements of two dimensional or multi dimensional array you can check here http://w3schools.com/php/php_array.asp

Didn't work either
Link to comment
Share on other sites

That means you did something wrong. What does your code look like?
Basically I pasted it right in
<?phprequire('JSONAPI.php'); // get this file at: https://github.com/alecgorge/jsonapi/raw/master/sdk/php/JSONAPI.php$api = new JSONAPI(CENSORED, CENSORED, CENSORED, CENSORED, CENSORED);var_dump($api->call("getPlayer", array($_POST["name"]))); echo '<table>';echo '<tbody>';foreach($array as $property=>$value) {  echo '<tr>';  echo "<td>$property</td><td>$value</td>";  echo '</tr>';}echo '</tbody>';echo '<thead><tr><th>Property</th><th>Value</th></tr></thead>';echo '</table>';?>

Link to comment
Share on other sites

You missed the point. You have to store the result of the function call into a variable like we already showed you earlier:

$array = $api->call("getPlayer", array($_POST["name"]))

Link to comment
Share on other sites

You missed the point. You have to store the result of the function call into a variable like we already showed you earlier:
$array = $api->call("getPlayer", array($_POST["name"]))

Sorry, I had it changed to the var dump because of how it didn't work and I forgot to change back when testing your answer. it gave me the table but it didn't show the results. Here's the table: Property Valueresult successsource getLatestConsoleLogssuccess Array and here is getLatestConsoleLogs.php
<?phprequire('JSONAPI.php'); // get this file at: https://github.com/alecgorge/jsonapi/raw/master/sdk/php/JSONAPI.php$api = new JSONAPI([color=#000000][size=2]CENSORED[/size][/color][color=#666600][size=2],[/size][/color][color=#000000][size=2] CENSORED[/size][/color][color=#666600][size=2],[/size][/color][color=#000000][size=2] CENSORED[/size][/color][color=#666600][size=2],[/size][/color][color=#000000][size=2] CENSORED[/size][/color][color=#666600][size=2],[/size][/color][color=#000000][size=2] CENSORED[/size][/color]);$array = $api->call("getLatestConsoleLogs");echo '<table>';echo '<tbody>';foreach($array as $property=>$value) {  echo '<tr>';  echo "<td>$property</td><td>$value</td>";  echo '</tr>';}echo '</tbody>';echo '<thead><tr><th>Property</th><th>Value</th></tr></thead>';echo '</table>';?>

and birbal's didn't give me anything

Link to comment
Share on other sites

It looks like you have nested arrays. When you find an array as one of the property values, you have to loop through it. A recursive function will work if you have an undetermined amount of arrays. It's a bit more complicated to teach so you would first need to properly understand what the current code is doing before you continue.

Link to comment
Share on other sites

I'm not the one to ask if you want a pre-made solution to your problem. My job here is to help people learn, and working with an undefined amount of nested arrays is probably too advanced for your current knowledge. For now we can consider that you only have a second level in your array. Inside your loop, you will have a condition: if the current element is an array then print out a new table, and loop through each element of this array, printing the name-value pairs in table cells. The result would be nested tables, but they are semantically correct. You could choose to use a different element, but for this purpose it doesn't matter.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...