Trouble retrieving friends data from Facebook API - php

I am trying to:
Retrieve a friend list from a user with the friend information:
Location
Education
Name
Profile picture
The problem is the standard permissions given only has friend profile picture and name. I used this to get the other information:
$params = array(
'scope' => 'email, friends_likes, user_about_me, friends_about_me, friends_location, friends_website, friends_work_history, friends_education_history'
);
$loginUrl = $facebook->getLoginUrl($params);
}
So now when I log in on my page, Facebook asks me if I'm willing to give those permissions to the application, so that is a good sign.
The problem is I'm still not able to figure out how to get the information. When I try and do
print_r($friends);
or
print_r($friends["data");
I still just get an array with name and ID.
Why am I not seeing the extra information?
This is not the full code, but a lot of it (most came from the example.php from the main Facebook GitHub account):
<?php
if ($user):
echo "Name: " . $user_profile['name']; ?> <br />
<?php echo "Location: " . $user_profile['location']['name']; ?> <br />
<?php echo "Bio: " . $user_profile['bio']; ?> <br />
<?php
foreach($user_profile['work'] as $work) {
echo "Work: " . $work['employer']['name'];?><br />
<?php echo "Position: " . $work['position']['name'];?><br />
<?php
}
?>
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture?type=large">
<h3>Your User Object (/me)</h3>
<pre><?php
print_r($friends);
?></pre>
<?php
echo '<ul>';
foreach ($friends["data"] as $value) {
echo '<li>';
echo '<div class="pic">';
echo '<img src="https://graph.facebook.com/' . $value["id"] . '/picture?type=large"/>';
echo '</div>';
echo '<div class="picName">'.$value["name"].'</div>';
echo '<div class="location">'.$value["location"]["name"];
echo '<div class="bio">'.$value["bio"];
echo '</div>';
echo '</li>';
}
echo '</ul>';
?>
The API call (note I made the $permissions just for testing to be sure I was getting it, and they are showing up).
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
$permissions = $facebook->api('/me/permissions');
$friends = $facebook->api('/me/friends');
}
catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}

Your API call to /me/friends is incorrect. You need to do something like:
$friends = $facebook->api('me/friends?fields=birthday,location,about...');
You need to add in the extra fields you require as parameters in the API call. I've added a few in the above example, but you will need to add the remaining ones.

Related

php variable using mongodb not showing on website server

i am making a twitter clone website using php, and this is the script for creating a tweet:
<?php
session_start();
require_once('twitterdbconnect.php');
if(!isset($_POST['body'])) { //see if the post variable has the body of our tweet
exit;
}
$user_id = $_SESSION['user']; //if there is a tweet get the users id from session
$userData = $db->users->findOne( array('_id'=>$user_id)); //get the users data by asking the database for this user
$body = substr($_POST['body'], 140); //body is the first 140 characters of the post
$date = date('Y-m-d H:i:s');
$db->tweets->insertOne( array( //create a tweets collection and send the tweet there
'authorId' => $user_id,
'authorName' => $userData['username'],
'body' => $body,
'created' => $date
));
header('Location: home.php');
?>
this is the script of printing it on the home page:
<div>
<?php
$recent_tweets = get_recent_tweets($db);
foreach($recent_tweets as $tweet) {
echo '<p>' . $tweet['authorName'] .'</p>';
echo '<p>' . $tweet['body'] . '</p>';
echo '<p>' . $tweet['created'] . '</p>';
echo '<hr>';
}
?>
</div>
the other variables seem to work, but the body variable does not show up and the end result is so:
How can I make it appear as so:
?

Unable to get FirstName and LastName from openid attributes

I am able to get the email when the user sign in via apps.com.
My problem is I cant get first name and last name. here's a sample code from intuit, I tried adding 'namePerson/first' and 'namePerson/last', to the required attribute but it doesn't help.
The codes that i use is from intuit example https://github.com/IntuitDeveloper/SampleApp-PHP-for-OpenId-OAuth-V3APICalls/blob/master/PHPSample/index.php
<?php
ob_start();
require_once("config.php");
require_once("CSS Styles/StyleElements.php");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<h3>IPP PHP Sample App</h3>
<title>IPP PHP sample</title>
<script type="text/javascript" src="https://appcenter.intuit.com/Content/IA/intuit.ipp.anywhere.js"></script>
<script>
// Runnable uses dynamic URLs so we need to detect our current //
// URL to set the grantUrl value ########################### //
/*######*/ var parser = document.createElement('a');/*#########*/
/*######*/parser.href = document.url;/*########################*/
// end runnable specific code snipit ##########################//
intuit.ipp.anywhere.setup({
menuProxy: '',
grantUrl: 'http://'+parser.hostname+'/sampleqboapp/PHPSample/oauth.php?start=t'
// outside runnable you can point directly to the oauth.php page
});
</script>
</head>
<body>
<?php
# This sample uses the LightOpenID library located here: https://gitorious.org/lightopenid
echo '<div> Please refer to the <a target="_blank" href="http://localhost/PHPSample/ReadMe.htm">Read Me</a> page for detailed instructions and information regarding this sample </div><br />';
require 'lightopenid-lightopenid/openid.php';
try {
# Change 'localhost' to your domain name.
$openid = new LightOpenID('localhost');
if(!$openid->mode) {
echo '<div> This sample uses PHP 5.6.3 and Intuit PHP SDK version v3-php-sdk-2.2.0-RC
</div><br />';
echo '<div> To be listed on QuickBooks Apps.com, any app must implement OpenID for user authentication. This sample uses LightOpenID library located at
<a target="_blank" href="https://gitorious.org/lightopenid"> https://gitorious.org/lightopenid </a><br />
</div><br />';
# The connectWithIntuitOpenId parameter is passed when the user clicks the login button below
# The subscribeFromAppsDotCom parameter is an argument in the OpenID URL of a sample app on developer.intuit.com
# Example of OpenID URL: http://localhost/ippPhpOpenId/IPP-PHP-OpenID-Login.php?subscribeFromAppsDotCom
$openid->identity = "https://openid.intuit.com/Identity-jameshwart";
# The following two lines request email and full name
# from the Intuit OpenID provider
$openid->required = array(
'namePerson/friendly',
'contact/email' ,
'contact/country/home',
'namePerson',
'namePerson/first',
'namePerson/last',
'pref/language',
);
header('Location: ' . $openid->authUrl());
} elseif($openid->mode == 'cancel') {
echo 'User has canceled authentication!';
} else {
# Print the OpenID attributes that we requested above, email and full name
echo '<pre>';
print_r($openid);
print_r($openid->getAttributes());
echo '</pre>';
# Add a link to allow the user to logout. The link makes a JavaScript call to intuit.ipp.anywhere.logout()
echo '<br />Sign Out';
//oAuth code
require_once('../v3-php-sdk-2.2.0-RC/config.php'); // Default V3 PHP SDK (v2.0.1) from IPP
require_once(PATH_SDK_ROOT . 'Core/ServiceContext.php');
require_once(PATH_SDK_ROOT . 'DataService/DataService.php');
require_once(PATH_SDK_ROOT . 'PlatformService/PlatformService.php');
require_once(PATH_SDK_ROOT . 'Utility/Configuration/ConfigurationManager.php');
error_reporting(E_ERROR | E_PARSE);
// After the oauth process the oauth token and secret
// are storred in session variables.
$tk = $_SESSION['token'];
if(!isset($_SESSION['token'])){
echo "<h3>You are not currently authenticated!</h3>";
echo '<div> This sample uses the Pecl Oauth library for OAuth. </div> <br />
<div> If not done already, please download the Oauth package from
<a target="_blank" href="http://pecl.php.net/package/oauth"> http://pecl.php.net/package/oauth </a> and follow the instructions given
<a target="_blank" href="http://pecl.php.net/package/oauth"> here </a> for installing the Oauth module.
</div><br />
<div> Add the OAuth Consumer Key and OAuth Consumer Secret of your application to config.php file </div> </br>
<div> Click on the button below to connect this app to QuickBooks
</div>';
// print connect to QuickBooks button to the page
echo "<br /> <ipp:connectToIntuit></ipp:connectToIntuit><br />";
} else {
echo "<h3>You are currently authenticated!</h3>";
$token = unserialize($_SESSION['token']);
echo "If not already done, please make sure that you set the below variables in the app.config file, before proceeding further! <br />";
echo "<br />";
echo "realm ID: ". $_SESSION['realmId'] . "<br />";
echo "oauth token: ". $token['oauth_token'] . "<br />";
echo "oauth secret: ". $token['oauth_token_secret'] . "<br />";
echo "<br />";
echo "<button class='myButton' title='App Home Page' onclick='myFunction($value)'>Go to the app</button>";
echo ' ';
echo "<button class='myButton' title='Disconnect your app from QBO' onclick='Disconnect($value)'>Disconnect the app</button>";
echo ' ';
echo "<button class='myButton' title='Regenerate the tokens within 30 days prior to token expiration' onclick='Reconnect($value)'>Reconnect the app</button>";
echo "<br />";
echo "<br />";
echo "<br />";
echo '<div> <small> <u> Note:</u> Configuring the Oauth tokens manually in app.config file is only for demonstartion purpose in this sample app. In real time production app, save the oath_token, oath_token_secret, and realmId in a persistent storage, associating them with the user who is currently authorizing access. Your app needs these values for subsequent requests to Quickbooks Data Services. Be sure to encrypt the access token and access token secret before saving them in persistent storage.<br />
Please refer to this <a target="_blank" href="https://developer.intuit.com/docs/0050_quickbooks_api/0020_authentication_and_authorization/connect_from_within_your_app"> link </a>for implementing oauth in your app. </small></div> <br />';
}
}
} catch(ErrorException $e) {
echo $e->getMessage();
}
ob_end_flush();
?>
<script>
function myFunction(parameter){
window.location.href = "http://localhost/PHPSample/SampleAppHomePage.php";
}
function Disconnect(parameter){
window.location.href = "http://localhost/PHPSample/Disconnect.php";
}
function Reconnect(parameter){
window.location.href = "http://localhost/PHPSample/Reconnect.php";
}
</script>
</body>
</html>
Could someone point out what did i miss?

Why I'm not able to print the whole output to the screen in PHP?

I'm using Slim framework for my project. I've copied the Slim folder to my project directory.
No following is the code I'm having issue with :
PHP Code(requestdemo.php):
<?php
require 'Slim/Slim.php';
/* Invoke the static "registerAutoloader()" function defined within Slim class.
* Register the autoloader is very important.
* Without doing it nothing will work.
*/
\Slim\Slim::registerAutoloader();
//Instantiate Slim class in order to get a reference for the object.
$application = new \Slim\Slim();
$application->get(
'/request',
function()
{
GlOBAL $application;
echo " <br/><b>request methods</b>";
echo "<br/>application->request->getMethod()=".$application->request->getMethod();
echo "<br/>application->request->isGet()=".$application->request->isGet();
echo "<br/>application->request->isPost()=".$application->request->isPost();
echo "<br/>application->request->isPut()=".$application->request->isPut();
echo "<br/>application->request->isDelete()=".$application->request->isDelete();
echo "<br/>application->request->isHead()=".$application->request->isHead();
echo "<br/>application->request->isOptions()=".$application->request->isOptions();
echo "<br/>application->request->isPatch()=".$application->request->isPatch();
echo "<br/>application->request->isAjax()=".$application->request->isAjax();
echo "<br/> <br/><b>request headers</b>";
$headers = $application->request->headers;
foreach($headers as $k=>$v)
{
echo "<br/>$k => $v";
}
echo "<br/> <br/><b>request body</b>";
echo "<br/>body=".$application->request->getBody();
echo "<br/> <br/><b>request variables</b>";
echo "<br/>width=".$application->request->params('width');
echo "<br/>height=".$application->request->params('height');
echo "<br/> <br/><b>request get variables</b>";
echo "<br/>width=".$application->request->get('width');
echo "<br/>height=".$application->request->get('height');
echo "<br/> <br/><b>request post variables</b>";
echo "<br/>width=".$application->request->post('width');
echo "<br/>height=".$application->request->post('height');
echo "<br/> <br/><b>resource uri</b>";
/*From the below line I'm not able to see the output in a browser.*/
echo "<br/>rootUri=".$application->request->getUri();
echo "<br/>resourceUri=".$application->request->getResourceUri();
echo "<br/> <br/><b>request ajax check</b>";
echo "<br/>rootUri=".$application->request->isAjax();
echo "<br/>resourceUri=".$application->request->getResourceUri();
echo "<br/> <br/><b>request helpers</b>";
echo "<br/>content type=".$application->request->getContentType();
echo "<br/>media type=".$application->request->getMediaType();
echo "<br/>host=".$application->request->getHost();
echo "<br/>scheme=".$application->request->getScheme();
echo "<br/>path=".$application->request->getPath();
echo "<br/>url=".$application->request->getUrl();
echo "<br/>user agent=".$application->request->getUserAgent();
});
$application->run();
?>
The file 'requestdemo.php' is present in the directory titled "slimsamples" at location /var/www/slimsamples
As I hit the URL 'http://localhost/slimsamples/requestdemo.php/request' I'm able to see only the part of output in a browser window. From where I'm not able to see the output I've commented in my code. I'm not able to see the output after line resource uri. See the screenshot for further understanding.
Also there is no syntactical error in it then why it's happening I'm not understanding.
Can someone please find out the mistake I'm making here?
Thanks in advance.
Use: request->getUrl()
(You used request->getUri())
See http://dev.slimframework.com/phpdocs/classes/Slim.Http.Request.html#getUrl

Unable to store the Facebook profile picture in sql database

As you can see the code below retrieve and shows all the required information from a Facebook profile..
Access Token:
User ID:
Name: Aneesh
First Name:
Last Name:
Email:
Gender:
Birthday:
Location: N
Time Zone:
but i am not able to store the profile picture in sql database ?
<?php
require 'lib/db.php';
require 'lib/facebook.php';
require 'lib/fbconfig.php';
session_start();
$facebook=$_SESSION['facebook'];
$userdata=$_SESSION['userdata'];
$logoutUrl=$_SESSION['logout'];
$access_token_title='fb_'.$facebook_appid.'_access_token';
$access_token=$facebook[$access_token_title];
if(!empty($userdata))
{
echo '<h1>Login User Details</h1>';
echo '<img src="https://graph.facebook.com/'.$userdata['id'].'/picture">';
At the moment the above the code displays the image...but how to store is the problem
I used BLOB which showed error
echo "<br/>";
echo '<b>Access Token: </b>'.$access_token;
echo "<br/>";
echo '<b>User ID: </b>'.$userdata['id'];
echo "<br/>";
echo '<b>Name: </b>'.$userdata['name'];
echo "<br/>";
echo '<b>First Name: </b>'.$userdata['first_name'];
echo "<br/>";
echo '<b>Last Name: </b>'.$userdata['last_name'];
echo "<br/>";
echo '<b>Email: </b>'.$userdata['email'];
echo "<br/>";
echo '<b>Gender: </b>'.$userdata['gender'];
echo "<br/>";
echo '<b>Birthday: </b>'.$userdata['birthday'];
echo "<br/>";
echo '<b>Location: </b>'.$userdata['location']['name'];
echo "<br/>";
echo '<b>Time Zone: </b>'.$userdata['timezone'];
echo "<br/>";
echo "<br/>";
$facebook_id=$userdata['id'];
$name=$userdata['name'];
$email=$userdata['email'];
$gender=$userdata['gender'];
$birthday=$userdata['birthday'];
$location=mysql_real_escape_string($userdata['location']['name']);
$hometown=mysql_real_escape_string($userdata['hometown']['name']);
$bio=mysql_real_escape_string($userdata['bio']);
$relationship=$userdata['relationship_status'];
$timezone=$userdata['timezone'];
$inserty = "INSERT INTO `users` (`facebook_id`, `name`, `email`, `gender`, `birthday`, `location`,`timezone`, `access_token`,`??????`)
VALUES ('$facebook_id','$name','$email','$gender','$birthday','$location','$timezone','$access_token',`?????`)";
mysql_query($inserty, $connection);
????? I meant column for image
echo "<br/>";
echo 'Logout Facebook';
echo 'Logout Facebook';
include('status_update.php');
}
else
{
header("Location: fblogin.php");
}
?>
It sounds like you are trying to store the binary data for the image itself, instead of the URL to the image. Ideally, you shouldn't store the image / URL as it's ever-changing on Facebook. Depending on the user, it may change everyday! You can get the current image easily using the API or Facebook ID.
If you must store it, you should store the URL to the Facebook version rather than the image itself.
If you still want to store the image in the DB, try using the BINARY type instead.

showing content in profile box from another file

i'm stuck at this facebook application, not knowing how do i go ahead.
for the canvas, the app works quite perfectly. i've two pages, quote.html and quote.php. in quote.html, quote.php [which gets the quotes from the database] is called through ajax which displays a quote randomly for 10 secs.
however, for the wall tab/profile box, i can't seem to know where i'm going wrong. i created a page called 'profile_box.php', call quote.php for the quotes, setFBML. while this works on canvas [sans the timing], it doesn't display anything on the wall tab.
<?php
include 'quote.php';
$row = mysql_fetch_array($result, MYSQL_ASSOC);
if ( !$row ) {
echo "Empty";
}
else{
$fb_box = "<p>" . h($row['cArabic']) . "</p>";
$fb_box .= "<p>" . h($row['cQuotes']) . "</p>";
$fb_box .= "<p>" . h($row['vAuthor']) . "</p>";
$fb_box .= "<p>" . h($row['vReference']) . "</p>";
}
try{
$url="http://website/name/quote.html";
$facebook->api_client->fbml_refreshRefUrl($url);
$fbml = "<fb:ref url='$url'/>";
if(isset($_REQUEST['fb_sig_page_id'])){
$page_id = $_REQUEST['fb_sig_page_id'];
$profile_type = $_REQUEST['fb_sig_type'];
$facebook->api_client->profile_setFBML($appapikey, $page_id, "$fbml", NULL, NULL, "$fb_box");
}
else {
$is_tab = isset($_REQUEST['fb_sig_in_profile_tab']);
if( !$is_tab ) $user_id = $facebook->require_login();
$result = $facebook->api_client->profile_setFBML($appapikey, $user_id, "$fbml", NULL, NULL, "$fb_box");
}
}
catch(Exception $ex){
echo 'Caught exception: ', $ex->getMessage(), "\n";
}
echo "<fb:add-section-button section=\"profile\" /><br />";
?>
can anyone please give any pointers?
I hate to be the bearer of bad news, but Profile Boxes and the Boxes Tab are gone.
They are being replaced with Application Tabs.
In other words, you will no longer be able to display custom content on a user's profile. It all has to go into a application-specific tab.

Categories