escape 'value' in OO PHP - php

I was not sure how to title this question feel free to re-word it...
I am dealing with a SOAP response that has an object by the name of 'value'(I know... not smart it is third party so I cannot change it!)
$application = $result->return[$i]->extendedist[12]->value->displayValue;
[value] => stdClass Object
(
[displayValue] =>
[internal] =>
[id] =>
)
As you can see this is a problem because PHP wants to use its built in value function. How can I escape this and use the value object from the response?

$result->{'return'}
Should work.
Depending on how you read the response you could decode it into an array instead and use
$result['return']

You can access an object property such as $foo->value as $foo->{'value'} as well.

Related

PHP: why do arrays with one element get treated as objects when consuming an ASMX function? [duplicate]

I am fetching some data using SoapClient. I get this resuls from one of the calls:
stdClass Object
(
[payTransIncome] => stdClass Object
(
[item] => stdClass Object
(
[payTransId] => 141281
[payTransItId] => 630260
[payTransBuyerId] => 1311
)
)
)
However the docs of this WebAPI say payTransIncome is an array.
Seems to me SoapClient found a one element array and converted it to a single stdClass object. And this makes it harder to parse because sometimes I think it might actually return more then 1 element.
Sure I can put everywhere checks if (is_array()) but maybe there is a simple, more elegant way?
Please try to set features to SOAP_SINGLE_ELEMENT_ARRAYS in your SoapClient options:
$client = new SoapClient("some.wsdl", ['features' => SOAP_SINGLE_ELEMENT_ARRAYS]);
In older versions of PHP, SoapClient may ignore the "features" option.
That was my case using PHP 5.3.
But you can always cast an object of stdClass to array:
$client = new SoapClient("some.wsdl");
$objResult = $client->__soapCall("someFunction");
$objArray = (array)$objResult;

How to access properties of a nested PHP object?

I'm trying to do an web app which people can see their status in League of Legends, but I don't even know how to do some things. I've got this class:
stdClass Object
(
[player] => stdClass Object
(
[id] => xxxxxx
[name] => yyyy
[profileIconId] => 627
[summonerLevel] => 30
[revisionDate] => 1422798145000
)
)
and im using this php code:
<?php
$summoner = 'yohanbdo';
$summonerdata = $leagueclass->getsummoner($summoner);
?>
I want to take only the id, name and profileIconId and show it. I don't know how to do this.
PD: My english isn't that good so thanks you all for the edits.
Weird, I was just looking that Riot API a little while ago.
I feel like you're pretty new to this type of notation, so I'll try and be quick but concise with my explanation.
What you have there, as Gerifield stated, are objects. You access their properties by using the -> operator. For example, if I assume that the object $main is what you're var_dumping out, then you could simply get the objects like so:
$main = json_decode($some_json_string);
//Now that we have the object set, we can deal with the properties.
echo $main->player->name;
//This will output the player name.
echo $main->player->id;
//Will output the player ID.
Notice in each case that since the player key of the $main object is also an object, it's properties must be accessed via the -> operator.
However, you could also simply use associative arrays by passing the second parameter to json_decode, like so:
$main = json_decode($some_json_string,TRUE);
echo $main['player']['id'];
echo $main['player']['name'];
Hopefully this helps.

Pulling apart stdclass with PHP

I'm trying to access part of an StdClass that has a property #text. PHP uses '#' for comments so I'm having trouble getting PHP to parse it not as a comment. An example of the StdClass is below
stdClass Object
(
[artist] => stdClass Object
(
[name] => John Denver
[mbid] => 34e10b51-b5c6-4bc1-b70e-f05f141eda1e
[url] => http://www.last.fm/music/John+Denver
[image] => Array
(
[0] => stdClass Object
(
[#text] => http://userserve-ak.last.fm/serve/34/521025.jpg
[size] => small
and I've tried to access it with:
$json->artist->image[0]->#text
but how can I escape the '#' or tell php to interpret it differently. Or is there another format to find the #text property.
I also tried:
$json['artist']['image'][0]['#text']
but I get an error. I'm sure this is something simple but it's really got me at the moment.
You can access such properties with the following code:
$object->{'#text'}
It seems to be JSON data, so when decoding, you can also set the second parameter to true:
$json = json_decode($input, true);
Now you are able to use this:
$json['artist']['image'][0]['#text']
Citing another answer you can put the key in a variable an accessing it via this variable.
$var='myvarwithstrangecharacters$asdfas#';
$object->$var;//do whatever you want
from:
How do I access a PHP object attribute having a dollar sign?

TrialPay sending malformed JSON on callback

I am attempting to implement TrialPay/Offerwall/Dealspot on a Facebook app. In their documentation they give an example of what the JSON looks like that they send you:
{"order_id":9006316682257,"buyer":409697,"app":107032282669135,"receiver":409697,
"amount":1,"time_placed":1322622026,"update_time":1322622027,"data":"",
"items":[{"item_id":"0","title":"3 Fred Currency","description":"Make it rain!",
"image_url":"http:\/\/external.ak.fbcdn.net\/safe_image.php?d=AQDldsPcWsejAJdC&url=http\u00253A\u00252F\u00252Fwww.etftrends.com\u00252Fwp-content\u00252Fuploads\u00252F2011\u00252F10\u00252Fcurrency-trading.jpg",
"product_url":"","price":1,"data":"{\"modified\":{\"product\":\"URL_TO_APP_CURR_WEBPAGE\",
\"product_title\":\"Fred Currency\",\"product_amount\":3,\"credits_amount\":1}}"}],"status":"placed"}
They say if you json_decode it as an array you should get this:
Array (
[order_id] => 9006316682257
[buyer] => 409697
[app] => 107032282669135
[receiver] => 409697
[amount] => 1
[time_placed] => 1322622026
[update_time] => 1322622027
[data] =>
[items] => Array (
[0] => Array (
[item_id] => 0
[title] => 3 Fred Currency
[description] => Make it rain!
[image_url] => http://external.ak.fbcdn.net/safe_image.php?d=AQDldsPcWsejAJdC&url=http%3A%2F%2Fwww.etftrends.com%2Fwp-content%2Fuploads%2F2011%2F10%2Fcurrency-trading.jpg
[product_url] =>
[price] => 1
[data] => {"modified":{"product":"URL_TO_APP_CURR_WEBPAGE","product_title":"Fred Currency","product_amount":3,"credits_amount":1}}
)
)
[status] => placed
)
It doesn't though, data actually looks like this:
[data] => "{"modified":{"product":"URL_TO_APP_CURR_WEBPAGE","product_title":"Fred Currency","product_amount":3,"credits_amount":1}}"
The JSON being inside the string is causing it to be invalid JSON. Is there is a straightforward way to remove those quotes?
First off, it looks like you need to finish configuring your app on Trialpay's site, hence the URL_TO_APP_CURR_WEBPAGE. The issue here may be that you have not completed your app configuration to the extent needed to produce valid JSON.
If that's not the answer however, if you still get invalid JSON (which I agree, that's invalid) I would suggest contacting your Trialpay representative. They're usually pretty responsive and we did unearth a few issues w/ their product during our game development.
Good luck - post back here if/when you find more info.
Cheers
Developer at TrialPay here. We might have a typo in our doc sites, and I'll send a note around to double-check that.
In the meantime, I've verified that the actual JSON that Facebook is passing to the server-side callback on completion of an offer-based order for in-app currency should be valid, and decodes properly to the desired result above.
If you encounter any further problems outside the scope of this thread, feel free to ping me directly.
Edit:
After copying your code and validating against JSONLint, I encountered a problem right away at the point you mentioned. However, after removing the bad line break before \"product_title\", I was able to validate correctly. Example PHP snippet included below:
<?php
$order_details = '{"order_id":9006316682257,"buyer":409697,"app":107032282669135,"receiver":409697,"amount":1,"time_placed":1322622026,"update_time":1322622027,"data":"","items":[{"item_id":"0","title":"3 Fred Currency","description":"Make it rain!","image_url":"http:\/\/external.ak.fbcdn.net\/safe_image.php?d=AQDldsPcWsejAJdC&url=http\u00253A\u00252F\u00252Fwww.etftrends.com\u00252Fwp-content\u00252Fuploads\u00252F2011\u00252F10\u00252Fcurrency-trading.jpg","product_url":"","price":1,"data":"{\"modified\":{\"product\":\"URL_TO_APP_CURR_WEBPAGE\",\"product_title\":\"Fred Currency\",\"product_amount\":3,\"credits_amount\":1}}"}],"status":"placed"}';
$order_details_decoded = json_decode($order_details, true);
$order_details_decoded['items'][0]['data'] = json_decode($order_details_decoded['items'][0]['data'], true);
print_r($order_details_decoded);
As I mentioned early, if anything else comes up outside the scope of this thread, feel free to ping me directly.
Did you try json_decode($json_string, true); that will convert it into an associative array.

Working with Delicious API json response

When I try to find out the total count of delicious bookmarks, Delicious returns a json file which when decoded is:
Array (
[0] => stdClass Object (
[hash] => e60558db2d649c8a1933d50f9e5b199a
[title] => ISRAEL: Thousands march in Jerusalem rally, Israel still kicking
new families out of land they've owned for 60+ years
[url] => http://english.aljazeera.net/news/middleeast/2010/03/2010362141312196.html
[total_posts] => 2
[top_tags] => Array ()
)
)
The array is a stdClass Object. How can I extract [total_count] using PHP.
P.S Since I could not figure out how to extract it, I am using strpos to find 'total_count' and then I am cutting the string from that position and then extracting integers out of it. :)
BUt it is way too long.
Solved
If you pass a second argument, true, in your json_decode, it will return a regular array as opposed to an object. You might find this easier to work with.
$array = json_decode($json, true);
If you pass a second argument, true, in your json_decode, it will return a regular array as opposed to an object. You might find this easier to work with.
$array = json_decode($json, true);
Have you tried
echo $stdClass->total_posts;
Do you mean [total_posts]? If so, you should use $delArray[0]->total_posts.
See http://us.php.net/manual/en/language.types.object.php for samples on accessing object properties.

Categories