json decode transforms Facebook payload - php

Facebook POST request from lead ads test tool is getting transformed on my end:
Facebook lead ad testing tool:
https://developers.facebook.com/tools/lead-ads-testing
Code to accept payload:
$input = json_decode(file_get_contents('php://input'), true);
Framework: CakePHP 2.0
Debug:
2017-07-07 12:12:27 Debug: Array
(
[entry] => Array
(
[0] => Array
(
[changes] => Array
(
[0] => Array
(
[field] => leadgen
[value] => Array
(
[ad_id] => 0
[form_id] => 7.2426956441826E+14
[leadgen_id] => 7.9242177426971E+14
[created_time] => 1499447542
[page_id] => 4.8242822517084E+14
[adgroup_id] => 0
)
)
)
[id] => 482428225170841
[time] => 1499447543
)
)
[object] => page
)
PHP versions: 5.5.9
The problem:
[form_id] => 7.2426956441826E+14
[leadgen_id] => 7.9242177426971E+14
When it should be:
[form_id] => 724269564418262
[leadgen_id] => 792421774269707
So it seems to be converting those numbers for some reason.
I have a clone of this app on an older version of PHP ( 5.4 ) and dont seems to be having this issue there.

I suspect you may need to set the 'options' of json_decode to use JSON_BIGINT_AS_STRING.
Check out http://php.net/manual/en/function.json-decode.php
And this post led me to the possible solution PHP JSON large integers

Related

Getting array key(protected)-value from an array inside object (response from RTM-php)?

When using php library for RTM (https://github.com/bartosz-maciaszek/php-rtm), I am getting a response for a particular tasks-list like this:
Rtm\DataContainer Object
(
[attributes:Rtm\DataContainer:private] => Array
(
[0] => Rtm\DataContainer Object
(
[attributes:Rtm\DataContainer:private] => Array
(
[id] => 19594773
[taskseries] => Rtm\DataContainer Object
(
[attributes:Rtm\DataContainer:private] => Array
(
[id] => 310899576
[created] => 2013-10-03T05:35:52Z
[modified] => 2013-11-06T17:24:36Z
[name] => A new task
[source] => js
[url] =>
[location_id] =>
)
)
)
)
)
)
I want to get the value of [name]. How can I do it?
As far as I could understand from the docs, you have to call something like
$receivedObject->getTaskSeries()->getName()
Or there is a suggestion to apply toArray or toJson to returned object - that should work.

getting story tags from graph api returns strange id results for the tagged

The best way for me to explain this is to show you. Seems like a float() error in a 64bit system.
when i call /anotherfeed/feed or any page for that matter, posts with story_tags return some of the id's as a float error.
sample story tag with float error in id. [id] => 1.7153566624E+14
My question is, how do i fix this, or what am i doing wrong? all i am doing is looping in a foreach statement.
if($fvalue[story_tags]){
echo 'Tags: ';
$sTags=$fvalue[story_tags];
foreach ($sTags as $skey=>$svalue){
foreach ($svalue as $gkey=>$hvalue){
$id=$hvalue[id];
echo ''.$hvalue[name].' '.$id.' ';
}
}
}
[story_tags] => Array
(
[0] => Array
(
[0] => Array
(
[id] => 1.7153566624E+14
[name] => Another Feed
[offset] => 0
[length] => 12
[type] => page
)
)
Array
(
[data] => Array
(
[0] => Array
(
[id] => 171535666239724_156133294510726
[from] => Array
(
[name] => Another Feed
[category] => App page
[id] => 171535666239724
)
[story] => Another Feed shared Non-Profits on Facebook's photo.
[story_tags] => Array
(
[0] => Array
(
[0] => Array
(
[id] => 1.7153566624E+14
[name] => Another Feed
[offset] => 0
[length] => 12
[type] => page
)
)
[20] => Array
(
[0] => Array
(
[id] => 41130665917
[name] => Non-Profits on Facebook
[offset] => 20
[length] => 23
[type] => page
)
)
)
[picture] => http://photos-d.ak.fbcdn.net/hphotos-ak-ash3/557037_10150932300320918_1908237167_s.jpg
[link] => http://www.facebook.com/photo.php?fbid=10150932300320918&set=a.85612830917.95996.41130665917&type=1
[name] => Wall Photos
[caption] => Have you heard of the #[159208207468539:274:One Day without Shoes] (ODWS) campaign? ODWS is an annual initiative by #[8416861761:274:TOMS] to bring awareness around the impact a pair of shoes can have on a child's life.
During the 2012 campaign, #[20531316728:274:Facebook] drove 20% of traffic to the ODWS microsite and TOMS even launched a Facebook-exclusive "Barefoot & Blue" giveaway with #[25266987484:274:Essie Nail Polish] for the second year in a row.
We think this is a pretty cool example of creating exclusive content around an important initiative that keeps people engaged and involved!
[properties] => Array
(
[0] => Array
(
[name] => By
[text] => Non-Profits on Facebook
[href] => http://www.facebook.com/nonprofits
)
)
[icon] => http://static.ak.fbcdn.net/rsrc.php/v2/yD/r/aS8ecmYRys0.gif
[type] => photo
[object_id] => 10150932300320918
[application] => Array
(
[name] => Photos
[id] => 2305272732
)
[created_time] => 2012-07-02T17:57:23+0000
[updated_time] => 2012-07-02T17:57:23+0000
[comments] => Array
(
[count] => 0
)
)
solution:
cURL - had to use number format with PHP_EOL to solve in cURL.
// $locs = curl call to graph api for /anotherfeed/feed, still need solution for foreach.
$locs=json_decode($returned, true);
$stId=number_format($locs[data][1][story_tags][0][0][id], 0, '', '').PHP_EOL;
echo $stId;
PHP-SDK
solution is same, long numbers in the foreach loop need to be ran through number_format.

PDOStatement::getColumnMeta returns original table name instead of view name

I am using the CakePHP framework. When returning the results of a query, the framework calls the "experimental" PDOStatement::getColumnMeta to "arrayify" the data when it comes back from the database. However, there are mixed results depending on the query.
There are times when the array of data comes back as expected where all columns are associated to the name of the view. Other times, the data comes back mixed, where some of the data sits in an array associated with the original table that corresponds to the view.
// correct
Array(
[MyInstall] => Array
(
[id] => a6d1342a-7b4d-11e1-8397-60195b7d6275
[user_id] => dc038c9e-7b4b-11e1-8397-60195b7d6275
[script_id] => 057de1e0-7b48-11e1-8397-60195b7d6275
[path] =>
[url] =>
[created] => 2009-06-15 12:43:30
[version] => 3.2.1
[admin_url] => wp-admin
[name] => WordPress
[icon] => icon_WordPress.gif
)
)
//incorrect
Array(
[MyInstall] => Array
(
[id] => c71a2368-7b4d-11e1-8397-60195b7d6275
[user_id] => dc038c9e-7b4b-11e1-8397-60195b7d6275
[path] =>
[url] =>
[created] => 2011-11-07 22:26:38
[version] => 3.2.1
[admin_url] => wp-admin
)
[Script] => Array
(
[script_id] => 057de1e0-7b48-11e1-8397-60195b7d6275
[name] => WordPress
[icon] => icon_WordPress.gif
)
)
The way the results are built is from the results of the PDOStatment::getColumnMeta. Here is what a sample result of getColumnMeta looks like:
Array
(
[native_type] => STRING
[pdo_type] => 2
[flags] => Array
(
[0] => not_null
)
[table] => MyInstall
[name] => id
[len] => 108
[precision] => 0
)
Any suggestions on how I can get this same information using PDO for MySQL? Or is there another solution to this problem?
BTW: I already filed a bug with the PHP folks on this.
As it turns out, this is a now known bug in MySQL: http://bugs.mysql.com/bug.php?id=66794, still pending at the time of writing.

Wikipedia API returns only a tiny set of data?

Hey there,
I'm trying to extract data from Wikipedia articles using its API (http://en.wikipedia.org/w/api.php) from a PHP script, but I always only seem to get a fraction of the real content.
For example, when trying :
$page=get_web_page("http://en.wikipedia.org/w/api.php?action=query&titles=Cat&prop=links&format=txt");
echo $page["content"];
This is what I get :
Array ( [query] => Array ( [pages] => Array ( [6678] => Array ( [pageid] => 6678 [ns] => 0 [title] => Cat [links] => Array ( [0] => Array ( [ns] => 0 [title] => 10th edition of Systema Naturae ) [1] => Array ( [ns] => 0 [title] => 3-mercapto-3-methylbutan-1-ol ) [2] => Array ( [ns] => 0 [title] => Abyssinian (cat) ) [3] => Array ( [ns] => 0 [title] => Actinidia polygama ) [4] => Array ( [ns] => 0 [title] => Adaptive radiation ) [5] => Array ( [ns] => 0 [title] => African Wildcat ) [6] => Array ( [ns] => 0 [title] => African wildcat ) [7] => Array ( [ns] => 0 [title] => Afro-Asiatic languages ) [8] => Array ( [ns] => 0 [title] => Age of Discovery ) [9] => Array ( [ns] => 0 [title] => Agouti signalling peptide ) ) ) ) ) [query-continue] => Array ( [links] => Array ( [plcontinue] => 6678|0|Albino ) ) )
I was requesting the full list of links on the "Cat" article, but I only seem to get the first 10 in alphabetic order.
This happens no matter the format I choose and even from the API itself (see http://en.wikipedia.org/w/api.php?action=query&titles=Cat&prop=links).
What is causing this restriction, and how can I fix it ?
If you look at the API manual, you will see that there is a pllimit option, which specifies how many links you want to be sent. You can get 500, or 5000 if you have a bot account, at one time.
You will see at the end of the data dump you provided the following: [plcontinue] => 6678|0|Albino ). You can provide this information to the server and get back more links from the page, starting from that point. So the next query you make would be
$page=get_web_page("http://en.wikipedia.org/w/api.php?action=query&titles=Cat&prop=links&format=txt&plcontinue=6678|0|Albino");
You will need to keep doing this until the server does not return a plcontinue value.

Odd behavior of json_decode()

Take the following JSON string (generated by some ExtJS code - but that's irrelevant):
[{"action":"Setting","method":"toggle","data":["welcome-home"],"type":"rpc","tid":2},{"action":"ContentExtFeFillout","method":"todo","data":[true,0,8,false],"type":"rpc","tid":3}]
being sent to a server as a POST request and retrieved via $GLOBALS['HTTP_RAW_POST_DATA'].
Running
json_decode($GLOBALS['HTTP_RAW_POST_DATA']);
on our development machine (5.2.10-2ubuntu6.4 with Suhosin Patch 0.9.7) gives a correct print_r() output of:
Array
(
[0] => stdClass Object
(
[action] => Setting
[method] => toggle
[data] => Array
(
[0] => welcome-home
)
[type] => rpc
[tid] => 2
)
[1] => stdClass Object
(
[action] => ContentExtFeFillout
[method] => todo
[data] => Array
(
[0] => 1
[1] => 0
[2] => 8
[3] =>
)
[type] => rpc
[tid] => 3
)
)
Running the same code on a client's production machine (5.2.5 with Suhosin Patch 0.9.6.2 and Zend Optimizer; SUSE Linux by the way) gives the following print_r() output:
Array
(
[0] => stdClass Object
(
[action] => Setting
[method] => toggle
[data] => Array
(
[0] => welcome-home
)
[type] => rpc
)
[1] => 2
[2] => stdClass Object
(
[action] => ContentExtFeFillout
[method] => todo
[data] => Array
(
[0] => 1
[1] => 0
[2] => 8
[3] =>
)
[type] => rpc
)
[3] => 3
)
Note the missing tid property which obviously has been moved into the main array as an own value - this naturally breaks all the following code.
We also downloaded a Windows PHP version 5.2.5 to check if there's a bug in json_decode() but we get the correct output here.
Are there any known issues with json_decode() at all that could cause this odd behavior?
We're currently totally clueless...
Thanks to all of you!
Best regards
Stefan
OK guys - problem solved. Lacking any more options we persuaded the client to update the installed PHP version and guess what: it works.
There seems to have been a subtle bug in their PHP installation (PHP, Zend Optimizer and/or Suhosin) which has been fixed with the update. Still, a quite weird thing.
Thanks to all of you!
Best regards
Stefan
Have you tried swapping the tid and type keys? Also how about using a hardcoded variable to check if the problem could be with $GLOBALS['HTTP_RAW_POST_DATA']?
Try the following:
$t1='[{"action":"Setting","method":"toggle","data":["welcome-home"],"type":"rpc","tid":2},{"action":"ContentExtFeFillout","method":"todo","data":[true,0,8,false],"tid":2,"type":"rpc"}]';
print_r(json_decode($t1));

Categories