Get Facebook profile name using profile id - php

Am using php sdk to get facebook information. I have an array of facebook ids.
facebook ids 100003186894762, 100002039757708, 100001527538655, 100005502043347
$access_token = $facebook->getAccessToken();
$facebook ->setAccessToken($access_token);
$user_profile = $facebook->api(/100003186894762);
Here am getting one user information. Is there any way to get an array of information in a single request.
like this way
$user_profile = $facebook>api(/100003186894762/100002039757708/100001527538655/100005502043347);
I tried with looping each request but its taking too much time
$resultfbid is an array of facebook ids
foreach($resultfbid as $key=>$fbid) {
$user_profile = $facebook->api('/'.$fbid);
$posts[] = array('name'=> $user_profile['first_name'], 'id'=> $user_profile['id'],'status'=>$resultstatus[$key] );
}
Please help me to find one solution for this issue, Thanks

You can query the API for multiple IDs by using a GET parameter id and list the ids comma-separated:
https://developers.facebook.com/tools/explorer?method=GET&path=%3Ffields%3Dname%26ids%3D100003186894762%2C100002039757708%2C100001527538655%2C100005502043347

What about this?
file_get_contents( 'http://graph.facebook.com/100003186894762/picture' );

Related

Retrieve All User Lists using Aweber API

I am adding AWeber as an autoresponder in a web application. Using AWeber API, I am able to add a new subscriber to list with a known name which is in this case is firstlist:
$app = new MyApp();
$app->findSubscriber('whtever#aol.com');
$list = $app->findList('firstlist');
$subscriber = array(
'email' => 'someemail#gmail.com',
'name' => 'Name here'
);
$app->addSubscriber($subscriber, $list);
Function definition for findList() is:
function findList($listName) {
try {
$foundLists = $this->account->lists->find(array('name' => $listName));
return $foundLists[0];
}
catch(Exception $exc) {
print $exc;
}
}
As I am developing a public application, so I need to provide users an option to select from their available lists.
Please guide me how I can retrieve all the lists name.
You are returning $foundLists[0] so it will return single list. Try to return foundLists and check what it returns.
This may help you: https://labs.aweber.com/snippets/lists
In short, I pulled the lists by first finding the Aweber User Id so that I could use it in the URL https://api.aweber.com/1.0/accounts/<id>/lists
To find the User ID, I first got the account.
$this->aweber->getAccount($token['access'], $token['secret']);
Then, I retrieve the user's information.
$aweber_user = $this->aweber->loadFromUrl('https://api.aweber.com/1.0/accounts');
From that, I grabbed the user ID with...
$id = $aweber_user->data['entries'][0]['id'];
Once I had the user ID, I could then retrieve their lists with...
$lists = $this->aweber->loadFromUrl('https://api.aweber.com/1.0/accounts/'.$id.'/lists');
This example is more of a procedural approach, of course, I recommend utilizing classes.

Display Facebook ADs Statistics on webpage using Facebook Ads API in PHP

Does anyone know how to fetch all facebook ads statistics and display on webpage using Facebook Ads Api-PHP SDK. I am using this API and I am getting campaign details like name of campaign, id, status. but not able to get impressions,clicks, spent.
What I am doing let me share with you:
1) I am getting access token by authorizing user
2) After getting access token, I am using below code
$account = new AdAccount('act_XXXXXXXXXXXXXXX');
$account->read();
$fields = array(
AdCampaignFields::ID,
AdCampaignFields::NAME,
AdCampaignFields::OBJECTIVE,
);
$params = array(AdCampaignFields::STATUS => array(AdCampaign::STATUS_ACTIVE,AdCampaign::STATUS_PAUSED,),);
$campaigns = $account->getAdCampaigns($fields, $params);
/* Added By Jigar */
$campaign = new AdCampaign('XXXXXXXXXXXXXXXX');
$compainDetails = $campaign->read($fields);
3) then printing the array
echo "<pre>";
print_r($compainDetails);
exit;
If anyone know any suggestion in above code, please share. All code is in PHP. Dose anyone have any tutorial that fetch all above required data then share it
You could try to use the facebook insights api instead of $campaign->read. Here's an example:
https://developers.facebook.com/docs/marketing-api/insights/v2.5#create-async-jobs
What you have to do to get impressions, click and spent is to add these fields to the $fields param. In your case, the complete code should look like the following:
use FacebookAds\Object\Campaign;
use FacebookAds\Object\Values\InsightsLevels;
use FacebookAds\Object\Values\InsightsFields;
$campaign = new Campaign();
$fields = array(
InsightsFields::IMPRESSIONS,
InsightsFields::UNIQUE_CLICKS,
InsightsFields::CALL_TO_ACTION_CLICKS,
InsightsFields::INLINE_LINK_CLICKS,
InsightsFields::SOCIAL_CLICKS,
InsightsFields::UNIQUE_SOCIAL_CLICKS,
InsightsFields::SPEND,
);
$params = array(
'level' => InsightsLevels::CAMPAIGN,
);
$async_job = $campaign->getInsightsAsync($fields, $params);
$async_job->read();
I don't know what exactly the "click" param means for you, but if you take a look at all these click params, I'm sure you'll find it or you'll know how to calculate it.
For a complete list of fields available on insights objects, have a look at: https://github.com/facebook/facebook-php-ads-sdk/blob/master/src/FacebookAds/Object/Fields/InsightsFields.php
Hope that helps.
Regards, Benjamin

Custom photo caption when uploading a picture using Facebook PHP SDK?

I have a Facebook App that allows users to upload pictures from my website to my Facebook page. I would like to know who uploaded the pictures and after some research I managed to get this following PHP code:
$args = array(
'message' => $fbme['name'].' will have a love marriage.',
'image' => '#' . realpath('./images/wall-e-and-eva1.jpg'),
);
$data = $facebook->api('/'.$uid.'/photos', 'post', $args);
My output is suppose to be on the lines of Adam Smith will have a love marriage unfortunately the only output I am getting is will have a love marriage.
How can I get the value of $fbme['name'] - can you give me a pratical example of code please?
Thanks.
You can use the graph api to fetch basic information (including name) of your authenticated users
try {
$fbme = $facebook->api('/'.$uid); // $fbme['name'] will be the user's name
// put rest of your code below
} catch (FacebookApiException $e) {
// handle errors
}

PHP Facebook API - extended permission data not showing up with (offline) access_token

I'm trying to retrieve profile information regarding users who allowed my app to collect data about them (birthday, location etc.). This works fine when, and only when, the user actually visits my app and authenticates with facebook through token exchange.
However, I want to be able to routinely check if the user location hasn't changed, also if a user adds information about himself on FB, I want to "sync" it with my app, so that the information stays the same.
So I save the users access token, and later on, with a cron job I use the token to get his profile:
$user = mysql_query ... <- get data from mysql
and then:
$user_profile = $facebook->api('/'.$user['uid']);
And it works, of course, but all the data I want from using extended permissions - just doesn't show up!
Is the the curse of the offline_access disabling? Or do I need to use a different way of obtaining such data. I tried adding ?fields=birthday and etc. on the end of the query, but it still doesn't work.
Not sure if this is what you meant by 'offline_access disabling' however Facebook are deprecating offline_access as you can see in the roadmap:
http://developers.facebook.com/roadmap/
You can read more information on the deprecation here:
http://developers.facebook.com/roadmap/offline-access-removal/
Also, are you correctly setting the access token received from the database?
$facebook->setAccessToken($user['access_token']);
Found out how to accomplish this - the access token has to be passed as an additional parameter on the api queries, like such:
$user_profile = $facebook->api('/'.$user['uid'].'', array('access_token' => $session['access_token']));
In case anyone wonders onto this page, here is a way to use saved access tokens, to access full information about the user you want, without having to authenticate:
<?
require "facebook.php";
$facebook = new Facebook(array(
'appId' => your_app_id,
'secret' => your_app_secret
));
// get the saved user details
$user = mysql_fetch_assoc(mysql_query('SELECT * FROM `facebook_tokens` WHERE `id` = "1"'));
$session = array(
'access_token' => $user['access_token'],
'secret' => $user['secret'],
'session_key' => $user['session_key'],
'uid' => $user['uid']
);
$sig = '';
foreach($session as $key => $value) {
$sig .= implode('=', array($key, $value));
}
$session['sig'] = md5($sessionStr.'Your App Secret');
$facebook->setSession($session, false);
$user_profile = $facebook->api('/'.$session['uid'], array('access_token' => $session['access_token']));
echo '<pre>';
print_r($user_profile);
echo '</pre>';
?>

Retrieving Family information from Facebook using FQL?

Hi I am trying to pull information from Facebook using FQL. I was able to gather info on my friends and the profile users likes however I cant seem to get a list of family information, the code I currently have is below. I was wondering if anyone could help and let me know what I am doing wrong, any help would be great appreciated, thanks
<h1>My Family</h1>
<?php
$user = $facebook->getUser();
$friends = $facebook->api('/me/friends');
$fql_query = array(
'method' => 'fql.query',
'query' => "SELECT profile_id, name, relationship FROM family WHERE profile_id={$user}");
$family = $facebook->api($fql_query);
$fam = count($family);
echo $fam;
for ($i=0;$i<$fam;$i++){
echo "<p>".$family[$i]['name']." ".$family[$i]['relationship']." from ".$family[$i]['sex']." </p>";
}
?>
You need friends_relationships extended permission to get access to the user's family.
https://developers.facebook.com/docs/reference/api/permissions/
also "sex" you have there seems unrelated
You need user_relationships permission to view the user's family info.

Categories