Post: Runescape API help ARRAY
05-09-2017, 11:14 PM #1
KLArcher
Little One
(adsbygoogle = window.adsbygoogle || []).push({}); Hello, I am trying to create a hiscores website although i am not quite good with arrays, but this is what i am trying to do once the user enters there username using the $_GET['player'] then it will pull the information from Runescapes API from which i want it to store in my database using PDO.*

Runescape API:*https://services.runescape.com/m=hiscore_oldschool/index_lite.ws?player=$player


I just need help with the parsing the arrays and inserting them into my database with some basic validation.*

if anymore information is required i can provide*

Thanks.
05-13-2017, 01:06 PM #2
Algebra
[move]mov eax, 69[/move]

Originally posted by KLArcher View Post
Hello, I am trying to create a hiscores website although i am not quite good with arrays, but this is what i am trying to do once the user enters there username using the $_GET['player'] then it will pull the information from Runescapes API from which i want it to store in my database using PDO.*

Runescape API:*https://services.runescape.com/m=hiscore_oldschool/index_lite.ws?player=$player


I just need help with the parsing the arrays and inserting them into my database with some basic validation.*

if anymore information is required i can provide*

Thanks.



The return value is gibberish and I don't know which value to store in the database but maybe you can work off this ?


    
<?php
if(isset($_POST['submit'])){
/*
try {
$pdo = new PDO('mysql:host=127.0.0.1;dbname=database;charset=utf8mb4', 'rootuser', 'rootpass'Winky Winky;
}
catch(PDOException $ex){
print 'Error ' . $ex->getMessage();
}

*/

$player = htmlspecialchars($_POST['player']);

$url = htmlspecialchars("https://services.runescape.com/m=hiscore_oldschool/index_lite.ws?player=$player");

$jurl = file_get_contents($url);

$json_data = json_encode($jurl, JSON_PRETTY_PRINT);

$result = json_decode($json_data);


/*

$stmt = $pdo->prepare("INSERT INTO `users` (id, player ,highscore) VALUES ('', '$player' ,'$highscore'Winky Winky");
$stmt = $pdo->execute(array(
':player' => $player,
':highscore' => $highscore
));

Add error handling

*/

}
?>
<html>
<form method="post" action="test.php">
<input type="text" name="player" placeholder="Player Name" required />
<input type="submit" name="submit" value="Search" />
</form>
<div>

<?php
echo '<pre>';

echo print_r(stripcslashes($result));

echo '</pre>';
?>

</div>
</html>

Last edited by Algebra ; 05-13-2017 at 01:12 PM.
05-13-2017, 05:08 PM #3
Tustin
Balls of Steel
Like Algebra said, I don't really know what value is what on that API response, but it definitely isn't an array (at least not when I tried to pull the hiscores of the user "bob"). If you know what each value is, then you probably want to use explode because it looks like each value is delimited by a comma.

Example:
    
<?php
$contents = file_get_contents("https://services.runescape.com/m=hiscore_oldschool/index_lite.ws?player=$player");
$contents = explode(",", $contents);
var_dump($contents):


You'll have to manually give each value in the array a key because like I said, that API response doesn't seem to give anything but some random numbers. But then you can use Algebra's code for inserting whatever values you want to store into your database.

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo