FB API (PHP) - How to get photo URL - php

When I try to get photo data by this request
$response = $fb->get('/me/albums');
$albums = $response->getDecodedBody();
foreach($albums['data'] as $album)
{
echo 'Album: '. $album['name'] .'<br/>';
$response = $fb->get('/'. $album['id'] .'/photos');
$photos = $response->getDecodedBody();
foreach($photos['data'] as $photo)
{
$response = $fb->get('/'. $photo['id']);
$data = $response->getDecodedBody();
var_dump($data);
}
}
I get only [ created_time, id ] of each photo. Is some privilege required to get this content?
Do I need to "user_photos" privilege accepted?
I thought that admin may test it without acceptation.

You do need the user_photos permission, but you don´t need to get it approved to make it work for an App admin. You get results, so i assume your authorization is correct. The only thing that is missing are the fields, check out "Declarative Fields" in the changelog: https://developers.facebook.com/docs/apps/changelog#v2_4

Related

saving picture on server - PHP Facebook

I am trying to save the picture which you get from the $FB->get on my server. So I can get that pic and make it a profile picture when login in with facebook. The only thing now is that I don't know how to save that pic on my server. I think I need to use copy or rename function from php or something like that. As you can see I tried using it but I have no clue how I need to fix this.
$oAuth2Client = $FB->getOAuth2Client();
if (!$accessToken->isLongLived())
$accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
$response = $FB->get("/me?fields=id, first_name, last_name, email,gender,link, birthday,picture.type(large)", $accessToken);
$userData = $response->getDecodedBody();
$fb_foto_url = $userData['picture']['data']['url'];
copy($fb_foto_url ,'assets/images/profielfotos');
$voornaam= $app->get_klant_fb($userData['first_name']);
$_SESSION['voornaam'] = $userData['first_name'];
$_SESSION['achternaam'] = $userData['last_name'];
$fb_fotoUrl = $app->saving_fb_foto($userData['picture']['data']['url']);
How I am making a user based on facebook login.
//data ophalen
$klant= $app->get_klant_fb($userData['id']);
if($klant['id'] > 0) {
//sessie klant zetten
$_SESSION['klant_id'] = $klant['id'];
} else {
//klant aanmaken
unset($query);
$query['oauth_uid'] = $userData['id'];
$query['ledenpagina_id']= $_SESSION['ledenpagina_id'];
$query['voornaam'] = $userData['first_name'];
$query['achternaam'] = $userData['last_name'];
$query['emailadres'] = $userData['email'];
$query['geboortedatum'] = $userData['birthday'];
$query['gender'] = $userData['gender'];
$query['link'] = $userData['link'];
$app->insert_query('klanten', $query);
$klant= $app->get_klant_fb($userData['id']);
if($klant['id'] > 0) {
$_SESSION['klant_id'] = $klant['id'];
}
}
This is what the develeper.facebook looks like
If you have the URL which it looks like..
You can do a save..
file_put_contents("your/local/server/file.png", fopen($fb_foto_url, 'r'));
However it would make more sense to me, to store the Facebook URL in your database, and simply use that.. .
Ref : http://php.net/manual/en/function.file-put-contents.php
Please try using the curl option to save the image to the server.
$fb_foto_url = $userData['picture']['data']['url'];
$ch = curl_init($fb_foto_url);
$fp = fopen('assets/images/profielfotos/photo.ext', 'wb'); //Please print out the url and replace the .ext with extension returned such as jpg/png
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
//The image will be stored by now
//Remove the copy function
But if you really don't want to edit the picture or something try getting it directly from the url so it will be automatically updated as well.

Retrieving Facebook / Google+ / Linkedin profile picture having email address only

What I need
I need to automatically find & download profile picture for user knowing his email address only. Originally, I focused on Facebook considering the amount of people actively using it. However, there seem to be no direct support from their API anymore.
There was similar question here:
How to get a facebook user id from the login email address which is quite outdated and current answers there are "it's deprecated" / "it's not possible"...
EDIT: I've found even better question: Find Facebook user (url to profile page) by known email address (where it is actually explained why and since when this feature isn't supported)
There must be a way...
What makes me think that this should be possible is that Spokeo is somehow doing it:
http://www.spokeo.com/email-search/search?e=beb090303%40hotmail.com
There are some services / APIs offering this kind of feature:
Clearbit
Pipl
...but I haven't found anything free.
Alternatives
If there is some workaround or different approach than using Facebook's API to achieve this, I would like to know. If Facebook is really completely hopeless here, then combination of these: Google+, Linkedin and/or Gravatar could do.
My first (original) attempt:
Once you have Facebook's username or user ID, it's easy to build URL to download the picture. So I was trying to look for Facebook's user IDs using emails with the /search Graph API:
https://graph.facebook.com/search?q=beb090303#hotmail.com&type=user&access_token=TOKEN
which unfortunatelly always ends with "A user access token is required to request this resource."
Using FB PHP API + FB App ID & Secret
I've also tried this: at first I retrieve access_token using app ID and secret and then I'm trying to use it as a part of /search request with curl:
function post_query_url($url, $data) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$res = curl_exec($ch);
curl_close($ch);
return $res;
}
function get_query_url($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$ret = curl_exec($ch);
curl_close($ch);
return $ret;
}
function get_retrieve_app_access_token($app_id, $secret) {
$url = 'https://graph.facebook.com/oauth/access_token?client_id='.$app_id.'&client_secret='.$secret.'&grant_type=client_credentials';
$res = get_query_url($url);
if (!empty($res)) {
$tokens = explode('=', $res);
if (count($tokens) == 2)
return $tokens[1];
}
return null;
}
function post_retrieve_app_access_token($app_id, $secret) {
$url = 'https://graph.facebook.com/oauth/access_token';
$data = 'client_id='.$app_id.'&client_secret='.$secret.'&grant_type=client_credentials';
$res = post_query_url($url, $data);
if (!empty($res)) {
$tokens = explode('=', $res);
if (count($tokens) == 2)
return $tokens[1];
}
return null;
}
function get_id_from_email($email, $accessToken) {
$url = 'https://graph.facebook.com/search?q='.urlencode($email).'&type=user&access_token='.$accessToken;
$res = get_query_url($url);
if (!empty($res)) {
return $res;
}
return null;
}
echo 'Retrieving token...<br>';
$token = post_retrieve_app_access_token('MY_APP_ID', 'SECRET');
echo 'Retrieved token: ' . $token . '<br>';
echo 'Retrieving user ID...<br>';
$id = get_id_from_email('beb090303#hotmail.com', $token);
echo 'Retrieved ID: ' . $id . '<br>';
outputs something like:
Retrieving token...
Retrieved token: 367458621954635|DHfdjCnvO243Hbe1AFE3fhyhrtg
Retrieving user ID...
Retrieved ID: {"error":{"message":"A user access token is required to request this resource.","type":"OAuthException","code":102}}
Other info
Since it's asking for "user access token", I've also tried to go to Facebook's Graph Explorer: https://developers.facebook.com/tools/explorer/
let it generate access token for me and queried:
search?q=beb090303#hotmail.com&type=user&debug=all
That one ends with:
{
"error": {
"message": "(#200) Must have a valid access_token to access this endpoint",
"type": "OAuthException",
"code": 200
}
}
...so Facebook seems kinda hopeless here.
That's exactly why Gravatar exists and why people use Gravatar, users know which public profile image they bind to which e-mail address and they know where to change it.
Your app can have the possibility for users to upload their own profile image and fallback to Gravatar.
If you just try to extract an image from Facebook or Google+, it might freak your users out and it will also be harder for them to know where your service got the profile image from.
Using Gravatar in PHP it is as simple as this:
<?php
$email = "email#server.com";
$default = ""; // absolute url to default image goes here or leave empty for default gravatar image
$size = 200;
$grav_url = "http://www.gravatar.com/avatar/" . md5(strtolower(trim($email))) . "?d=" . urlencode($default) . "&s=" . $size;
header("content-type: image/jpeg");
echo file_get_contents($grav_url);
?>
Apart from that, you can also use Facebook and/or Google+ as external login providers where users can grant your application access to their profile information.
There was a bug: Can't search for user by email after July 2013 Breaking Changes that has been closed as "By Design" with official response:
"The ability to pass in an e-mail address into the "user" search type was removed on July 10, 2013. This search type only returns results that match a user's name (including alternate name)" ~ Joseph Tuấn Anh Phan (Facebook Team)
so probably no direct support from Graph API.
I've tried Graph API Explorer where you can try to play with some FQL too (just need to select version 2.0 as newer versions are not supported anymore), unfortunately query like:
SELECT uid, name FROM user where email = 'some.email#gmail.com'
gives:
"error": {
"message": "(#604) Your statement is not indexable. The WHERE clause must contain
an indexable column. Such columns are marked with * in the tables linked from
http://developers.facebook.com/docs/reference/fql ",
"type": "OAuthException",
"code": 604
}
and reference for table user shows that only uid and third_party_id can be used in WHERE.
You should need access token as well as Facebook id of the user. without knowing them cannot get their profile pic
I think Spokeo might have an agreement with Facebook to access the data? I would not be surprised.
Anyway, if you are on a profile you can maybe search for profile_id in the HTML. It's a hack, not sure if it works.
You could always allow people to comment by logging in with their g+/facebook/whatever account (requires you to do something OpenID-like, though); if they've logged in, you should be able to get the facebook uid.
Also, there's something called libravatar, which allows people to associate pictures with their OpenID or email address (and which falls back to gravatar if they haven't configured anything specifically for libravatar); using that should give you more photos than if you stick to "just" gravatar.

How to get count of likes,shares,comment for individual post in facebook using open graph?

I am using Open graph to get face-book page contents.The issue is that for anonymous user accessing the graph result its showing count of likes,shares and comments to be zero for each post. This is the code which I using for php.
$graph_url = "http://graph.facebook.com?id=".urlencode($feed_url);
$content = json_decode(file_get_contents($graph_url));
$account_url = "https://www.facebook.com/feeds/page.php?id=".$content->id."&format=json";
Here content->id is the facebook page id . Is there any way of getting count of likes,shares and comment for each post with anonymous user itself. Any help will be much appreciated.Thanks in advance.
You cannot access private data as anonymous user. You need an access-token (FB-oAuth) in case with a Facebook Application and Users/Page/App permissions to access private data.
You can try it with fql or graph API. If you try to access "private" data. Make sure your created a facebook app before and unlock all user permissions you need to. Means, Stream Reading I think: https://developers.facebook.com/docs/facebook-login/permissions/v2.2. Without that permission, you cannot access private data.
Public example:
http://graph.facebook.com/386050065267_10153256675935268/comments?summary=true
SELECT like_info.like_count, comment_info.comment_count, share_count
FROM stream
WHERE post_id IN (
SELECT concat(id,'_', substr("https://www.facebook.com/Macklemore/posts/10153256675935268", strpos("https://www.facebook.com/Macklemore/posts/10153256675935268", '/posts')+7, strlen("https://www.facebook.com/Macklemore/posts/10153256675935268")))
FROM profile WHERE username IN (
SELECT substr(url, strpos(url, 'facebook.com/')+13, strpos(url, '/posts')-strpos(url, 'facebook.com/')-13)
FROM object_url
WHERE url = "https://www.facebook.com/Macklemore/posts/10153256675935268"))
Result:
{
"data": [
{
"like_info": {
"like_count": 5506
},
"comment_info": {
"comment_count": 353
},
"share_count": 392
}
]
}
Public data can be fetched like:
http://graph.facebook.com/386050065267_10153256675935268/comments?summary=true
or
https://www.facebook.com/PAGE_ID/posts/POST_ID
enter code here
<?php
$fql = "SELECT share_count, like_count, comment_count ";
$fql .= " FROM link_stat WHERE url = '$url'";
$fqlURL = "https://api.facebook.com/method/fql.query?format=json&query=" . urlencode($fql); // Facebook Response is in JSON $ch = curl_init(); curl_setopt($ch,CURLOPT_URL,$fqlURL);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$output = curl_exec($ch);
curl_close($ch);
$facebookdata json_decode($output);
echo $facebookdata[0]->share_count;
echo "<br>";
// facebook like count
echo $facebookdata[0]->like_count;
echo " count <br>";
// facebook comment count
echo $facebookdata[0]->comment_count;
It's very easy please try it

didn't get full quantity of picture with same hashtag

I need show the photos from instagram with my company hash tag to my website.
I'm using the code of below:
<?php
function callInstagram($url)
{
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 2
));
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$tag = 'hermomy';
$client_id = 'my client id';
$url = 'https://api.instagram.com/v1/tags/'.$tag.'/media/recent?client_id='.$client_id;
$inst_stream = callInstagram($url);
$results = json_decode($inst_stream, true);
//Now parse through the $results array to display your results...
foreach($results['data'] as $item){
$image_link = $item['images']['low_resolution']['url'];
echo '<img src="'.$image_link.'" />';
}
?>
*I'm already replace the "my client id" to my true client id
After it i get the result of only show 13 photos from it, by right it should have 373 photos.
You may check http://web.stagram.com/tag/hermomy/ . total is about 373 photos with this hash tag - #hermomy
My result page - 103.6.244.109/~hermo/ayeetest.php
So,how can i show all those photos?
Someone else may be better able to answer but this doesn't seem possible looking at the api.
The reason you are only seeing 13 is that the url you are using is only for recent media. See Tag Endpoint.
Instagram API will not return all photos with a single API call, each call I think returns a maximum of 20 photos. After making the first API call, you have to use the "next_url" in "pagination" of JSON response to make another API call to get the next set of 20 images, for example you may have to implement a "show more" button which will load the next set and so on.
Below is a typical response you get from a instagram API, the making a request to API url at pagination.next_url will return you the next set of photos.
{
"meta": {
"code": 200
},
"data": {
...
},
"pagination": {
"next_url": "...",
"next_max_id": "13872296"
}
}

How can I check if a YouTube channel name exists?

I own a website that generates YouTube names, how can I show the user if a channel is already taken or not?
You can use channel->list request for this.
As mentioned in documents, all you need to do is do
channels.list(part="id", forUsername="username")
If this return an empty list, there is no channel with that username.
Also here are some samples to get you started.
You can try
$api = 'https://gdata.youtube.com/feeds/api/users/';
$user = "user";
$headers = get_headers($api . $user, true);
if ($headers[0] == "HTTP/1.0 200 OK") {
// its ok
}

Categories