How do I get data out of SimpleDB? All I wanted to do was put data in and then get data out. It looks like the data is not easy to get out and in the end will require computing power to extract by looping etc. Am I correct?
Here is my code to extract the data:
<?php
// Include the SDK
require_once 'sdk.class.php';
// Instantiate
$sdb = new AmazonSDB();
$select_expression = 'SELECT * FROM ' . $domain_name;
$next_token = null;
do {
if ($next_token) {
$response = $sdb->select($select_expression, array(
'NextToken' => $next_token,
));
} else {
$response = $sdb->select($select_expression);
}
// Get Data for row
$body = $response->body->to_array()->getArrayCopy();
echo "ID: " . $msgId . "<br>";
$next_token = isset($response->body->SelectResult->NextToken)
? (string) $response->body->SelectResult->NextToken
: null;
}
while ($next_token);
echo "<br>";
?>
Here is the extract of the data I'm trying to extract.
Array
(
[#attributes] => Array
(
[ns] => http://sdb.amazonaws.com/doc/2009-04-15/
)
[SelectResult] => Array
(
[Item] => Array
(
[0] => Array
(
[Name] => 1
[Attribute] => Array
(
[0] => Array
(
[Name] => msgAddress
[Value] => +2782555555
)
[1] => Array
(
[Name] => msgType
[Value] => S
)
[2] => Array
(
[Name] => msgSubmitDate
[Value] => 2012-09-02 15:48:46
)
[3] => Array
(
[Name] => msgText
[Value] => Test SMS message for ID no 1
)
)
)
[1] => Array
(
[Name] => 2
[Attribute] => Array
(
[0] => Array
(
[Name] => msgAddress
[Value] => +27825555555
)
[1] => Array
(
[Name] => msgType
[Value] => P
)
[2] => Array
(
[Name] => msgSubmitDate
[Value] => 2012-09-02 15:48:46
)
[3] => Array
(
[Name] => msgText
[Value] => Test phone message for ID no 2
)
)
)
[2] => Array
(
[Name] => 3
[Attribute] => Array
(
[0] => Array
(
[Name] => msgAddress
[Value] => name#domain.co.za
)
[1] => Array
(
[Name] => msgType
[Value] => E
)
[2] => Array
(
[Name] => msgSubmitDate
[Value] => 2012-09-02 15:48:46
)
[3] => Array
(
[Name] => msgText
[Value] => Test email message for ID no 3 to name#domain.co.za
)
)
)
[3] => Array
(
[Name] => 4
[Attribute] => Array
(
[0] => Array
(
[Name] => msgAddress
[Value] => andrebruton
)
[1] => Array
(
[Name] => msgType
[Value] => P
)
[2] => Array
(
[Name] => msgSubmitDate
[Value] => 2012-09-02 15:48:46
)
[3] => Array
(
[Name] => msgText
[Value] => Test push notification message for ID no 4
)
)
)
)
)
)
All I want to get is the following variables:
msgId (the Name), msgType, msgAddress, msgText, msgSubmitDate
If I can get it using $msgType = Body->SelectResult->Item->Name or something like that.
Using XPath is going to perform about 100x faster. I haven't tested this code, but the XPath expression should be pretty close:
$msgAddress = (string) $response->body->query('//Attribute[Name[text()="msgAddress"]]/Value')->first();
I figured it out using some old Tarzan code. This works for a 2 dimensional array (data similar to a standard database)
Here is the working code:
<?php
// Include the SDK
require_once 'sdk.class.php';
// Instantiate
$sdb = new AmazonSDB();
$select_expression = 'SELECT * FROM ' . $domain_name;
$next_token = null;
do {
if ($next_token) {
$response = $sdb->select($select_expression, array(
'NextToken' => $next_token,
));
} else {
$response = $sdb->select($select_expression);
}
// Get Data for row
foreach ($response->body->SelectResult->Item as $item)
{
$msgId = $item->Name;
foreach ($item->Attribute as $attr)
{
if ($attr->Name == 'msgAddress') {
$msgAddress = $attr->Value;
}
if ($attr->Name == 'msgType') {
$msgType = $attr->Value;
}
if ($attr->Name == 'msgSubmitDate') {
$msgSubmitDate = $attr->Value;
}
if ($attr->Name == 'msgText') {
$msgText = $attr->Value;
}
}
echo "<br>msgId: $msgId<br>";
echo "msgAddress: $msgAddress<br>";
echo "msgType: $msgType<br>";
echo "msgSubmitDate: $msgSubmitDate<br>";
echo "msgText: $msgText<br><br>";
}
$next_token = isset($response->body->SelectResult->NextToken)
? (string) $response->body->SelectResult->NextToken
: null;
}
while ($next_token);
echo "<br>";
?>
$body = $response->body->to_array()->getArrayCopy();
echo "ID: " . $msgId . "<br>";
//but $msgId is not set
Related
I'm trying to use Facebook Graph API to get comments from the last 2 posts from my accounts
I wrote the code and it works but it only returns the second post (the last array) only
this my code :
$postItems = $postsArray['posts']['data'];
foreach ($postItems as $post) {
$fullPostId = $post['id'];
$comments_number = 10;
$facebook_graph_api2 = "https://graph.facebook.com/v2.4/$fullPostId/comments?access_token=$accToken&pretty=1&summary=true&limit=$comments_number&after";
$jsonData = grab_page($facebook_graph_api2,$accCookie);
$postDataArrays = json_decode($jsonData, true);
$CommentsItems = $postDataArrays['data'];
}
//foreach($CommentItems as $item) {
// echo $users = $item['from']['id'].'\n';
//}
// #$postItems = $postsArray['data'];
/** Update Balance Code **/
$toolId = '87637';
#$itemsCount = #count($CommentItems);
$toolPrice = toolPrice($toolId)*$itemsCount;
$finalBalance = updateBalance($toolPrice);
/** Update Balance Code **/
if ($finalBalance) {
if (isset($CommentItems)) {
echo '{"status":1,"balance":'.$finalBalance.',"message":"Success !","data":"';
foreach($CommentItems as $item) {
echo $users = $item['from']['id'].'\n';
// echo json_encode($users, JSON_FORCE_OBJECT);
// $items[] = $item['from']['id'];
}
// print_r($items);
// echo '{"status":1,"message":"Success !","data":"'.$items.'"}';
echo '"}';
}elseif( isset($postDataArray['error']) && $postDataArray['error']['code']== 1 ){
$msgError = $postDataArray['error']['message'];
echo '{"status":0,"message":"Error !","reason":"'.$msgError.'"}';
}else{
echo '{"status":0,"message":"Error !","reason":"Please Update Your Facebook Access Token"}';
}
}else{
echo '{"status":0,"message":"Error !","reason":"You do not have enough funds on balance"}';
}
The code above returns only last array data not all posts data.
this is an example of $postDataArrays data should be
Array
(
[data] => Array (
[0] => Array
(
[created_time] => 2023-02-08T14:46:12+0000
[from] => Array
(
[name] => Name1
[id] => id1
)
[message] => test Message
[id] => 61xxxxxxxxxxx_12xxxxxxxxxxxx
)
[1] => Array
(
[created_time] => 2023-02-08T14:42:52+0000
[from] => Array
(
[name] => name2
[id] => id2
)
[message] => test Message
[id] => 61xxxxxxxxxx_7xxxxxxxxxxxx
)
)
[paging] => Array
(
[next] => https://graph.facebook.com/v2.4/92xxxxxxxxxxxx_61xxxxxxxxxxxxxxxxxx/comments?access_token=EAAG&pretty=1&summary=true&limit=2&after
)
[summary] => Array
(
[order] => ranked
[total_count] => 58
[can_comment] => 1
)
)
Array
(
[data] => Array
(
[0] => Array
(
[created_time] => 2023-02-07T18:23:40+0000
[from] => Array
(
[name] => name1 from array 2
[id] => id2 from array 2
)
[message] => test Message
[id] => 6xxxxxxxxxxxxxx_xxxxxxxxxxxxxxxx
)
[1] => Array
(
[created_time] => 2023-02-07T18:20:49+0000
[from] => Array
(
[name] => name2 from array 2
[id] => id2 from array 2
)
[message] => test Message
[id] => 6xxxxxxxxxxxxxx_xxxxxxxxxx
)
)
[paging] => Array
(
[next] => https://graph.facebook.com/v2.4/9xxxxxxxxxxxxxxxxxxxxx/comments?access_token=EAAG&pretty=1&summary=true&limit=2&after
)
[summary] => Array
(
[order] => ranked
[total_count] => 121
[can_comment] => 1
)
)
the arrays above should return 4 users but it returns only last 2 users
First i create an array
$responses= array();
And modified the code and put all neded data inside the array
foreach ($postItems as $post) {
$fullPostId = $post['id'];
$comments_number = 3000;
$facebook_graph_api2 = "https://graph.facebook.com/v2.4/$fullPostId/comments?access_token=$accToken&pretty=1&summary=true&limit=$comments_number&after";
$jsonData = grab_page($facebook_graph_api2,$accCookie);
$postDataArray = json_decode($jsonData, true);
$CommentsItems = $postDataArray['data'];
foreach($CommentsItems as $item) {
$users = $item['from']['id'];
$userId['user_id'] = $users;
$responses[] = $userId['user_id'];
}
}
Then , Encoded the the final response to be json , and i decoded it again
$enResponse = json_encode($responses);
$allComments = json_decode($enResponse,true);
To show all data , just loop $allComments
foreach($allComments as $item) {
echo $users = $item.'\n';
}
How can I create a function to display an array as a tree. For example I want to obtain a decision tree which I want to walk until I get to the leafs based on the branch's values. I create the tree like bellow:
$tree= new DS_Tree();
$node=array('name' => 'start');
$tree->insert_node($node);
$tree->goto_root();
$mytree = new id3();
$mytree->init($data_array_AttrList,$data_array_values,$data_class,$data_array_instances,$tree);
$mytree->run();
echo '<pre class="brush: php">';
print_r($mytree->tree->draw_tree());
echo '</pre>';
The function draw_tree() is:
public function draw_tree() {
return $this->nodes;
}
The function that creates my tree is:
private function make_tree($attr) {
foreach($this->Values[$attr] as $v) {
$subset = $this->get_subset($attr, $v);
if($the_class = $this->has_same_class($subset)) {
$node =array(
'name' => $attr,
'arc' => $v
);
$this->tree->insert_node($node);
$this->Instance = array_diff_key($this->Instance, $subset);
} else {
$node =array(
'name' => $this->Classa,
'arc' => $v
);
$unresolved = $this->tree->insert_node($node);
}
}
if (isset($unresolved)) {
$this->tree->goto_index($unresolved);
}
}
}
The result is:
Array
(
[0] => Array
(
[name] => Time
[parent] =>
[children] => Array
(
[0] => 1
)
)
[1] => Array
(
[name] => Focus
[arc] => Array
(
[0] => 2 day/week
[1] => 3 day/week
[2] => 4 day/week
[3] => 5 day/week
[4] => 6 day/week
)
[parent] => 0
[children] => Array
(
[0] => 2
)
)
[2] => Array
(
[name] => Dificulty
[arc] => Array
(
[0] => Weght loss
[1] => Mantain weight
[2] => Gain Mass
)
[parent] => 1
[children] => Array
(
[0] => 3
)
)
[3] => Array
(
[name] => Sex
[arc] => Array
(
[0] => Beginner
[1] => Intermediar
[2] => Advance
)
[parent] => 2
[children] => Array
(
[0] => 4
)
)
[4] => Array
(
[name] => Array
(
[Exercise] => Array
(
[0] => Array
(
[0] => Ex1
[1] => Ex2
[2] => Ex3
[3] => Ex4
[4] => Ex5
)
)
)
[arc] => Array
(
[0] => F
[1] => M
)
[parent] => 3
)
)
Just to display an array as a tree:
echo "<pre>";
var_dump($array);
echo "</pre>";
Here is a way to iterate through this data structure and look for a certain value:
function recurseFind($tree, $find, $path = "") {
if (!is_array($tree)) {
// it is a leaf:
if ($tree === $find) {
return $path; // return path where we found it
}
return false;
}
foreach($tree as $key => $value) {
$result = recurseFind($value, $find, $path . (is_numeric($key) ? "[$key]" : "['$key']"));
if ($result !== false) return $result;
}
return false;
}
For the sample input you provided, if you would call it like this:
echo recurseFind($tree, "Mantain weight", '$tree');
Outputs the "location" of that value (first match only) in a format that can be evaluated in PHP:
$tree[2]['arc'][1]
Hey all i am trying to get all the likes for a wall post in PHP. The array frame looks like this:
Array (
[data] => Array (
[0] => Array (
[id] => XXXXXXXX_XXXXXXXXXXX
[from] => Array (
[name] => Bob Barker
[id] => XXXXXXXXXXX
)
[message] => This is a message here!!!
[story] => We shared a message with others
[story_tags] => Array (
[0] => Array (
[0] => Array (
[id] => XXXXXXXXXX
[name] => Bob Barker
[offset] => 0
[length] => 11
[type] => user
)
)
[19] => Array (
[0] => Array (
[id] => XXXXXXXXXXXXXXXXXXXXXXX
[name] => NAME
[offset] => 19
[length] => 5
[type] => page
)
)
)
[picture] => [removed]
[link] => [removed]
[name] => Timeline Photos
[caption] => This is just a caption for the post here
[properties] => Array (
[0] => Array (
[name] => By
[text] => NAME
[href] => [removed]
)
)
[icon] => https://fbstatic-a.akamaihd.net/rsrc.php/v2/yD/r/aS8ecmYRys0.gif
[actions] => Array (
[0] => Array (
[name] => Like
[link] => [removed]
)
)
[privacy] => Array (
[value] =>
)
[type] => photo
[status_type] => shared_story
[object_id] => XXXXXXXXXXXXXXXX
[application] => Array (
[name] => Photos
[id] => XXXXXXXXXX
)
[created_time] => 2014-02-12T21:33:17+0000
[updated_time] => 2014-02-12T21:33:17+0000
[likes] => Array (
[data] => Array (
[0] => Array (
[id] => XXXXXXXXXXXX
[name] => John Doe
)
[1] => Array (
[id] => XXXXXXXXXXXX
[name] => Steve Doe
)
)
[paging] => Array (
[cursors] => Array (
[after] => XXXXXXXXXXXXXXXX
[before] => XXXXXXXXXXXX==
)
)
)
)
My current PHP code is:
$feed = $facebook->api('/me/home'); //This is the array above
foreach($feed['data'] as $post) {
$story = (isset($post['story']) ? $post['story'] : null);
$id = (isset($post['id']) ? $post['id'] : null);
$name = (isset($post['name']) ? $post['name'] : null);
$message = (isset($post['message']) ? $post['message'] : null);
$post_link = (isset($post['actions'][0]['link']) ? $post['actions'][0]['link'] : null);
$picture = (isset($post['picture']) ? $post['picture'] : null);
foreach($post['likes'] as $liked) {
echo $post['id'];
}
echo $name . ' ' . $story . ' it was: ' . $message . ' and ' . $post_link . ' and <img src="' . $picture . '" /><br />';
echo '=======================================================================================================';
}
The
foreach($post['likes'] as $liked) {
echo $post['id'];
}
doesnt seem to be working as-is. What am i missing in order to make it loop through all the "liked" posts in the array?
Yes
foreach($post['likes'] as $liked) {
echo $post['id'];
}
will not work since
$post['likes'] elements are again a nested array as
likes => array => data => array=> ..
So you need to do the loop as
if(array_key_exists('data',$post['likes'])){
foreach($post['likes']['data'] as $liked=>$val) {
echo '<br />Like ID: ' .$val["id"].'<br />' ;
}
}
It is always better to do a check if the array key exists before doing the loop, so that if the returned data does not contain the key you do not get undefined index error.
Try
foreach($post['likes']->data as $like) {
echo $like->id;
}
I have a facebook array and I am trying to echo out the names and ID.
All I have is this array: $friends_list.
When I use this code:
print_r($friends_list);
Then the following comes out below: But how do I loop thru these? Or what if I just wanted to find out how many names there are or just call on one var by id. In short how do I echo out $friends_list which is a multiD array?
Array ( [data] => Array ( [0] => Array ( [name] => Wendy Cukierski [id] => 524320383 ) [1] => Array ( [name] => Leticia Mercado Correa [id] => 537763225 ) [2] => Array ( [name] => Olivia Urbina [id] => 538711855 ) [3] => Array ( [name] => Ruth Albrecht [id] => 541610111 ) [4] => Array ( [name] => Josh Brahm [id] => 577546485 ) [5] => Array ( [name] => Kim Yu [id] => 583515871 ) [6] => Array ( [name] => SisterTracey Dugas [id] => 643567171 ) [97] => Array ( [name] => Mary Martinez [id] => 100004696266210 ) ) [paging] => Array ( [next] => https://graph.facebook.com/1566027944/friends?limit=5000&offset=5000&__after_id=100004696266210 ) )
DUPLICATE --> How to store a Facebook user's friends list in MySQL using PHP?
$friends_list = file_get_contents('https://graph.facebook.com/me/friends?access_token=' . $atoken );
$friends_list_array = json_decode($friends_list,true);
$arr= $friends_list_array['data'];
$friend_ids_arr = array();
foreach($arr as $friend) {
$friend_ids_arr[] = $friend['id'];
}
$friend_ids = implode("," , $friend_ids_arr);
echo $friend_ids; // output: id1,id2,id3...etc
I have following array pattern.
Array
(
[0] => Array
(
[label] => 2011
[subtable] => Array
(
[0] => Array
(
[label] => 05
[subtable] => Array
(
[0] => Array
(
[label] => /hello-world.html
[url] => http://example.com/2011/05/hello-world.html
)
[1] => Array
(
[label] => /test-test-test.html
[url] => http://example.com/2011/05/test-test-test.html
)
)
)
[1] => Array
(
[label] => 04
[subtable] => Array
(
[0] => Array
(
[label] => /i-have-some-problem-with-gfc-updates.html
[url] => http://example.com/2011/04/i-have-some-problem-with-gfc-updates.html
)
[1] => Array
(
[label] => /testing-for-google-frineds-update.html
[url] => http://example.com/2011/04/testing-for-google-frineds-update.html
)
)
)
[2] => Array
(
[label] => /05
[url] => http://example.com/2011/05
)
[3] => Array
(
[label] => /04
[url] => http://example.com/2011/04
)
)
)
[1] => Array
(
[label] => /index
[url] => http://example.com/
)
)
I want to get "label" and "url" into another array like
array(
[0] = array([label] => a [url] => http://example.com/a/)
[1] = array([label] => b [url] => http://example.com/b/)
)
How can I achieve it using recursion, or by passing it to a function?
You may use array_walk_recursive for this:
$parsed_array = array();
function parse_array($item, $key) {
global $parsed_array;
if(is_array($item) && isset($item['url'])) {
$parsed_array[] = $item;
}
}
array_walk_recursive($your_array, 'parse_array');
Or you may implement it using a recursive function:
function parse_array($array) {
$result = array();
foreach($array as $item) {
if(isset($item['subtable'])) {
$result = array_merge($result, parse_array($item['subtable']));
}else{
$result[] = $item;
}
}
return $result;
}
$parsed_array = parse_array($your_array);