Trying to get property of non-object with external json file - php

This is my JSON file which I get from an external link:
[
{
"Id":441,
"Name":"Gary"
},
{
"Id":1864,
"Name":"Bob"
}
]
When I try and display the Id and Name, I receive the error:
Notice: Trying to get property of non-object
$file = file_get_contents('http://linktojson.com');
$decode = json_decode($file, false);
$name = $decode->Name;
$id = $decode->Id;
echo $name;
echo $id;

Your json data has nested objects. So you need to access it like this:
$decode[0]->Name;
See here: https://3v4l.org/2aY22
Seeing as you have multiple objects with the same structure, you probably want to loop over them, like this:
foreach($decode AS $person) {
echo $person->Id . ": " . $person->Name;
}
Side note: it really helps to examine your data structure if you're having trouble navigating it. Just doing a var_dump($decode); shows you quite clearly how it is structured and how you need to access it!

Related

PHP funcions and Webservice returns

When I make a request to my webservice one time I get:
funcion login returns => {
"token":"fsuehfisubfibiefasasdapmwineq","client":"Admin","permission":"ADMIN"
}
And I can just save something like the client or the token in a variable, doing for example:
$json = login($email,$password); - saving the JSON in the variable $json.
$values = json_decode($json); - decoding the $json and saving it on $values.
And know if I want to save the name or the token for future requests to the webservice I make a variable:
$token = $values->{'token'};
$client = $values->{'client'};
But my question is what i do when i get this from the webservice, because I did the same and tried somethings and I'm not getting anywhere.
funcion member returns = {
"Table":[
{
"client":"Admin",
"name":"Martin Lupin",
"age":"25",
"city":"Lisbon"
}
]
}
How do I access to "client", "name", "age", "city"?
I tried to do:
$name = $valuesclient->{'client'}; - and nothing
$name = $valuesclient->{'table'}->{'client'}; - and nothing
$name = $valuesclient->{'table'->{'client'}}; - and nothing
Can someone help me?

Converting a string stored in JSON array to variable name in PHP

I have created a JSON file which I will use as a datasource for global configurations of the app.
Excerpt from json file
//Have not put the complete json file. No error in the file
{
"loginType":[
{
"name":"Facebook",
"url":"#",
"method":"",
"label":"Continue with Facebook",
"type":"social",
"class":"",
"icon":"",
"callBack_url" : "fbLoginUrl",
"providerButton":"<div class='fb-login-button' data-max-rows='1'
data-size='large' data-button-type='continue_with' data-use-
continue-as='true'></div>"
},
{
"name":"Twitter",
"url":"#",
"method":"logInWithTwitter()",
"label":"Continue with Twitter",
"type":"social",
"class":"",
"icon":"",
"callBack_url" : "twitterLoginUrl",
"providerButton" :""
}
]
}
The callBack_url key in the json file has a variable with a similar name which has a url as its value e.g $twitterLoginUrl = "https://some.site.com/twitter_login?param1"
$jsonData_signIn =json_decode
(file_get_contents(/path/to/oauth2_provider.json));
$oauth2Provider = jsonData_signIn->loginType;
foreach($oauth2Provider as $type){
if($type->type == 'local' ){
echo "{$type->label}";
}
}
For the above, as output for the link I get eg Continue with facebook
echo "{$type->label}";
The reason I am not storing the complete URI is I will generate some parameters dynamically.
Take a look at the manual for variable variables: http://php.net/manual/en/language.variables.variable.php
You basically just wrap the string variable name in ${ } to make it behave like an actual variable.
$fbLoginUrl = 'https://www.facebook.com/v2.10/dialog/oauth?client_id=xxxxxxx&state=xxxxxxx&response_type=code&sdk=php-sdk-5.6.2&redirect_uri=some.site.com/fbLogin.php&scope=public_profile';
$json = '{"callBack_url" : "fbLoginUrl"}';
$decoded = json_decode($json);
echo ${$decoded->callBack_url};

Twitter Oauth Api - Trying to get property of non-object

I'm trying to access "screen_name" data sent by oauth api. I'm getting "Trying to get property of non-object" error.
My current php code is this;
foreach($multi_responses as $response){
echo $response->user->screen_name;
}
This is the data sent by oauth:
http://pastebin.com/SJbmYggQ
As I see, you have got an array as a response, which is contains JSON strings. Try to parse JSON strings into an array or an object. If your $multi_responses variable in the foreach is fully contains the content of your linked file.
Array version:
<?php
foreach($multi_responses as $response){
$jsonDecodeArray = json_decode($response, true);
if( isset($jsonDecodeArray['user']['screen_name']) ){
echo $jsonDecodeArray['user']['screen_name'];
}
}
?>
Object version:
<?php
foreach($multi_responses as $response){
$jsonDecodeOBJ = json_decode($response);
if( isset($jsonDecodeOBJ->user->screen_name) ){
echo $jsonDecodeOBJ->user->screen_name;
}
}
?>

Post 1:N (one customer many phonenumbers) JSON Array to android and parse it in PHP

I want to create a more complex JSON Array where a customer (which has a name) has many phonenumbers so that i can parse it in PHP and i need your help.
i.e.:
public Class ContactVO
{
public String diplayname;
public ArrayList<PhoneVO> phonenumbers = new ArrayList<PhoneVO>();
}
public Class PhoneVO
{
public String number;
}
Can s.o. give me an example how to create the above 1:N structure as JSON Array and how to parse it via PHP?
I put everything in a ArrayList and added the GSON library to may project.
The result is:
[
{"contact_id":"1","displayname":"Bjyyyyy","phonenumbers":[{"number":"066-6228"}]},
{"contact_id":"2","displayname":"Rainer Unsinn","phonenumbers":[{"number":"(066) 214-52"}]},
{"contact_id":"3","displayname":"Dieter karpenstein","phonenumbers":[{"number":"06621716669"}]},
{"contact_id":"4","displayname":"Sido","phonenumbers":[{"number":"(085) 011-1555"}]},
{"contact_id":"5","displayname":"Jochen Müller","phonenumbers":[{"number":"01773313261"}]}
]
How should the receiving PHP File lookslike to parse that?
Are you just looking for the json_decode function?
$fromPost = $_POST['contact'];
$object = json_decode($fromPost, true); // Read the doc to decide whether you want the "true" or not
var_dump($object);
Edit:
You could have something like that (not tested)
$string = '[
{"contact_id":"1","displayname":"Bjyyyyy","phonenumbers":[{"number":"066-6228"}]},
{"contact_id":"2","displayname":"Rainer Unsinn","phonenumbers":[{"number":"(066) 214-52"}]},
{"contact_id":"3","displayname":"Dieter karpenstein","phonenumbers":[{"number":"06621716669"}]},
{"contact_id":"4","displayname":"Sido","phonenumbers":[{"number":"(085) 011-1555"}]},
{"contact_id":"5","displayname":"Jochen Müller","phonenumbers":[{"number":"01773313261"}]}
]';
$decoded = json_decode($string);
foreach($decoded as $person) {
echo $person['displayname'] . "\n";
foreach($person['phonenumbers'] as $phone) {
echo $phone['number'] . "\n";
}
}

PHP: Send $_POST data to my Class?

I'm trying to send my $_POST data to a class of mine for processing... but it doesn't seem to matter how I send it, PHP is telling me:
Trying to get property of non-object
Class method:
public function test_me_out($postdata) {
if(isset($postdata->price)) {
return "the price was: " . $postdata->price . " …and this was added.";
} else {
return "it's apparently not set...";
}
}
$_POST is not an object. You should access its information using it as an array like $_POST['my_data'].
You can still do
$postData = new ArrayObject($_POST,ArrayObject::ARRAY_AS_PROPS);
Then $postData->price will work as expected to be.
How are you calling test_me_out? Like this?
test_me_out($_POST)
$_POST is an array, not an object. Therefore you can access the variables in your function like so:
$postdata['price']

Categories