pagination within the facebook API (photos) - php

I'm currently developing a facebook app that needs access to a users photos. It needs to loop through all of the users active photos however I'm having some trouble with the pagination aspect of the feed. I get results from the API like
stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[id] => 10151796309135076
[from] => stdClass Object
(
[name] => Daniel Benzie
[id] => 762525075
)
)
)
)
obviosuly the above is an excerpt and then down the bottom there is a section for next and previous pages.
[previous] => https://graph.facebook.com/762525075/photos?access_token=xxxxxxx&limit=25&since=1338985293&__previous=1
[next] => https://graph.facebook.com/762525075/photos?access_token=xxxxx&limit=25&until=1332002972
this is always set- does anyone know the best way to loop through the photos in this case? thanks in advance (:

Keep calling the next url until there is no more data
while ($some_photos['data'])
{
$all_photos = array_merge( $all_photos, $some_photos['data'] );
$paging = $some_photos['paging'];
$next = $paging['next'];
$query = parse_url($next, PHP_URL_QUERY);
parse_str($query, $par);
$some_photos = $facebook->api(
$user_id."/photos", 'GET', array(
'limit' => $par['limit'],
'until' => $par['until'] ));
}

Related

how can i extract specific data from facebook graph api array?

I have convert my object data to an array and now i am trying to extract certain parts from the multi dimensional array however i am having some problems. Assistance is appreciated, thank you.
/* PHP SDK v4.0.0 */
/* make the API call */
$request = new FacebookRequest(
$session,
'GET',
'/89647580016/feed'
);
$response = $request->execute();
$graphObject = $response->getGraphObject()->AsArray();
/* handle the result */
// print data
echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';
The following is the output:
Array
(
[data] => Array
(
[0] => stdClass Object
(
[id] => 89647580016_10153019927930017
[from] => stdClass Object
(
[name] => Central Casting Los Angeles
[category] => Local Business
[category_list] => Array
(
[0] => stdClass Object
(
[id] => 176831012360626
[name] => Professional Services
)
)
[id] => 89647580016
)
[message] => ***NON Union Submissions***
Must be registered with Central Casting!
Jessica is currently booking a TV show working tomorrow Friday June 26th with a possible recall Monday June 29th in LA. These will be Night Calls, so you must be okay working late into the night.
She is looking for Caucasian looking men, who appear to be in their 30's-50's, who appear to be very upscale, who have business suits.
If this is you please call submission line 818-260-3952. Thank you!
[actions] => Array
(
[0] => stdClass Object
(
[name] => Comment
[link] => https://www.facebook.com/89647580016/posts/10153019927930017
)
[1] => stdClass Object
(
[name] => Like
[link] => https://www.facebook.com/89647580016/posts/10153019927930017
)
[2] => stdClass Object
(
[name] => Share
[link] => https://www.facebook.com/89647580016/posts/10153019927930017
)
)
How can i print all the ['message'] ? I tried:
foreach ($graphObject as $key => $value) {
echo '<br>'.$key['message'];
}
But i got a error. Thank you for your help.
Your array has some keys which elements are actually StdClass. You can't reffer to it as $key['message'] but as: $key->message.
Also, don't forget to include your error message. An error is extremely generic and we can't/won't help you if there is no indication of what is wrong.
I didn't notice your foreach. As you are iterating only on $graphObject first level, you will get as $key data and as value another whole array which each key has a StdClass. On your foreach, run it as foreach ($graphObject['data'] as $key => $value) , then use it as echo $value->message

get instagram user id using php foreach in wordpress

I created wordpress shortcode to get user ID by search,
I using foreach() but result not working!
I want foreach because i will make shortocde to get latest instagram images,
Resource:
http://instagram.com/developer/endpoints/users/
http://codex.wordpress.org/Function_Reference/wp_remote_get
http://codex.wordpress.org/Function_Reference/wp_remote_retrieve_body
This is my code:
function insta_id($atts, $content = null){
$my_access_token = "MY ACCESS TOKEN IS HERE";
$get_id = wp_remote_get("https://api.instagram.com/v1/users/search?q=youtube&access_token=$my_access_token&count=2");
$retrieve_id = wp_remote_retrieve_body( $get_id );
$result = json_decode($retrieve_id, true);
foreach ( $result as $user_id ) { // loop start
print_r($user_id); // this working but display all array
echo $user_id->id; // not working!
}
}
add_shortcode("insta_id", "insta_id");
print_r($user_id); display this:
Array ( [0] => Array ( [username] => youtube [bio] => Behind the scenes with stars from your favorite YouTube channels. [website] => [profile_picture] => http://photos-b.ak.instagram.com/hphotos-ak-xaf1/10691785_700124036737985_752862120_a.jpg [full_name] => [id] => 1337343 ) [1] => Array ( [username] => youtubewtfff [bio] => ғυnniest moments on yoυтυвe 😂 Kik: Youtubewtf Email : tysroark#hotmail.com Want a shoutout? Tag #Youtubewtf [website] => [profile_picture] => http://images.ak.instagram.com/profiles/profile_489772119_75sq_1391400797.jpg [full_name] =>
You may be retrieving the wrong key from the response. Double check the instagram User ID with https://www.thekeygram.com/find-instagram-user-id/
It should be: $user_id['id']

How to pull out certain value using php and this output

I've come across a weird scenario I do not know how to code around. I'm creating a JSON API for a wordpress site. I'm using the Connections plugin and trying to pull out the "original" image filename. The output of my sql command is this:
{
["options"]=>
string(396) "a:4:{s:5:"entry";a:1:{s:4:"type";s:12:"organization";}s:5:"group";a:1:{s:6:"family";a:0:{}}s:4:"logo";a:2:{s:6:"linked";b:0;s:7:"display";b:0;}s:5:"image";a:3:{s:6:"linked";b:1;s:7:"display";b:1;s:4:"name";a:4:{s:9:"thumbnail";s:25:"invoicelogo_thumbnail.jpg";s:5:"entry";s:21:"invoicelogo_entry.jpg";s:7:"profile";s:23:"invoicelogo_profile.jpg";s:8:"original";s:24:"invoicelogo_original.jpg";}}}"
}
}
I'm using the following command to acquire that:
querystr = "SELECT options FROM {$wpdb->prefix}connections WHERE id= '{$_GET['companyID']}'";
$options = $wpdb->get_results($querystr);
I'm not sure how to pull out the "original" part of this code though as it's not all that organized. Any help would be appreciated.
What you are seeing is the results of a php serialize call
To get at the original name just do this.
$decodedOptions = unserialize($options);
$original = $decodedOptions["image"]["name"]["original"];
Hope that helps
As a side note the deserialized data looks like
Array
(
[entry] => Array
(
[type] => organization
)
[group] => Array
(
[family] => Array
(
)
)
[logo] => Array
(
[linked] =>
[display] =>
)
[image] => Array
(
[linked] => 1
[display] => 1
[name] => Array
(
[thumbnail] => invoicelogo_thumbnail.jpg
[entry] => invoicelogo_entry.jpg
[profile] => invoicelogo_profile.jpg
[original] => invoicelogo_original.jpg
)
)
)

How to access more than 10 item's detail in Amazon api using php?

I am working with amazon api and have used code from online sources http://www.codediesel.com/php/accessing-amazon-product-advertising-api-in-php/.
I would like to get more than 10 product's detail when I make a search query using amazon api. I am aware about the amazon api policy of getting 10 data per call but is it possible to get more data by creating loop or something?
When I make a request I have assigned following parameteres
$parameters = array("Operation" => "ItemSearch",
"SearchIndex" => "Electronics",
"ResponseGroup" => "Images,ItemAttributes,EditorialReview,Offers ",
"ItemPage"=>"10",
"Keywords" => $search );
So even though I have asked for 10 pages of result, I am unsure of how to display data from every page (1 to 10 ) so in total I get 100 items when I make a query. I get following response when I try to make run the code:
SimpleXMLElement Object (
[Request] => SimpleXMLElement Object (
[IsValid] => True
[ItemSearchRequest] => SimpleXMLElement Object (
[ItemPage] => 10
[Keywords] => laptop
[ResponseGroup] => Array (
[0] => Images
[1] => ItemAttributes
[2] => EditorialReview
[3] => Offers
)
[SearchIndex] => Electronics
)
)
[TotalResults] => 3383691
[TotalPages] => 338370
[MoreSearchResultsUrl] => http://www.amazon.co.uk/gp/redirect.html?camp=2025&creative=12734&location=http%3A%2F%2Fwww.amazon.co.uk%2Fgp%2Fsearch%3Fkeywords%3Dlaptop%26url%3Dsearch-.................(and on)
)
Yes, you would need to loop through 10 times and appends an array or object. The AWS documentation says that ItemPage is actually the page of results, so you would just need to page through it 10 times to get your 100 results.
AWS Documentation on ItemPage:
http://docs.aws.amazon.com/AWSECommerceService/latest/DG/PagingThroughResults.html
$obj = new AmazonProductAPI();
$results = array();
for ($i=1;$i<=10;$i++) {
$parameters = array("Operation" => "ItemSearch",
"SearchIndex" => "Electronics",
"ResponseGroup" => "Images,ItemAttributes,EditorialReview,Offers ",
"ItemPage"=>$i,
"Keywords" => $search);
$results[] = $obj->searchProducts($parameters);
}
foreach ($results as $r) {
//do your stuff
}
We can use manufacturer parameter along with BrowseNode to retrieve more than 100 products in a specific category.

SugarCRM get_entry returns no fields with REST API

Calling "get_entry" on the REST webservice returns an empty result in this manner:
Array
(
[entry_list] => Array
(
[0] => stdClass Object
(
[id] => 85a67fbe-ab86-597a-6bca-4f0305719543
[module_name] => Contacts
[name_value_list] => Array
(
)
)
)
[relationship_list] => Array
(
)
)
My call looks like:
$method = 'get_entry';
$params = array(
'module_name' => 'Contacts',
'id' => $sugarbean_id,
);
But I have absolutely no clue why i get an "empty" object as a result.
Can anyone help me or give me some pointers?
Thanks
Jeroen
You need to provide a session id for the get_entry call. You get the session id when calling the login method. See the get_entry documentation for more information.
Found it. It turns out that I had to explicitly specify select_fields.

Categories