JSON not displaying with error in PHP - php

I'm looking to display Json from a url (http://pastebin.com/raw.php?i=MSyA8CBZ). Then, I want the "steamid" to be displayed. Currently, I am using this code, but it's not working:
<?php
$json = file_get_contents('http://pastebin.com/raw.php?i=MSyA8CBZ');
$obj = json_decode($json);
echo $obj->response->players->steamid;
?>
I get the error: Fatal error: Cannot use object of type stdClass as array in /home/u434538987/public_html/testjson.php on line 4 What is wrong with my code?

As mentioned in the comment, players is an array, is you could have seen if you had done print_r($obj) or even saw what the URL you load produced. So you should do :
$obj->response->players[0]->steamid;

Related

Trying to get property of non-object with syntax error

I created a instagram feed for opencart and when i wrote the code in normal php file it worked perfectly but when i transformed to opencart controller view it returns when
json_last_error();
int(4) syntax error
and here is my code in controller
$response= utf8_encode(file_get_contents("https://www.instagram.com/user/?__a=1"));
$response= str_replace(array('ï',''), '',$response);
$response= json_decode($response);
$data['orgimages'] = json_last_error();
i tried so many solutions or filtering out methods and that is my final code as it was changed so many times from stackoverflow answer solution for other questions but nothing seems to be worked out and even their forums didn't worked out with my issue.
Your error indicates that your json is wrongly formatted so try to echo it and validate it. I am suspecting that your json is not a valid json.
Use this : https://jsonlint.com/ validator for json.
After this line :
$response= str_replace(array('ï',''), '',$response);
echo your $response variable and see how your json looks like

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.

json_decode giving error in php for webservice call

I am consuming a webservice in PHP , below is code sample
> if ( $datas=file_get_contents($url) ) return json_decode($datas);
> return Array('error' => "Can't open url");
I return the the value of above function call to js page. but I dont get the value of webservice call in js but always get error value. However if I open the $url in browser I get below data (valid json) :
{"d":[ [ 5.305,428 ] ] }
Due to framework restriction i can use only json_decode() to return values back to my js page.
I also tried json_decode($datas,true); but still no help.
I also tried consuming same code in standalone php file :
I get this error : when I use json_decode($datas)
Catchable fatal error: Object of class stdClass could not be converted
to string in C:\xampp\htdocs\webservice-demo\clientRestTest.php on
line 32
and below error if I use json_decode($datas,true);
Notice: Array to string conversion in
C:\xampp\htdocs\webservice-demo\clientRestTest.php on line 32 Array
Please let me know how to resolve the issue .
Is there any special chars in $url? if there is, try urlencoding it first
file_get_contents(urlencode($url)); etc etc

How to catch error in JSON response?

I'm getting the json data from the twitter using the wordpress function wp_remote_get as below:
$response = wp_remote_get('http://api.twitter.com/1/statuses/user_timeline.json?screen_name=twitter');
$json = json_decode($response['body']);
and it works fine. However, it totally creates the problem on my page if the twitter returns some error. In that case the content on my page does not load after that error part. So how can I catch the error and continue, only if there is no error.
For example if the twitter limit exceeds, it returns following response:
{"request":"\/1\/statuses\/user_timeline.json?screen_name=twitter","error":"Rate limit exceeded. Clients may not make more than 150 requests per hour."}
From the above responsive, how can i get the error.
I'm trying following, but it does not work (my page does not load at all)
if (!isset($json['error']){
//continue
}
It doesn't work because you are trying to access $json as an array, which it is not. It's an object, so use:
if (!isset($json->error){
//continue
}
Your page doesn't load because it's causing a fatal error, which in turn produces a 500 internal server error. If you check your error log you'll find:
Fatal error: Cannot use object of type stdClass as array in ....
You need to use json_decode to "transform" your "json string" in an object.
$j = json_decode($json);
echo $j->{'error'}
Here you will find everything what you need to know about json_decode: http://php.net/manual/en/function.json-decode.php

Call to undefined method SimpleXMLElement::child_nodes()

i am having a code which parses the xml text which is obtained from google search
it used to working fine before , I think after updating my version infos it not working fine
what might be the problem i am getting the following error
Fatal error: Call to undefined method SimpleXMLElement::child_nodes() in /home/search.php in line 70
Please let me know how can i solve this problem
This code is used in smarty
And it gets the response string from curl.
SimpleXMLElement does not have a method called child_nodes. Were you looking for the children method?
okay many thanx for your answers .
i got solved my problem by using the domxml-php4-to-php5.php file i have just uploaded this file and included that file name in my file . And it got worked .
I got that file from this link : http://alexandre.alapetite.fr/doc-alex/domxml-php4-php5/
:)
use the following notation to iterate thru your xml (change 'myfile.xml' and 'tagName'):
<?php
include('simple_html_dom.php');
if (file_exists('myfile.xml')) {
$xml = simplexml_load_file('myfile.xml');
print_r($xml);
foreach( $xml->children() AS $child ) {
$name = $child->getName();
if ($name == 'tagName') {
foreach( $child->children() AS $grandchild ) {
// DO SOMETHING
}
}
}
}
?>
there are other more elegant ways to achieve this, but this is a simple beginner's way to do it. for technical info: http://us2.php.net/manual/en/class.simplexmlelement.php

Categories