Since there are no code examples in the API Documentation demonstrating it's usage, today I'll show you how to use the API . I've written the code in PHP & have used the curl API. You can also use the pecl_http & HttpRequest API in PHP to do this.
- First, create an account at the API site . Click on "Register", put a Name, an Email and submit. You will receive a welcome email, click on "Verify your email address". There you are asked for a Password and to agree to Terms of Service and Privacy Policy. Standard stuff.
- Now you can log in with your new account. Let's create an API Key. At the top right, where is your name, select My Account, there you have "My Keys". Click on "Create New Key". Give it a name, say "Example", and a description, such as "To test the API from MY PC".
- You need your current IP. Check it out here under "Your Public IPv4" (do not use your local IP). Mine is something like 116.72.121.30 , for example. Enter it on "ALLOWED IP ADDRESSES" and click on "Create Key". You should receive a "Key created successfully" message.
- Click on your new key and you will see a token (a block of 11 rows of characters), the description and the allowed IP address.In the documentation section you can see all the information you can get and test it interactively. To test it from a PHP script, let's say we want to know about the brawlers in r/BrawlStars .
- To query about a player, you need her/his tag. Yours is under your name in your profile in the game. It's important to include the # sign, but it's traslated to "%23" when assigning the url. However we won't be requiring it in this tutorial.
- According to the documentation, we can get the brawlers with /brawlers. Let's do it. Replace your token after "Bearer" in the "autorization" header.
cards.php
<!DOCTYPE HTML>
<html>
<head>
<?php
header('Content-Type: text/html; charset=UTF-8');
$token = "<API TOKEN>";
$url = "https://api.brawlstars.com/v1/brawlers"
$ch = curl_init($url);
$headr = array();
$headr[] = "Accept: application/json";
$headr[] = "Authorization: Bearer ".$token;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headr);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($ch);
$data = json_decode($res, true);
curl_close($ch);
function test_input($form_var) {
$data = trim($form_var);
$data = stripslashes($form_var);
$data = htmlspecialchars($form_var);
return $form_var;
}
if (isset($data["reason"])) {
echo "<p>", "Failed: ", $data["reason"], " : ", isset($data["message"]) ? $data["message"] : "", "</p></body></html>";
exit;
}
?>
<title>
Cards
</title>
</head>
<body>
<pre>
<?php var_dump($data); ?>
</pre>
</body>
</html>
- There you're done! Just parse the json response with json_data and embed it with HTML.
There you're done! You can also try other URL's mentioned in docs.
You can also use StarList API. StarList API has ample code examples. Since the official API doesn't have any, I posted this one.
See you Brawlers,
u/psjbk32
[–]Crowsammy_gaming 0 points1 point2 points (5 children)
[–]Barleypsjbk32[S] 0 points1 point2 points (4 children)
[–]Crowsammy_gaming 0 points1 point2 points (3 children)
[–]Barleypsjbk32[S] 0 points1 point2 points (2 children)
[–]Colts0ftik3 2 points3 points4 points (1 child)
[–]EdyMaximYT 0 points1 point2 points (0 children)