At the moment im getting a json reponse when i query http://api.steampowered.com/IEconItems_{appid}/GetPlayerItems/v0001/?key={apikey}&steamid={steamid}&format=json
The problem however is that i dont know what to do with this reponse without any aditional info that i cant find anywhere. A part of response im getting when looking at my own cs:go inventory:
{
"id": 235322185,
"original_id": 190991409,
"defindex": 19,
"level": 1,
"quality": 4,
"inventory": 70,
"quantity": 1,
"rarity": 4,
"attributes": [
{
"defindex": 6,
"value": 1130627072,
"float_value": 228
},
{
"defindex": 7,
"value": 1148436480,
"float_value": 975
},
{
"defindex": 8,
"value": 1031063904,
"float_value": 0.059762358665466309
},
{
"defindex": 147,
"value": "models\/weapons\/stattrack.mdl"
},
{
"defindex": 145,
"value": "models\/weapons\/w_smg_p90_mag.mdl"
}
]
},
{
"id": 236527226,
"original_id": 236502674,
"defindex": 27,
"level": 1,
"quality": 9,
"inventory": 82,
"quantity": 1,
"rarity": 3,
"attributes": [
{
"defindex": 6,
"value": 1133608960,
"float_value": 291
},
{
"defindex": 7,
"value": 1142240880,
"float_value": 596.8505859375
},
{
"defindex": 8,
"value": 994750258,
"float_value": 0.0030927178449928761
},
{
"defindex": 80,
"value": 0,
"float_value": 0
},
{
"defindex": 81,
"value": 0,
"float_value": 0
},
{
"defindex": 147,
"value": "models\/weapons\/stattrack.mdl"
},
{
"defindex": 145,
"value": "models\/weapons\/w_shot_mag7_mag.mdl"
}
]
},
{
"id": 236529059,
"original_id": 136971214,
"defindex": 35,
"level": 1,
"quality": 4,
"inventory": 77,
"quantity": 1,
"rarity": 2,
"attributes": [
{
"defindex": 6,
"value": 1077936128,
"float_value": 3
},
{
"defindex": 7,
"value": 1141712676,
"float_value": 564.611572265625
},
{
"defindex": 8,
"value": 1031160533,
"float_value": 0.060122329741716385
},
{
"defindex": 147,
"value": "models\/weapons\/stattrack.mdl"
}
]
}
The id's correnspond with the weapons and the rarity and quantity makes sense too. But i cant be expected to manually find every id of every weapon in every state right? Am i missing a different API that i can use to see what weapon corrensponds with what id?
ps: Sorry for bad english
You will call the GetSchema to determine which items these are. You'll compare DefIndex values against the ones returned by GetSchema
Your work flow will look like this:
Call GetSchema and store result of result['items'] some place to look up later
Call your current call and parse through the items. For each item, look up a result in your stored value from above
GetSchema also has rarities and other values you may need (ie. those attributes)
Compare your inventory defindex (not attributes) value with this schema.
http://api.steampowered.com/IEconItems_730/GetSchema/v0002/?key=xxxxx
For example,defindex = 19 is weapon_p90 on schema.
Try to parse JSON strings into an array. Because this response is not totaly a valid JSON string try to insert this into a new array to parse a valid JSON string.
Such as: {"main_container": [ --> YOUR POSTED CODE <-- ]}
<?php
$jsonResponse = '{"main_container": [
{
"id": 235322185,
"original_id": 190991409,
"defindex": 19,
"level": 1,
"quality": 4,
"inventory": 70,
"quantity": 1,
"rarity": 4,
"attributes": [
{
"defindex": 6,
"value": 1130627072,
"float_value": 228
},
{
"defindex": 7,
"value": 1148436480,
"float_value": 975
},
{
"defindex": 8,
"value": 1031063904,
"float_value": 0.059762358665466309
},
{
"defindex": 147,
"value": "models\/weapons\/stattrack.mdl"
},
{
"defindex": 145,
"value": "models\/weapons\/w_smg_p90_mag.mdl"
}
]
},
{
"id": 236527226,
"original_id": 236502674,
"defindex": 27,
"level": 1,
"quality": 9,
"inventory": 82,
"quantity": 1,
"rarity": 3,
"attributes": [
{
"defindex": 6,
"value": 1133608960,
"float_value": 291
},
{
"defindex": 7,
"value": 1142240880,
"float_value": 596.8505859375
},
{
"defindex": 8,
"value": 994750258,
"float_value": 0.0030927178449928761
},
{
"defindex": 80,
"value": 0,
"float_value": 0
},
{
"defindex": 81,
"value": 0,
"float_value": 0
},
{
"defindex": 147,
"value": "models\/weapons\/stattrack.mdl"
},
{
"defindex": 145,
"value": "models\/weapons\/w_shot_mag7_mag.mdl"
}
]
},
{
"id": 236529059,
"original_id": 136971214,
"defindex": 35,
"level": 1,
"quality": 4,
"inventory": 77,
"quantity": 1,
"rarity": 2,
"attributes": [
{
"defindex": 6,
"value": 1077936128,
"float_value": 3
},
{
"defindex": 7,
"value": 1141712676,
"float_value": 564.611572265625
},
{
"defindex": 8,
"value": 1031160533,
"float_value": 0.060122329741716385
},
{
"defindex": 147,
"value": "models\/weapons\/stattrack.mdl"
}
]
}
]}
';
$jsonDecodeArray = json_decode($jsonResponse, true);
foreach($jsonDecodeArray as $mainContainerItems){
// LOOP THROUGH ALL ITEM GROUP. IN THIS CASE WE HAVE 3 ITEM GROUPS IN
// $mainContainerItems ARRAY
foreach($mainContainerItems as $inventoryItem){
echo $inventoryItem['id']; // will return for example: 236529059
echo $inventoryItem['original_id']; // will return for example: 136971214
echo $inventoryItem['defindex'];
}
}
?>
In this case the resoult will be a multi dimensional array, where you can loop through with multiple foreach.
The resoult is:
$jsonDecodeArray contains the main_container array with all items, where a single item is also an array.
$inventoryItem contains a single item with it's all properties. Like shown below.
Array
(
[id] => 236529059
[original_id] => 136971214
[defindex] => 35
[level] => 1
[quality] => 4
[inventory] => 77
[quantity] => 1
[rarity] => 2
[attributes] => Array
(
[0] => Array
(
[defindex] => 6
[value] => 1077936128
[float_value] => 3
)
[1] => Array
(
[defindex] => 7
[value] => 1141712676
[float_value] => 564.61157226562
)
[2] => Array
(
[defindex] => 8
[value] => 1031160533
[float_value] => 0.060122329741716
)
[3] => Array
(
[defindex] => 147
[value] => models/weapons/stattrack.mdl
)
)
)
Related
Here is the JSON object, so if I look at below object it has children till level 3 and object at level 3 has no children's.
So what is the efficient way to find out?
{
"id": 2,
"name": "Economic Segment",
"maxLevel": 10,
"parentId": null,
"children": [
{
"id": 7,
"name": "Revenue",
"maxLevel": 9,
"parentId": 2,
"children": [
{
"id": 12,
"name": "R Child",
"maxLevel": 8,
"parentId": 7,
"children": [
{
"id": 12,
"name": "R Child",
"maxLevel": 8,
"parentId": 7,
"children": []
}
]
}
]
},
{
"id": 8,
"name": "Expenditure",
"maxLevel": 9,
"parentId": 2,
"children": []
},
{
"id": 9,
"name": "Asset",
"maxLevel": 9,
"parentId": 2,
"children": []
}
]
}}
I am trying to do this:
$unitsPayments = Unit::leftJoin('uf_user', 'uf_unit.owner', '=', 'uf_user.id')
->select('uf_unit.code','uf_user.display_name','uf_unit.neighborhood',
'uf_unit.id AS unit_id','uf_unit.owner AS unit_owner')->get();
but it return the data separated like this.
I need to have first user and all it unit inside it
so at the end I will only have 2 nodes for users and each node will have 2 unit nodes
This is current JSON:
[
{
"code": "121",
"display_name": "zal",
"neighborhood": "wan",
"unit_id": 49,
"unit_owner": 17
},
{
"code": "47",
"display_name": "Ahmad",
"neighborhood": "aim",
"unit_id": 63,
"unit_owner": 2
},
{
"code": "21",
"display_name": "Ahmad",
"neighborhood": "wan",
"unit_id": 64,
"unit_owner": 2
},
{
"code": "102",
"display_name": "zal",
"neighborhood": "war",
"unit_id": 65,
"unit_owner": 17
}
]
and this is how I need the data to look like:
[{
"display_name": "zal",
"unit_owner": 17,
"units": {
"code": "102",
"neighborhood": "war",
"unit_id": 65
} {
"code": "121",
"neighborhood": "wan",
"unit_id": 49
}
},
{
"display_name": "Ahmad",
"unit_owner": 2,
"units": {
"code": "47",
"neighborhood": "aim",
"unit_id": 63,
} {
"code": "21",
"neighborhood": "wan",
"unit_id": 64,
}
}
]
You really should set up proper relationships using Eloquent: https://laravel.com/docs/5.5/eloquent-relationships
Your code will look like this:
$unitsPayments = User::with('units')->get();
And in User class:
class User extends Model {
public function units(){
return $this->hasMany(Unit::class, 'owner');
}
}
I am using Magento 2 rest api for listing all the catgories.
{{production_url}}/index.php/rest/V1/categories
It will return all the categories,
{
"id": 2,
"parent_id": 1,
"name": "Default Category",
"is_active": true,
"position": 1,
"level": 1,
"product_count": 0,
"children_data": [{
"id": 3,
"parent_id": 2,
"name": "T-shirts",
"is_active": true,
"position": 1,
"level": 2,
"product_count": 8,
"children_data": []
}, {
"id": 4,
"parent_id": 2,
"name": "Phants",
"is_active": true,
"position": 2,
"level": 2,
"product_count": 0,
"children_data": []
}, {
"id": 5,
"parent_id": 2,
"name": "Chridar",
"is_active": true,
"position": 3,
"level": 2,
"product_count": 0,
"children_data": []
}]
}
But i need custom attributes for every categories in the result.But now i have to call the below api for getting custom attributes.
{{production_url}}/index.php/rest/V1/categories/3
It will return ,
{
"id": 3,
"parent_id": 2,
"name": "T-shirts",
"is_active": true,
"position": 1,
"level": 2,
"children": "",
"created_at": "2017-06-02 11:21:16",
"updated_at": "2017-06-02 11:21:16",
"path": "1/2/3",
"available_sort_by": [],
"include_in_menu": true,
"custom_attributes": [
{
"attribute_code": "description",
"value": "<p>retest</p>"
},
{
"attribute_code": "image",
"value": "Screen_Shot_2017-06-16_at_4.06.35_PM.png"
},
{
"attribute_code": "display_mode",
"value": "PRODUCTS"
},
{
"attribute_code": "is_anchor",
"value": "1"
},
{
"attribute_code": "path",
"value": "1/2/3"
},
{
"attribute_code": "children_count",
"value": "0"
},
{
"attribute_code": "custom_use_parent_settings",
"value": "0"
},
{
"attribute_code": "custom_apply_to_products",
"value": "0"
},
{
"attribute_code": "url_key",
"value": "redwine"
},
{
"attribute_code": "url_path",
"value": "redwine"
}
]
}
Suppose if there are n catgories i need to call n api for getting custom attributes.Is there any single api for getting all the attributes for all categories in a single API?
The Magento Api CatalogTreeInterface doesn't extend Magento\Framework\Api\ExtensibleDataInterface which means that custom attributes or extension attributes cannot be added to the tree response. The only workaround for me was to create my own module and a new api call that extends the tree interface to add my custom attributes.
I suppose Ruben is right. I've built Magento extension to add image attribute to the category tree, please have a look https://github.com/troublediehard/mma-customapi
Given the following two arrays, how can they be merged efficiently to result in the third array?
productData
$productData =
[
{
"product_id": 4,
"type": "electronic",
"name": "monitor",
"specs": {
"HDMI": true,
"VGA": false
}
},
{
"product_id": 5,
"type": "electronic",
"name": "HDMI cable",
"specs": {
"length": "3ft"
}
},
{
"product_id": 6,
"type": "kitchen",
"name": "spoon"
}
]
products
$products =
{
"products": 3,
"per_page": 10,
"current_page": 1,
"data": [
{
"id": 4,
"product_type": "electronic",
"product_id": 6
},
{
"id": 6,
"type": "electronic",
"product_id": 5
},
{
"id": 9,
"type": "kitchen",
"product_id": 4
}
]
}
productsFinal ($productData merged into $products - based on matching combo of product_id/product_id and type/product_type)
$productsFinal =
{
"products": 3,
"per_page": 10,
"current_page": 1,
"data": [
{
"id": 4,
"product_type": "electronic",
"product_id": 6,
// How to merge product data and wrap with "data" key
"data": {
"product_id": 6,
"type": "kitchen",
"name": "spoon"
}
},
{
"id": 6,
"type": "electronic",
"product_id": 5,
// How to merge product data and wrap in "data" key
"data": {
"product_id": 5,
"type": "electronic",
"name": "HDMI cable",
"specs": {
"length": "3ft"
}
}
},
{
"id": 9,
"type": "kitchen",
"product_id": 4,
// How to merge product data and wrap in "data" key
"data": {
"product_id": 6,
"type": "kitchen",
"name": "spoon"
}
}
]
}
I tried different things for the outcome in a foreach loop but still cannot get it to render as intended:
foreach($productData as $productDataItem) {
// when $productDataItem.product_id == $product.product_id && $productDataItem.type == $product.product_type
// move the matching $productDataItem object into matching $product object, wrapped in a new "data" key
}
I don't know Laravel too well. However you can join your data objects quite easily:
<?php
$productData = json_decode('[
{
"product_id": 4,
"type": "electronic",
"name": "monitor",
"specs": {
"HDMI": true,
"VGA": false
}
},
{
"product_id": 5,
"type": "electronic",
"name": "HDMI cable",
"specs": {
"length": "3ft"
}
},
{
"product_id": 6,
"type": "kitchen",
"name": "spoon"
}
]');
$products = json_decode('{
"products": 3,
"per_page": 10,
"current_page": 1,
"data": [
{
"id": 4,
"type": "electronic",
"product_id": 6
},
{
"id": 6,
"type": "electronic",
"product_id": 5
},
{
"id": 9,
"type": "kitchen",
"product_id": 4
}
]
}');
// combine both data objects
foreach($products->data As &$p) {
foreach($productData As $d) {
if(property_exists($p, "product_id") && property_exists($d, "product_id") && property_exists($p, "type") && property_exists($d, "type")) {
if($p->product_id==$d->product_id && $p->type==$d->type) {
//$p = (object) array_merge((array) $p, (array) $d);
$p->data = $d; // updated answer
continue;
}
}
}
}
echo("<pre>");
echo json_encode($products, JSON_PRETTY_PRINT);
?>
You can test the code here: http://sandbox.onlinephpfunctions.com/code/98a50c35ee32c30f0d2be1661f7afb5895174cbe
Update: http://sandbox.onlinephpfunctions.com/code/aeebfdcf4f4db5e960260e931982570cfed19e0e
I would suggest to check this package dingo/api. I assume you want to display some kind of JSON response. Take a look at Transformers. You can do something like this :
<?php
namespace App\Http\Transformers;
use App\Http\Controllers\ProductData;
use League\Fractal\TransformerAbstract;
class ProductsDataTransformer extends TransformerAbstract
{
/**
* Turn this item object into a generic array
*
* #return array
*/
public function transform(ProductData $productdata)
{
return [
'id' => $productdata->id,
'product_type' => $productdata->product_type,
'product /*or data*/' => Product::find($productdata->product_id),
];
}
}
This would find the product by it's ID and look like this :
{
"id": 4,
"product_type": "electronic",
"product" {
"product_id": 6,
"type": "kitchen",
"name": "spoon"
},
},
You can then also create a transformer for Product to take care of your specs attribute to do the same thing.
I searched around for the answer to this but couldn't find anything and was looking for help from you wonderful people. I have been toying around with steam web API for my site. I have found this code and am using it. Also, I have barely any experience in steam api, most of my experience is in C. Anyway, here is the code:
[insert_php]
$api = "http://api.steampowered.com/IEconItems_440/GetPlayerItems/v0001/?key=MyApiKey&steamid=MySteamId&format=json";
$json = (file_get_contents($api));
$schema = json_decode($json);
print var_dump($schema);
[/insert_php]
I am using a plugin and inserting this php into my WordPress page. This code goes into my steam backpack and gives me all the items I have in a complicated list. What I need help with is condensing it so that it can be easily read. A defining feature of the inventory items is the defindexes. What I want to do is have it so if it finds a certain amount of one item with the same defindex, it will return that amount like this into my page: Scrap Metal = # of defindexes of Scrap Metal found. I hope that this is clear enough and that there is an answer. Thank you.
Part of my code that is returned now:
{
"id": 1828947688,
"original_id": 1176490973,
"defindex": 5009,
"level": 1,
"quality": 6,
"inventory": 2147483650,
"quantity": 1,
"origin": 4
},
{
"id": 1828947700,
"original_id": 1176491289,
"defindex": 5009,
"level": 1,
"quality": 6,
"inventory": 2147483651,
"quantity": 1,
"origin": 4
},
{
"id": 1828947742,
"original_id": 1178541917,
"defindex": 5009,
"level": 1,
"quality": 6,
"inventory": 2147483652,
"quantity": 1,
"origin": 4
},
{
"id": 1828947755,
"original_id": 1178542060,
"defindex": 5009,
"level": 1,
"quality": 6,
"inventory": 2147483757,
"quantity": 1,
"origin": 4
},
{
"id": 1828947766,
"original_id": 1179066746,
"defindex": 5005,
"level": 1,
"quality": 6,
"inventory": 2147483653,
"quantity": 1,
"origin": 4
},
{
"id": 1828947780,
"original_id": 1181421843,
"defindex": 5009,
"level": 1,
"quality": 6,
"inventory": 2147483756,
"quantity": 1,
"origin": 4
},
{
"id": 1828947788,
"original_id": 1181426745,
"defindex": 5006,
"level": 1,
"quality": 6,
"inventory": 2147483654,
"quantity": 1,
"origin": 4
},
{
"id": 1828947793,
"original_id": 1187413384,
"defindex": 5007,
"level": 1,
"quality": 6,
"inventory": 2147483755,
"quantity": 1,
"origin": 4
},
{
"id": 1828947796,
"original_id": 1187413535,
"defindex": 5007,
"level": 1,
"quality": 6,
"inventory": 2147483655,
"quantity": 1,
"origin": 4
},
{
"id": 1828947801,
"original_id": 1187416362,
"defindex": 5007,
"level": 1,
"quality": 6,
"inventory": 2147483754,
"quantity": 1,
"origin": 4
},
{
"id": 1828947810,
"original_id": 1190342559,
"defindex": 5013,
"level": 1,
"quality": 6,
"inventory": 2147483656,
"quantity": 1,
"origin": 4
},
{
"id": 1828947826,
"original_id": 1190342965,
"defindex": 5013,
"level": 1,
"quality": 6,
"inventory": 2147483753,
"quantity": 1,
"origin": 4
},
{
"id": 1828947835,
"original_id": 1243518373,
"defindex": 5011,
"level": 1,
"quality": 6,
"inventory": 2147483657,
"quantity": 1,
"origin": 4
}
ETC.
You will need to loop through each item in your result $schema and search for the defindexes you are looking for.
For example, if you want to count all of your metal, you'll be looking for 5000, 5001, and 5002.
$metal_array = array(5000, 5001, 5002);
foreach ($schema as $item)
{
if (in_array($metal_array, $item->defindex)
{
// Do something for the $item. Presumably, you'll add these up
// So that you can get a total metal count (remember that refined is worth 9 scrap
// and reclaimed is worth 3)
}
}
Edit:
To answer some of the questions in your comment:
$item->defindex is referring to the defindex of the current $item object in your schema. You are performing object iteration in this foreach loop. Each item is being iterated over in the foreach loop. So, on the first pass through the loop, $item will look like this:
{
"id": 1828947688,
"original_id": 1176490973,
"defindex": 5009,
"level": 1,
"quality": 6,
"inventory": 2147483650,
"quantity": 1,
"origin": 4
}
You can access any of these values by doing $item->ATTRIBUTENAME (ie. $item->quality or $item->origin). On this pass through the loop, it will check if $item->defindex, which equals 5009 is in the $metal_array, which contains 5000, 5001, and5002`. It does not, so the if block does not execute.
As for what you'd do in that loop, I'd probably modify the code slightly to do something like this:
$metal_array = array(5000, 5001, 5002);
$total_metal = 0;
foreach ($schema as $item)
{
if (in_array($metal_array, $item->defindex)
{
switch ($item->defindex)
{
case 5000:
$total_metal++; // or $total_metal += 1;
break;
case 5001:
$total_metal += 3;
break;
case 5002:
$total_metal += 9;
break;
}
}
}
At the end of this block, you will have a value in $total_metal that equals the scrap value of all of your metal.
This block uses the switch statement to determine how much a particular item is worth. The break statement within each case prevents the logic from "falling through" to the next option.
For example, if the break was not added to the end of the case 5000 block of code and an $item->defindex equaled 5000, it would add 1 and then 3. This is not the answer you want. It is, however, a valid option to consider in certain circumstances.
switch ($item->defindex)
{
case 5000:
$total_metal++; // or $total_metal += 1;
case 5001:
$total_metal += 3;
break;
}
Finally, if you are new to PHP, I recommend taking a look at the documentation: http://php.net/manual/en/index.php