Steam Inventory in PHP from json - php

so basically Im trying to get steam player inventory in PHP from geting json content but I cannot figure out how to do it, espcially I didn't work with json a lot before. I have a problem with what I should pick in PHP to get what I want from the JSON.
PHP:
$inventoryJsonUrl = 'http://steamcommunity.com/inventory/'.$steamID.'/730/2?l=english&count=5000';
$inventoryJsonGet = file_get_contents($inventoryJsonUrl);
$inventory = json_decode($inventoryJsonGet, TRUE);
for ($i=0; $i < count($inventory['assets']) ; $i++) {
echo $inventory['assets'];
}
And lets say the $inventoryJsonURL is now http://steamcommunity.com/inventory/76561198260345960/730/2?l=english&count=5000
And I have problem with getting what I want, I mean lets say that in the for loop I want to have name of the item/skin, id of that item and some more things. But I don't know how and what I suppose to pick to get that.
Sorry for bad bad English.
Thanks in advance.

You can make use of PHP's foreach loop.
$inventories = json_decode($inventoryJsonGet , TRUE);
// you can check the structure of your array by
// print_r($inventories)
foreach ($inventories['descriptions'] as $key => $description) {
echo '<pre>';
echo $description['appid'];
echo $description['market_name'];
echo '</pre>';
}

The endpoint contains two lists: assets and descriptions. It's hard to offer some help if you don't really know what you are looking for. I think what you are looking for is descriptions, as there is all the data. See here for the first item: https://www.dropbox.com/s/z736vu6boh9rfi6/steam1.gif?dl=0
Seems as tho that is a shotgut from Counterstrike GO.
Also, this article may help you a bit: Getting someone's Steam inventory
And as a start, I suggest to beautify the content of that json, so you have a better overview of what's in there. I usually use https://codebeautify.org/jsonviewer but there are several other.

Related

Select specific values from json_decode in php

I've stumbled on a problem that i think is a bit strange since i've followed other examples to the exact code but i still didn't get the same result.
Short summary
I'm developing a shopping application and i'm using a session for the cart. Since i want to have the possibility to remove items from the cart i am using some levels inside the JSON object of the session.
Cart summary
The cart is created on button click
$_SESSION['cart']["cartItems"][$product_id]=$cartProduct;
Where $cartProduct is an array() of items like the product_id, product_name, etc.
Where the cartlevel: $product_id is for the remove cart row function().
The cart is then shown in an angularJS application. I will pass the full view and just show the getCart section and the JSON response.
$json = json_encode($_SESSION['cart']['cartItems']);
echo($json);
The JSON echo is:
{"3":{"id":"3","name":"Bedrukte Jurk","price":25.99,"size":"M","quantity":"3","total_product_price":77.97},"2":{"id":"2","name":"Blouse","price":26.99,"size":"S","quantity":"3","total_product_price":80.97}}
The JSON is stripped by removing the ['cartItems'] value.
The $_SESSION['cart']'s original JSON object would be:
{"cartItems":{"3":{"id":"3","name":"Bedrukte Jurk","price":25.99,"size":"M","quantity":"3","total_product_price":77.97},"2":{"id":"2","name":"Blouse","price":26.99,"size":"S","quantity":"3","total_product_price":80.97}}}
{"cartItems":{"3":{"id":"3","name":"Bedrukte Jurk","price":25.99,"size":"M","quantity":"3","total_product_price":77.97},"2":{"id":"2","name":"Blouse","price":26.99,"size":"S","quantity":"3","total_product_price":80.97}}}
The question
So now i would like to select only the "name" ,"id" and other values seperatly since i have to write them to a database. it would be something like:
$json_test = json_decode($_SESSION['cart']['cartItems']['name']);
echo $json_test;
$product_name = $json_decode_name;
So at this moment my question really starts. i'm having one issue and one real question on how ould i achieve this (thinkin of using foreach()
So first my problem is that i can't, or i am not able to select the specific values from the json cart session. I've tried some things from the PHP docs and made a small comparison:
$json1 = '{"foo-bar": "123", "foo-bar2": "456"}';
echo($json1);
echo "<br>";
$obj = json_decode($json1);
print $obj->{'foo-bar2'}; // 456
echo "<br>";
$json_decode = json_decode($_SESSION['cart']['cartItems']['3']);
print $json_decode->{'name'};
echo "<br>";
var_dump(json_decode($_SESSION['cart']['cartItems']['3'], true));
echo "<br>";
$json = json_encode($_SESSION['cart']);
echo($json);
The $json1 is working but i can't quite get my own version working.
Some extra tips?
The second thing is then pushing it to the database. I was thinking about: when achieving the name / names from the JSON string i would use a foreach() statement. Something in the area of:
$cart_product_name = $json_decoded_name
foreach ($cart_product_name as $product_name) {
insert_Query
}
And repeat this for the id's, quantity's, etc.
If the question isn't clear please let me know in the comments and i'll edit it.
As always, Thanks in advance!

Pulling title from API

Alright so I started with something simple just to get me familiar with what exactly I am getting my self into, how ever when tinkering I became lost.
Okay so I am trying to get contents from the following
$details = json_decode(file_get_contents("https://beam.pro/api/v1/users/63662"));
what's the best way I can go about doing this?
Currently I can display the username portion using print $details->username; and the id portion using print $details->id; but after this I become lost how could I go about pulling the title for example.
Here is what the Twitter looks like currently in the API
"name":"Thursday -- BR 2's [NA] w/ beam.pro/para",
Documentation is here
You would use the following:
echo $details->channel->name;
However, if you're more comfortable with arrays, you could do this:
$details = json_decode(file_get_contents("https://beam.pro/api/v1/users/63662"), true);
echo $details['channel']['name'];
Here is the object structure for future reference:

Assign variable to an array from Active Directory

I am having trouble figuring out how to correct an issue I am having with the following code. I am trying to list the names and emails of all the people in my Active Directory. This code works. However, I also get the following warning when it is executed.
Warning: Cannot use a scalar value as an array.
From what I have read online I need to set " $name['mail']['0'] = "Not Found";" to an array. My question is how would I go about doing this. I have tried every way I could think of with no success. If anyone could provide me with some feedback it would be greatly appreciated.
foreach ($results as $name) {
if (!isset($name['mail']['0'])){
$name['mail']['0'] = "Not Found";
}
$allnames[$name['cn']['0']]['mail'] = $name['mail']['0'];
It looks like you are trying to assign the "Not Found" value BACK into the results of the ldap query you are running.
When I wrote code to get user information from ldap, I always inserted values into a different array (getting the fields I wanted) and then displayed THAT array out in the HTML:
foreach ($results as $name)
{
if (isset($name['mail']['0']))
// Check if it IS set, rather than not set and then append the data.
{
$allnames[$name['cn']['0']]['mail'] = $name['mail']['0'];
}
}
Then after you have all the fields you want you display the $allnames array.
Having said that, when I construct the array of user information, I make the array much much simpler - knowing what I want to display out, more along the lines of this:
foreach ($results as $name)
{
if (isset($name['mail']['0']))
// Check if it IS set, rather than not set and then append the data.
{
$allnames['mail'] = $name['mail']['0'];
}
}
You (or at least I) don't need to follow the ldap structure when making the array of information I have gathered, but rather I want to get specific fields from the directory and then display them by what they are - for example when I output the value in mail I might want to hyperlink it as an email address, so I keep it in an array element that I know by key and refer to later.

PHP Cart with Object Oriented PHP

I am developing a simple system of sample products with Object Oriented PHP, very simple thing. So far, no problem, but I have to create a button that adds a product code recorded in the database to a form in the sidebar. I do not know to develop a shopping cart with OO PHP, and codes that I find always give error because of the call of the database, or when the data list. I've been thinking of doing for JS, any help?
sorry my bad english
I got it, first I did when I click a link step by GET and the ID Code I need after seto it in a cookie, each cookie with an ID. Then I check if cookie has registered with the IDs. Not the best and most correct, but it works (more or less). Now another problem, I need to click two times to get the result as it passes by id and need to be caught refresh = /
I think it was a bit confusing but it is the maximum that dyslexia allows me to do hehehe
Here I set up the structure with the data I have in my product page:
add
<?php
$cod_get = $_GET['cod'];
setcookie("SITENAME_cod_".$id_get."", $cod_get, time()+3600, "/","", 0);
?>
And here I have a loop checking if cookie with ids, I think it will give problems, but for now I think it works ...
Thank you all.
$produto = new produtos();
$i = 0;
$produto->selecionaTudo($produto);
$produto->selecionaCampos($produto);
while($res = $produto->retornaDados()):
$res->id;
$i++;
$get_cookie = $_COOKIE['SITENAME_cod_'.$res->id.''];
if (isset($get_cookie)) {
echo $get_cookie.', ';
}else{
echo "";
}
endwhile;

PHPFlickr script...could this be cleaner/leaner?

OK, here's my dilemma:
I've read all over about how many guys want to be able to display a set of images from Flickr using PHPFlickr, but lament on how the API for PhotoSets does not put individual photo descriptions. Some have tried to set up their PHP so it will pull the description on each photo as the script assembles the gallery on the page. However, the method has shown how slow and inefficient it can be.
I caught an idea elsewhere of creating a string of comma separated values with the photo ID and the description. I'd store it on the MySQL database and then call upon it when I have my script assemble the gallery on the page. I'd use explode to create an array of the photo ID and its description, then call on that to fill in the gaps...thus less API calls and a faster page.
So in the back-end admin, I have a form where I set up the information for the gallery, and I hand a Set ID. The script would then go through and make this string of separated values ("|~|" as a separation). Here's what I came up with:
include("phpFlickr.php");
$f = new phpFlickr("< api >");
$descArray = "";
// This will create an Array of Photo ID from the Set ID.
// $setFeed is the set ID brought in from the form.
$photos = $f->photosets_getPhotos($setFeed);
foreach ($photos['photoset']['photo'] as $photo) {
$returnDesc = array();
$photoID = $photo['id'];
$rsp = $f->photos_getInfo($photoID);
foreach ($rsp as $pic) {
$returnDesc[] = htmlspecialchars($pic['description'], ENT_QUOTES);
}
$descArray .= $photoID."|~|".$returnDesc[0]."|~|";
}
The string $descArray would then be placed in the MySQL string that puts it into the database with other information brought in from the form.
My first question is was I correct in using a second foreach loop to get those descriptions? I tried following other examples all over the net that didn't use that, but they never worked. When I brought on the second foreach, then it worked. Should I have done something else?
I noticed the data returned would be two entries. One being the description, and the other just an "o"...hence the array $returnDesc so I could just get the one string I wanted and not the other.
Second question is if I made this too complicated or not. I like to try to learn to write cleaner/leaner code, and was looking for opinions.
Suggestions on improvement are welcome. Thank you in advance.
I'm not 100% sure as I've just browsed the source for phpFlickr, and looked the the Flickr API for the getInfo() call. But let me have a go anyway :)
First off, it looks like you shouldn't need that loop, like you mention. What does the output of print_r($rsp); look like? It could be that $rsp is an array with 1 element, in which case you could ditch the inner loop and replace it with something like $pic = $rsp[0]; $desc = $pic['description'];
Also, I'd create a new "description" column in your database table (that has the photo id as the primary key), and store the description in their on its own. Parsing db fields like that is a bit of a nightmare. Lastly, you might want to force htmlspecialchars to work in UTF8 mode, cause I don't think it does by default. From memory, the third parameter is the content encoding.
edit: doesn't phpFlickr have its own caching system? Why not use that and make the cache size massive? Seems like you might be re-inventing the wheel here... maybe all you need to do is increase the cache size, and make a getDescription function:
function getDescription ($id)
{
$rsp = $phpFlickr->photos_getInfo ($id);
$pic = $rsp[0];
return $pic['description'];
}

Categories