I am trying to integrate the API of a game via cURL, I tried with the code that you see below, however, gives me error
Notice: Trying to get property of non-object in
C:\xampp\htdocs\over\index.php on line 173
JSON is not "data" and I think that's what bothers me:
{"SoloKills": "494", "DamageDone": "758,071", "Eliminations": "1,911",}
$mode = "quick-play";
$allheroes = curl_init();
curl_setopt($allheroes, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($allheroes, CURLOPT_RETURNTRANSFER, true);
curl_setopt($allheroes, CURLOPT_FAILONERROR, true);
curl_setopt($allheroes, CURLOPT_URL,"https://api.lootbox.eu/{$platform}/{$country}/{$battletag}/quick-play/allHeroes/");
$result3 = curl_exec($allheroes);
curl_close($allheroes);
$stats3 = json_decode($result3, true);
foreach ($stats3 as $allhero)
{
$uccisioni = $allhero['Eliminations'];
echo $uccisioni;
}
You have to becarefull in encoding. It's affect to whole your codes
"DamageDone": "758,071" not
DamageDone": "758,071"
you've forgot Quote Marks!
You don't have key "ObjectiveKills" at this JSON
{"SoloKills": "494", "DamageDone": "758,071", "Eliminations": "1,911"}
But i think you can get "SoloKills", "DamageDone", "Eliminations" like:
foreach ($stats3 as $allhero) {
$uccisioni = $allhero['SoloKills'];
echo $uccisioni;
}
Update:
You don't need foreach here, I got
array(41) { ["SoloKills"]=> string(2) "55" ["ObjectiveKills"]=> string(3) "138" ...]}
You can get $stats3['Eliminations']
Related
I am trying to bring in some API data to a directory in wordpress.
The data I am trying to get is just crypto coin price, none of the other information but because its format is sort of nested (?) it doesnt seem to work.
{
"bitcoin": {
"usd": 16808.82
}
}
This is my code so far:
<?php
$handle = curl_init();
$url = get_post_meta($entity-\>post()-\>ID, '\_drts_field_004', true);
// Set the url
curl_setopt($handle, CURLOPT_URL, $url);
// Set the result output to be a string.
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
$output = curl_exec($handle);
curl_close($handle);
$data = json_decode($output);
echo $output;
var_dump($data);
The results are:
{
"bitcoin":{
"usd":16833.02
}
}
object(stdClass)#10399 (1) {
["bitcoin"]=> object(stdClass)#10492 (1) {
["usd"]=> float(16833.02)
}
}
In this example I am only after the 16833.02
I am trying to do this for lots of different coins, the "usd" will always be the same but the "bitcoin" will change when other coins.
How can I echo only the number?
I have tried lots of variations of echo but cannot get it? Is it possible to do something like:
echo $data['bitcoin']['usd'];
but rather than bitcoin use * ?
As in anything can be there?
You can access the usd value by decoding the JSON to an array instead of an object like this
$data = json_decode($output, true);
$usd = current($data)['usd'];
I'm using this little PHP function that I wrote to make calls to my master database:
$db_request = curl_init(DB_ROOT.'/action/register.php');
curl_setopt($db_request, CURLOPT_HEADER, 0);
curl_setopt($db_request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($db_request, CURLOPT_POSTFIELDS, http_build_query($variables));
$db_response = curl_exec($db_request);
curl_close ($db_request);
parse_str($db_response, $information);
On register.php I use the following to respond:
echo http_build_query(array(
'test1'=>'value1',
'test2'=>'value2',
'test3'=>'value3'
));
My problem comes when trying to retrieve the first index of any given response. I can use var_dump($information) and will receive array(3) { ["test1"]=> string(6) "value1" ["test2"]=> string(6) "value2" ["test3"]=> string(6) "value3" }. However, when I try to echo $information['test1'], I receive this: Notice: Undefined index: test1 in....
Echoing anything other than the first index doesn't give me this problem.
Does anyone have any thoughts?
parse_str return type is void
Update your code
parse_str($db_response, $information);
Hello guys i started to try use Steam API and i already done login... but i have a big problem to get items. I cant find solution what im doing wrong.
$GetClientSteamItemsUrl = " http://steamcommunity.com/profiles/" . $_SESSION['steam_steamid'] . "/inventory/json/730/2";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $GetClientSteamItemsUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 3);
$output = curl_exec($ch);
$ArrayInventory = json_decode($output, true);
foreach($ArrayInventory['rgDescriptions'] as $inventory)
{
echo $inventory;
}
On that link is my items that i want to write their name on my page.
Error: Warning: Invalid argument supplied for foreach() in /data/web/virtuals/112855/virtual/www/subdom/ayy/index.php
Can someone help me to select specific part of code?
Try this
$inventory = file_get_contents("http://steamcommunity.com/profiles/".$_SESSION['steamid']."/inventory/json/730/2");
$myarrayInv = json_decode($inventory, true);
foreach($myarrayInv['rgDescriptions'] as $item)
{
echo $item['name'];
}
if you want icon_url etc. do $item['icon_url'], hope this helps.
I've been following this tutorial on how to find my membership id.
This part is what I'm stuck on. I have a simple PHP file using that code with the API key and correct settings. Its giving me property of non object error. Here:
<?php
$apiKey = 'REMOVED FOR SECURITY';
$ch = curl_init();
‘https://www.bungie.net/platform/destiny/1/Stats/GetMembershipIdByDisplayName/GAMERTAG REMOVED FOR SECURITY');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, array('X-API-Key: ' . $apiKey));
$json = json_decode(curl_exec($ch));
echo $json->Response;
?>
The link inside the code would usually have a gamertag, like 'MajorNelson' or something like that. It gives some error when you go to it, but that link doesnt matter. When hosting the php file using XAMPP I get this error
Notice: Trying to get property of non-object in E:\xampp\htdocs\bungieapi.php on line 9
Line 9 is the echo line before the ?>.
Check for errors:
// ...
$json = curl_exec($ch);
if ($json === false) {
die('ERROR: ' . curl_error($ch));
}
$obj = json_decode($json);
if (isset($obj->Response)) {
echo $obj->Response;
} else {
echo 'ERROR: The "Response" property is not there';
}
I am using the bukkit JSONAPI and php JSONAPI.php to get the list of players on my minecraft server to my website. To get the count, I do this:
require('JSONAPI.php'); // get this file at: https://github.com/alecgorge/jsonapi/raw/master/sdk/php/JSONAPI.php
$api = new JSONAPI("localhost", 20059, "user", "pass", "salt");
$limit = $api->call("getPlayerLimit");
$count = $api->call("getPlayerCount");
$c = curl_init($url);
curl_setopt($c, CURLOPT_PORT, 20059);
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);
echo "<h5>Players online:</h5>";
$num= '' . $count['success'] . '/' . $limit['success'];
echo $num;
This returns: 1/40
Then, I try to get the player list:
$list = $api->call('getPlayerNames');
echo $list;
This just returns: Array
However, when I do
var_dump($api->call('getPlayerNames'));
I get:
array(3) { ["result"]=> string(7) "success" ["source"]=> string(14) "getPlayerNames" ["success"]=> array(1) { [0]=> string(8) "gauso001" } }
However, what I want is simply a list of the players without all of the extra stuff. Sorry if this is a noob question, I only know pretty basic PHP.
Stuff that might help:
method docs: http://alecgorge.com/minecraft/jsonapi/apidocs/#package-JSONAPI%20standard
tell me what else..
THANK YOU in advance, I hope I'll be as good as you in PHP one day :D
Looks like player names, oddly enough, are contained as an array in the success key.
To access the player names, you could:
$list = $api->call('getPlayerNames');
// debug
print_r($list['success']);
// direct access
echo $list['success'][0];
// loop
foreach($list['success'] as $player) {
echo $player;
}
Format to your needs. But that should get you started.
Note: I'd also encourage you to learn about Arrays in PHP.
$api->call('getPlayerNames') returns a named array, one key of which (success) is another array containing the player names. Iterate over the success key to get the player list.
$players = $api->call('getPlayerNames');
foreach($players['success'] as $player) {
echo $player;
}