Working with Delicious API json response - php

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.

Related

Having trouble extracting data from PDO array

It might be the late hour or user error but I'm having trouble extracting variables from a PDO, where it would normally work. When I print_r my results by doing $array->results(), I get the following line:
Array ( [0] => stdClass Object ( [messageid] => 1 [fromid] => 2 [toid] => 1 [message] => Hello! ) )
However, on other projects when working with other people I have then just referred to the variable like this:
$result['message']
This is obviously after the line
$result = $array->results();
I've been troubleshooting and come to no conclusions, but I'm terrible at managing arrays so it could be something super simple. Any ideas? Thanks!
Since the result seems objects within array, so get your array and access object(s) inside it, to get message object from your $result, just do:
echo $result[0]->message;

Converting indexed array to normal or simple array

I am trying to convert indexed array to normal array. Basically what get is:
Array ( [0] => 14 [1] => 19 [2] => 20 )
And I need is:
Array(14,19,20);
I tried over Google but not information found. I think this kind of function isn't available in PHP, is there any? please let me know!
Thanks,
Asif
You're chasing shadows:
Both of the arrays you've shown are equal.
There is no such thing as an unindexed array in PHP.
But if you really want to be sure, use $newArray = array_values($array)

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.

Parsing php array in python

I'm getting a PHP array from a web page (as a string).
It looks like :
Array
(
[k1] => Array
(
[a] => Array
(
[id] => 1
[age] => 60
)
[b] => Array
(
[id] => 2
[age] => 30
)
)
[k2] => v2
)
I want to parse it in python.
Does anyone have an idea how to do this?
Thanks,
Rivka
Edit:
This is really not a json, like a few people commented.
Thanks for the comments, and I updated the question.
That's not JSON, that's just how PHP prints arrays. If you want to create JSON of the array, check out json_encode for PHP. Then use Python's JSON library (or here for py3) to read it.
If I understood you correctly, you are using print_r on array to get that output. This is a visual representation of array only, you can't really parse it. For example:
array('Array'.PHP_EOL."\t(".PHP_EOL." [0] => test".PHP_EOL."\t)")
will look exactly like
array(array('test'));
You should use some real serializing function to do what you want(json,serialize etc.);

Categories