TrialPay sending malformed JSON on callback - php

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.

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;

escape 'value' in OO 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.

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?

Fields get unwantedly concatenated in Salesforce SOQL query result. Developer nearly loses it

I'm probably missing something quite basic, but I'm getting very confused (and frustrated) with the results I get from my SOQL queries to the Salesforce API.
My query:
Select Id, FirstName, LastName FROM contact
The resulting object (as rendered by print_r):
stdClass Object
(
[done] => 1
[queryLocator] =>
[records] => Array
(
[0] => stdClass Object
(
[type] => Contact
[Id] => Array
(
[0] => 0032000000cPd7uAAC
[1] => 0032000000cPd7uAAC
)
[any] => BuzzAldrin
)
[1] => stdClass Object
(
[type] => Contact
[Id] => Array
(
[0] => 0032000000cPt1zABC
[1] => 0032000000cPt1zABC
)
[any] => RonnieVanZant
)
[2] => stdClass Object
(
[type] => Contact
[Id] => Array
(
[0] => 0032000000cPb60AA
[1] => 0032000000cPb60AA
)
[any] => PollyJeanHarvey
)
)
[size] => 3
)
The first thing I don't get is why "Id" is an array. A strange quirk, but a workaround is not too hard.
The second thing bothers me endlessly more, though: I select for FirstName and LastName and what happens is they get concatenated and returned as a single string value for a field called "any". To avoid the "split it on uppercase letters" advice I already got from my colleagues, I provided an example with both a two-capital first name and a two-capital last name, and anyhow, in reality I need many more (and more formally unpredictable) fields, and they all get added to this "any" property.
Does anyone see what I'm doing wrong? Assuming it's not such a badly written API, that is?
Edit:
Said developer will now go sit in a corner for a few hours, repenting for not having checked for more recent versions of PHP Toolkit. Seems I was using 11.0, whereas there's already a version 20.0. Shame on me, shame on me indeed. Sorry for wasting your time.
The behavior you are seeing is mostly because of how PHP's SoapClient interprets the results from the API. If you call getLastResponse() on your API connection after you make the query() calls above, you'll see what the actual SOAP messages look like coming back from Salesforce.
As far as the Id array -- its not really an array, but it is listed twice per record (once for the record itself and once as a field), but PHP turns it into an array because it sees it twice. As far as the any, that's happening because PHP is not understanding the namespaced field tags correctly.
As it looks like you found, using the PHP Toolkit can help with these oddities and return sensible objects for you to work with. You might also want to consider looking at Salesforce's REST API, whose results can be directly consumed by json_decode(). For making the HTTP calls to Salesfore, you might be interested in this simple (almost standalone) REST client in a project of mine.

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