Retrieve facebook posts more than its default limit - facebook

Is there a method to retrieve more posts from facebook? I am developing a site which needs to retrieve at least 200 posts from facebook. Right now I am getting 24 posts only. Is it possible to get 200 posts?
$user_posts = json_decode(#file_get_contents(
'https://graph.facebook.com/me/posts?access_token='.$access_token));
$user_posts = (array)$user_posts;
$user_posts = $user_posts['data'];
echo "<h1>Posts</h1>";
foreach($user_posts as $user_post){
$user_post = (array)$user_post;
echo "<table border='8' width='500'>";
if(($user_post['picture'])||($user_post['message'])){
echo "<tr>";
echo '<td height="4"></td>';
echo "<td>";
if($user_post['message']){
echo "<tr><td>Message : ".$user_post['message']."</td></tr>";
}
}
}
Thanks.

I found the answer for this one.
Just add a limit field to the url.
$user_posts = json_decode(#file_get_contents(
'https://graph.facebook.com/me/posts?access_token='.$access_token.'&limit=200'));

Related

Facebook API images not downloading

First post here,
i am developing a app to fetch and download facebook images from user's account.
I have a basic code till now as follows..
Need help as to why only the 1 file keeps downloading alone...in the zip i create
it is the only image without the ?oh= concatenation, all others with the oh parameter dont get
downloaded...
<?php
set_time_limit(0);
$url='https://graph.facebook.com/me/photos/uploaded?access_token=CAACEdEose0cBAEGw8R4QOGfN3bfX8qtuZCCy8FX6g5Sil8CKaYlp9x8lZAiNFEGJXhh9MYOWdZAJR1x2M627rc5I3n3AauVe8nJtYBsehjUZAljMLkQMMxP03VVXknZBa81Nk1S23z7SnyTkIlgt2oZC1euoFNCTuGesAap1uQUGZAX7LnILlBwyj3V7IVo9QQCNv66Lv8UcA86wYn9jlJE';
$result = file_get_contents($url);
$a=json_decode($result, true);
$files = array();
echo '<div class="col-lg-12">';
$num= count($a['data']);
echo "<table class='table table-hover table-responsive'>";
for($o=0;$o<$num;$o++)
{
$files_to_zip[$o]=$a['data'][$o]['source'];
if($a['data'][$o]['source']!="")
{
echo "<tr><td><img src='".$a['data'][$o]['source']."' id=".$o." width='200' height='200' /></td></tr>";
array_push($files,$a['data'][$o]['source']);
//echo $files_to_zip[$o]."<br/><br/><br/><br/>";
}
}
echo '</table></div><br/>';
?>

Adding an additional field to login with AWD Facebook Wordpress Plugin

I am using AWD Facebook wordpress plugin to allow my visitors to login with their Facebook account information. When a visitor registers on my site I automatically create a new post that is titled with their username and includes their Facebook profile picture as the content. The code for that is below:
function my_create_page($user_id){
$fbuide = 0;
$the_user = get_userdata($user_id);
$new_user_name = $the_user->user_login;
$new_user_avatar = get_avatar($the_user->user_email);
global $AWD_facebook;
$fbuide = $AWD_facebook->uid;
$headers = get_headers('http://graph.facebook.com/' . $fbuide . '/picture?type=large',1);
if(isset($headers['Location'])) {
$url = $headers['Location']; // string
} else {
$url = false;
}
$my_avatar = "<img src='" . $url . "' class='avatar AWD_fbavatar' alt='" . $alt . "' height='" . $size . "' />";
$my_post = array();
$my_post['post_title'] = $new_user_name;
$my_post['post_type'] = 'post';
$my_post['post_content'] = $my_avatar;
$my_post['post_status'] = 'publish';
wp_insert_post( $my_post );
}
add_action('user_register', 'my_create_page');
What I am looking to accomplish is a bit different though. I also want to include a brief biography about the user (currently the post is simply their picture). So when a visitor logs in with AWD Facebook, their needs to be an additional field that allows the user to type in their bio. Then I would be able to grab that info from their user profile and include it in the post. Any ideas on how I can accomplish this? Is there a different way to do this?
I would recommend storing their Facebook picture as metadata and use the content area as their bio for the automatically generated post. So something like this should get you started:
$my_post = array(
'post_title'=>$new_user_name,
'post_type'=>'post',
'post_content'=>'',
'post_status'=>'publish'
);
if( $id = wp_insert_post( $my_post ) ){
update_post_meta($id, 'avatar', $url);
}
Then you can generate the loop like so:
if ( have_posts() ) : while ( have_posts() ) : the_post();
//... stuff here
$avatar = get_post_meta($post->ID, 'avatar', 'true');
the_content();
echo '<img class="avatar AWD_fbavatar" src="'.$avatar.'" alt="'.$alt.'" height="'.$size.'" />';
endwhile;endif;

Tumblr API get current AVATAR URL

Everyone knows? about avatar url in tumblr api / read / json?
like for example the facebook?
http://graph.facebook.com/[your facebook id]/picture?type=normal
<?php
$tumblog = 'natadec0c0'; // change to your username
// if your Tumblog is self hosted, you need to change the base url to the location of your tumblog
$baseurl = 'http://' . $tumblog . '.tumblr.com';
$request = $baseurl . '/api/read/json';
$ci = curl_init($request);
curl_setopt($ci,CURLOPT_RETURNTRANSFER, TRUE);
$input = curl_exec($ci);
curl_close($ci);
// Tumblr JSON doesn't come in standard form, some str replace needed
$input = str_replace('var tumblr_api_read = ','',$input);
$input = str_replace(';','',$input);
// parameter 'true' is necessary for output as PHP array
$value = json_decode($input,true);
$content = $value['posts'];
$blogInfo = $value['tumblelog'];
// the number of items you want to display
$item = 10;
// Echo the blog info
echo "<h3>" . $blogInfo['title'] . "</h3>\n";
echo "<h4>" . $blogInfo['picture'] . "</h4>\n<hr />\n";
?>
how to append my current avatar?
A better solution for this is to put the Avatar api in an img tag.
api.tumblr.com/v2/blog/{base-hostname}/avatar[/size]
example: <img src='http://api.tumblr.com/v2/blog/myreallycoolblog.tumblr.com/avatar/48'/>
So as long as you have the blogname, you can display the avatar.
I guess you have to use
GET http://www.tumblr.com/api/authenticate?email=user#example.com&password=12345
to get the avatar of the user. The sample response for mine is
<tumblr version="1.0">
<user default-post-format="html" can-upload-audio="1" can-upload-aiff="1" can-ask-question="1" can-upload-video="1" max-video-bytes-uploaded="26214400" liked-post-count="134"/>
<tumblelog title="ABNKKPGPiCTuReNPLaKo?!" is-admin="1" posts="301" twitter-enabled="0" draft-count="0" messages-count="0" queue-count="" name="arvn" url="http://arvn.tumblr.com/" type="public" followers="17" avatar-url="http://28.media.tumblr.com/avatar_b1786ec9e62d_128.png" is-primary="yes" backup-post-limit="30000"/>
<tumblelog title="i kras yu." is-admin="1" posts="1" twitter-enabled="0" draft-count="0" messages-count="0" queue-count="" name="ikrasyu" url="http://ikrasyu.tumblr.com/" type="public" followers="2" avatar-url="http://25.media.tumblr.com/avatar_02a7ef66fce8_128.png" backup-post-limit="30000"/>
</tumblr>
and get the avatar-url field of the corresponding tumblelog. Too bad there is no json format option, maybe use preg_match. You also need the email address and password of the user, or do it via OAuth.
Or you could scrape the tumblelog for the avatar.
$page = file_get_contents("http://{$tumblog}.tumblr.com/");
$avatar = preg_match('/<img src="(http.+)" alt="portrait"/', $page, $matches) ? $matches[1]: 'http://example.com/blank.png';

How can I fetch information about the app/song/video etc. from iTunes Store?

I need to get an info about the app/song/video by item id from iTunes Store.
I've found this
But it doesn't work with apps.
Is there any public API?
UPD: I can get info using this link
, but this is not a structured data it's just a markup for iTunes to display stuff. I can't rely on that - it can be changed anytime and is hard to parse because it has no consistent structure...
Apple now seems to offer a friendlier search service returning JSON. NB: the documentation does stipulate that the API is for use in connection with promoting the search results (i.e. it's designed for affiliate links).
Example, fetching info about an app if you know its Apple ID:
http://itunes.apple.com/lookup?id=[appleID]
General keyword search
http://itunes.apple.com/search?term=[query]
As far as I know (and I've done a lot of looking), there isn't a public API.
You're right that the HTML isn't semantically structured, so parsing it won't be very robust. But I think it's your only option. Here are a few links which might help :-
A Python script which parses reviews.
An Ars Technica article: Linking to the stars: hacking iTunes to solicit reviews.
An Inside iPhone article: Scraping AppStore Reviews.
There is a public API into iTunes called "iTunes Store Web Service Search API" that returns quite a bit of information. Some of it is documented here but that documentation is incomplete.
You can use the API to get information about everything for sale in iTunes Store and App Store including urls for the artwork, links directly into iTunes, all the apps by a developer, and so on. It's very robust and I'd love to find updated documentation.
I'm currently writing an article at the iPhone Dev FAQ to show how a few things are done and extend the available documentation.
That link you have there is JSON! You've got the solution right here. You just need JSON.framework
I wrote this script for myself. It's not optimized or future-proof, but it's working for me in the meantime...
<?php
ini_set('display_errors', false);
if(isset($_GET['appID']) && isset($_GET['format']))
{
$appID = (int)stripslashes($_GET['appID']);
$format = stripslashes($_GET['format']);
$url = "http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=$appID&mt=8";
$useragent = "iTunes/4.2 (Macintosh; U; PPC Mac OS X 10.2";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
$result = curl_exec($ch);
curl_close($ch);
$temp = str_replace("½","",strip_tags(substr($result,
strpos($result,"Average rating for the current version:"),
strpos($result,"Rate this application:")-strpos($result,"Average rating for the current version:"))));
$temp1 = explode("ratings",$temp);
if(strpos($temp1[2], "Average rating for all versions:"))
$temp1[2] = substr($temp1[2],0,stripos($temp1[2],"Average rating for all versions:"));
$temp1[2] = preg_replace('/\s\s+/', ' ', $temp1[2]);
$temp2 = explode(" ",$temp1[2]);
$ratings[0] = $temp2[1];
$ratings[1] = $temp2[2];
$ratings[2] = $temp2[3];
$ratings[3] = $temp2[4];
$ratings[4] = $temp2[5];
if($format == "prettyPrint")
printRatings($ratings);
else if($format == "XML");
getXML($ratings);
}
else
{
echo "Enter the app id and format (http://iblackjackbuddy.com/getAppRatings.php?appID=###&format=###";
}
function printRatings($ratings)
{
echo "Five stars: " . $ratings[0];
echo "<br>Four stars: " . $ratings[1];
echo "<br>Three stars: " . $ratings[2];
echo "<br>Two stars: " . $ratings[3];
echo "<br>One star: " . $ratings[4];
echo "<hr>Total ratings: " . getTotalRatings($ratings);
echo "<br>Average rating: " . getAverageRating($ratings);
}
function getTotalRatings($ratings)
{
$temp = 1;
for($i=0; $i < count($ratings); ++$i)
$temp+=$ratings[$i];
return $temp;
}
function getAverageRating($ratings)
{
$totalRatings = getTotalRatings($ratings);
return round(5*($ratings[0]/$totalRatings)
+ 4*($ratings[1]/$totalRatings)
+ 3*($ratings[2]/$totalRatings)
+ 2*($ratings[3]/$totalRatings)
+ 1*($ratings[4]/$totalRatings),2);
}
function getXML($ratings)
{
header('Content-type: text/xml');
header('Pragma: public');
header('Cache-control: private');
header('Expires: -1');
echo '<?xml version="1.0" encoding="utf-8"?>';
echo '<Rating>';
echo '<FiveStars>'.$ratings[0].'</FiveStars>';
echo '<FourStars>'.$ratings[1].'</FourStars>';
echo '<ThreeStars>'.$ratings[2].'</ThreeStars>';
echo '<TwoStars>'.$ratings[3].'</TwoStars>';
echo '<OneStar>'.$ratings[4].'</OneStar>';
echo '<TotalRatings>'.getTotalRatings($ratings).'</TotalRatings>';
echo '<AverageRating>'.getAverageRating($ratings).'</AverageRating>';
echo '</Rating>';
}
?>

Fetching friends' birthdays from facebook profile

I want to fetch 'birthdays' of users, and their friends on my website, from their facebook profiles (with their facebook credentials supplied).
Is their a feature in Facebook API/Connect that I can use to fetch these details from facebook as possible on Native Facebook Apps using Facebook API.
I want to store this data in my DB, and users will be asked for their facebook credentials and consent before this is done.
Read the api documentation things like this are easily done. You can do it like this:
$facebook = new Facebook( $apikey, $secret );
$uid = $facebook->require_login();
$friends = $facebook->api_client->friends_get(); // $friends is an array holding the user ids of your friends
foreach( $friends as $f ) {
$data = $facebook->api_client->fql_query( "SELECT birthday_date FROM user WHERE uid=$f" );
// $data[0] is an array with 'birthday_date' => "02/29/1904"
// see api documentation for other fields and do a print_r
}
So recently I wanted to check my friends to see if any of them had their birthday for the current day. Using FQL this is super easy and I encourage you to explore FQL more because it will yield a more efficient solution than, say, what Pierre kindly offered. Here is a small snippet of the code:
$friends = $facebook->api_client->friends_get();
$uids = "";
foreach($friends as $f) {
$uids .= "uid=$f OR ";
}
$query_uids = substr($uids,0,strlen($query_uids)-4);
date_default_timezone_set('UTC');
$current_date = date("m/d");
echo "<br />Searching for birthdays for the given month/day: $current_date<br />";
$data = $facebook->api_client->fql_query( "SELECT name, uid FROM user WHERE ( ($query_uids) AND strpos(birthday_date,'$current_date') >= 0 )" );
if(count($data) > 0) {
foreach($data as $d) {
print_r($d);
}
} else {
echo "<br />No Birthdays Today<br />";
}
require_once('facebook-platform/client/facebook.php');
$facebook = new Facebook(API_KEY, SECRET);
$facebook->require_login();
function getInfo($user_list, $fields)
{
try
{
$u = $facebook->api_client->users_getInfo($user_list, $fields);
return $u;
}
catch (FacebookRestClientException $e)
{
echo $e->getCode() . ' ' . $e->getMessage();
}
}
function getFriendsBirthdays($user_id)
{
$f = $_REQUEST['fb_sig_friends'];
$f = explode(',', $f);
$birthdays = array();
foreach($f as $friend_id)
{
$birthdays[] = getInfo($friend_id, 'birthday');
}
return $birthdays;
}
Do something like that or use the Batch API to do multiple calls at once. Check the Facebook API.
You could fetch it via the API, but Facebook's terms strictly forbid you from storing anything other than their user ID in your database - see the developer wiki for details. You will need to query the API each time.