Error with Array - php

Im not sure why, never used to use arrays anyway till now. Well, I have this code:
$inventoryJsonUrl = 'http://steamcommunity.com/inventory/'.$steamID.'/730/2?l=english&count=5000';
$inventoryJsonGet = file_get_contents($inventoryJsonUrl);
$inventories = json_decode($inventoryJsonGet, TRUE);
foreach($inventories['success']['rgInventory'] as $key => $description)
{
echo $description['classid'];
}
And Im getting this error:
Warning: Invalid argument supplied for foreach()
I have also another trouble, how to get name/value or whatever it is thatis market on this image (it is also the Json im using: https://i.imgur.com/oaNCquW.png
If I can also ask, for any good but "simple" JSON tutorials for things that im using here?
Thanks for help.

Here you need to do this:
foreach($inventories['rgInventory'] as $key => $description)
{
echo $description['classid'];
}
Note: Steam API is a complicated API and often doesn't work the way you would like to. If you are calling more than 30 times per minute, make sure to use proxies.

Related

How can I write php codes as string?

I have this code:
<?php $myarray->appends(['action' => $_GET['desc']])->render() ?>
When there is action argument in the url, then my code works as well. Otherwise it throws this error message:
Undefined index: action (View:
/var/www/html/myweb/resources/views/mypage.blade.php)
So I need to do this ->appends(['action' => $_GET['desc']]) dynamically. Something like this:
<?php
if ( isset($_GET['desc']) ) {
$append_param = "->appends(['action' => $_GET['desc']])";
} else {
$append_param = "";
}
$myarray.$append_param->render();
?>
But I'm pretty much sure my code won't work .. I wore code about just for showing you what was my point.
Anyway, can anybody tell me how can I do that in a right way?
All I'm trying to do is: appending action argument to the pagination-links if it already exists. Otherwise I don't want to append it.
You should never take input from a user-controllable source and execute it. Doing this is a major security risk, and it will probably open more security holes in your server than you'd be able to patch in a lifetime.
If you really, really must do this, you can use eval(). But please, make sure you understand the security implications of this.
i think you are just horribly overthinking the whole situation. you don't have to put your code in a string to execute it conditionally, you can just write it as code itself.
having no further information about how your class works internally, the most straightforward and basic way would be:
if ( isset($_GET['desc']) ) {
$myarray->appends(['action' => $_GET['desc']])->render()
} else {
$myarray->render();
}
You cannot do that.
First, if you want to check for a valid key in an array, use "array_key_exists(key, array)" which is hugely recommanded.
if ( array_key_exists('dec',$_GET) ) {
Then, if I understand, you want to "append" data to an Array ? That means adding a value to a key in this array.
This can be done easily with PHP :
$array [ $key ] = $value;
Be very careful on your variable names :
$myarray.$append_param->render();
This makes no sense, if it's an array, you only should use [] on it (or array functions), but if it's an object, you only can use -> on it.

Unable to save the last xml file php

I am fairly new to php, and I have written code to work with the amazon API. When I request information from the API, I receive it, but am unable to sort through the XML. Here is the error:
Fatal error: Call to a member function children() on null in J:\XAMPP\htdocs\Phillip\src\MarketplaceWebServiceProducts\Samples\csv_prep.php on line 117
Here is the code:
if(is_array($xmlFiles)){
foreach($xmlFiles as $xmlFile){
$xml = simplexml_load_file($xmlFile);
foreach($xml->GetMatchingProductForIdResult as $items) {
//Line 117 ->
if(isset($items->Products->Product->AttributeSets->children('ns2', true)->ItemAttributes->ListPrice->Amount) !== False) {
$amount = $items->Products->Product->AttributeSets->children('ns2', true)->ItemAttributes->ListPrice->Amount
}else{
$amount = '0.00';
}
}
}
}
The tag in the XML that I am trying to get the value of looks like this:
<ns2:amount>9.99</ns2:amount>
It is in the same place as it says in the code. I only have this problem with large files and I am not sure what is happening. If someone could help, I would greatly appreciate it. Thanks in advance!
I'm not certain, but based on the error message, it looks like somewhere in the $items->Products->Product->AttributeSets hierarchy something you're specifying does not exist.
Do tests to see if $items or $items->Products or $items->Products->Product or $items->Products->Product->AttributeSets and whichever one fails, print out that fact and call exit;.
You could do a print_r on $items to help as well.

json_decode - issues with api

I am trying to find the game someone is playing on twitch by using the api. I have setup the json_decode and it shows all of the content from the api. However whenever I try to print_r the game I get an error.
The error:
Notice: Undefined property: stdClass::$game in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\projects\Portfolio -- Website\twitchstreaminfo\streaminfo.php on line31
PHP code:
$streamer = $_POST['username'];
$apiurl = "https://api.twitch.tv/kraken/streams/" . $streamer;
$apicontent = file_get_contents($apiurl);
$streamerinfo = json_decode($apicontent);
print_r($streamerinfo->game);
Try just doing the following first and verify the result:
print_r( $streamerinfo );
From what I can see with the API, the following should work:
print_r( $streamerinfo->stream->game );
Your error is saying that the propery of "$game" does not exist on the object "$streamerinfo". As suggested above, try priting the "$streamerinfo" to varify that it is valid. Another thing you can do to prevent this is to add the following :
if (isset($streamerinfo->game) {
print_r($streamerinfo->game);
}
That code will prevent this error, but not fix the problem. I suggest this as a final solution to help you solve the problem
if (isset($streamerinfo->game) {
print_r($streamerinfo->game);
} else {
print_r($streamerinfo);
}
This will keep your code from breaking in the way that it is now. But, it will also print "$streamerinfo" if it fails. This way you can see why it failed.

splitting apart response from an API

I am trying to start using APIs, doing calls and so forth. Just barely starting to learn. Found a way to get Facebook shares on a post using the graph api.
I did this with PHP; here is the code:
$response = file_get_contents('https://graph.facebook.com/? id=mydomain.com');
echo $response;
this is the response that I get:
{"id":"http://sportsmockery.com/2014/11/hey-bears-fire-everyone/","shares":22}
What I want is to somehow get the share count (22) into a variable so I can do stuff with it…(i changed the domain that gets that share count to my domain.com);
anyway; I am not sure what is the standard way to do this, if you control what is received with how you do your call; or if you just get the full response and pull out what you want…
Been looking around and have not been able to find anything that will really help with this.
I am hoping someone can help me with this…
All the best, G
Use json_decode() which will make all of the properties easily accessible.
$parsedResponse = json_decode($response);
$count = $parsedResponse->shares;
echo $count;
This is a json response.
Parsing JSON file with PHP
http://php.net/manual/en/function.json-decode.php

Get Tweets from EpiTwitterJson

I've been banging my head against the wall for a few days now trying to do something that should be so simple. I am trying to use Jaison Mathai's EpiTwitter library to retrieve tweets from my account. Here is my code to get my tweets:
<?php $twitterObj = new EpiTwitter($__TWITTER['ConsumerKey'], $__TWITTER['ConsumerSecret'],$__TWITTER['MyAccessToken'],$__TWITTER['MyAccessTokenSecret']);
$twitterInfo= $twitterObj->get_accountVerify_credentials();
$twitterInfo->response;
// ^ This part works fine, I can get my user info and profile pic and whatnot
$username = $twitterInfo->screen_name;
$tjson = $twitterObj->get('statuses/user_timeline.json', array("screen_name"=>$username));
As far as I can tell, ^ this also works as it should. If I var_dump($tjson), I can see the tweets that I want to access, among a HUGE mess of other data.
My question boils down to this: from this point, how can i simply print out each tweet returned? Here's what I've tried so far:
foreach($tjson as $tweet) print($tweet)
//^ Throws IllegalArgumentException, "Not an array or object"
foreach($tjson->responseText as $tweet) print($tweet)
//^ Warning: illegal argument. Doesn't print anything
print_r($tjson->response) //Prints NULL
I thought $tjson->response would be what I want, because in the __get method in EpiTwitterJson, there is a line:
$this->response = json_decode($this->responseText, 1);. Right after this line if I var_dump($this->response), I get my tweets along with a little less data than var_dump($tjson) before. If on that same line I print($this->response['status']['text']), I get a nice string of my tweet, which is exactly what I want.
What I don't quite understand is why $this->response is set inside of the EpiTwitterJson class, but $tjson->response is NULL in my code. I've looked and looked and looked but I can't seem to find any code snippets to get tweets with this library specifically. I think it's just one of those things that must be so obvious and simple that my brain refuses to accept it.
Obviously I could just crawl through $tjson->responseText, but I thought the whole point of the EpiTwitterJson class was so I would not have to do that? The ideal answer to my question will be code to boil down my $tjson object so i can say
foreach(something as $tweet) //do stuff with the tweet
Second best answer would be code to do this with another library. I only chose the EpiTwitter lib because it 'supports async' which sounds kinda cool if I ever decide to use it, and because it seemed like the simplest (ie least lines of code on my part). Quite frankly I really care more about making this work than understanding it; I can figure out what it all means later on.
foreach($tjson->response as $post){
echo '<pre>';
var_dump($post);
echo '</pre>';
}
this works for me.
for eg. u can get id of post like that $post['id'], or user name $post['user']['name']

Categories