How to set uploaded photo as profile picture in facebook? - php

I have successfully uploaded picture to facebook using Graph API v5.
But how do i make it default profile picture ?
Following is my upload() function:
//Upload image
function upload($path,$token,$fb)
{
$image = [
'message' => 'My awesome photo upload example.',
'source' => $fb->fileToUpload($path), // $fb is instance
];
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->post('/me/photos', $image, $token);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$graphNode = $response->getGraphNode();
debug_to_console($graphNode);
echo " \n Photo ID: " . $graphNode['id'];
}
https://developers.facebook.com/docs/php/api

Related

How to upload a video to Facebook event page via PHP

I have created an event on my test page and I'm going to upload a video to that event page from my web site. I have used the below code but it is doesn't work.
$data = [
'title' => 'test Video',
'description' => 'This is test video',
'source' => $fb->videoToUpload($video_path),
];
try {
$response = $fb->post('/' . $event_id . '/videos', $data, $page_token);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
return 'Graph returned an error: ' . $e->getMessage();
} catch(Facebook\Exceptions\FacebookSDKException $e){
return 'Facebook SDK returned an error: ' . $e->getMessage();
}
$graphNode = $response->getGraphNode();
I got this error.
Graph returned an error:(#33) This object does not exist or does not support this action.
So I fix some parts like this.
$data = [
.......
'source' => $video_path
];
try{
$response = $fb->post('/' . $event_id . '/feed', $data, $page_token);
}
.......
Then it works like this.
But I want to result like as this picture.
How shall I do?
Official reference for post video on event (Api Graph v5.0):
Upload:
$fb = new Facebook\Facebook([
'app_id' => '{app-id}',
'app_secret' => '{app-secret}',
'default_graph_version' => 'v2.2',
]);
$data = [
'title' => 'My Foo Video',
'description' => 'This video is full of foo and bar action.',
'source' => $fb->videoToUpload('/path/to/foo_bar.mp4'),
];
try {
$response = $fb->post('/me/videos', $data, 'user-access-token');
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$graphNode = $response->getGraphNode();
var_dump($graphNode);
echo 'Video ID: ' . $graphNode['id'];
POST ON EVENT:
/* PHP SDK v5.0.0 */
/* make the API call */
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->get(
'/{event-id}/videos',
'{access-token}'
);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$graphNode = $response->getGraphNode();
/* handle the result */
If you see the result JSON :
{
"data": [],
"paging": {}
}
All reference:
-Start - Initialize an upload session
-Transfer - Upload video chunks
-Finish - Post the video
In the function:
private function postFBVideo($authResponse, $fileObj, $formData)
{
FacebookSession::setDefaultApplication('yourAppkey', 'yourAppSecret');
$ajaxResponse = '';
try {
$session = new FacebookSession($authResponse->accessToken);
} catch (FacebookRequestException $ex) {
// When Facebook returns an error
$ajaxResponse = 'FB Error ->' . json_encode($ex) ;
} catch (\Exception $ex) {
// When validation fails or other local issues
$ajaxResponse = 'FB Validation Error - ' . json_encode($ex) ;
}
if ($session) {
$response = (new FacebookRequest(
$session, 'POST', '/.$idevent./videos', array(
'source' => new CURLFile('path', 'video/MOV'),
'message' => $formDataMessage,
)
))->execute();
$ajaxResponse = $response->getGraphObject();
}
return json_encode($ajaxResponse);
}

API Facebook Post in album user

I use graph v2.10, php sdk 5.5 I try to post on user album if is founded, but my script display an error:
Graph return an error. Permission error.
When an user try to login to my website, user give permissions publish_actions, email, user_photos to app. But graph say permission error.
$albums = $fb->get('/me/albums', $_SESSION['facebook_access_token'])->getGraphEdge()->asArray();
$album = array();
foreach($albums as $k=>$v) {
if($fetch_api['album_name'] == $v['name']) {
$album[$fetch_api['album_name']] = $v['id'] ;
}
}
if(count($album > 0)) {
$data = [
'message' => $_POST['msg'],
'source' => $fb->fileToUpload('images/gallery-img1.jpg'),
];
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->post('/'.$album[$fetch_api['album_name']].'/photos', $data, $_SESSION['facebook_access_token']);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}

How to change facebook profile pic using php

I want to make a Facebook canvas app for a cause, where I need to change the profile pic of user with his/her permission, I have fetched user's profile pic with selected viewport and placed an overlay of that cause but &makeprofile=1 is not working it redirects me to pic in theater mode. I want something like this app.
<?php
session_start();
require_once __DIR__ . '/Facebook/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => 'app',
'app_secret' => 'secret',
'default_graph_version' => 'v2.6',
]);
$helper = $fb->getCanvasHelper();
$permissions = ['email','publish_actions']; // optionnal
try {
if (isset($_SESSION['facebook_access_token'])) {
$accessToken = $_SESSION['facebook_access_token'];
} else {
$accessToken = $helper->getAccessToken();
}
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
if (isset($accessToken)) {
if (isset($_SESSION['facebook_access_token'])) {
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
} else {
$_SESSION['facebook_access_token'] = (string) $accessToken;
// OAuth 2.0 client handler
$oAuth2Client = $fb->getOAuth2Client();
// Exchanges a short-lived access token for a long-lived one
$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);
$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
}
// validating the access token
try {
$request = $fb->get('/me');
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
if ($e->getCode() == 190) {
unset($_SESSION['facebook_access_token']);
$helper = $fb->getRedirectLoginHelper();
$loginUrl = $helper->getLoginUrl('https://apps.facebook.com/my-first-qode/', $permissions);
echo "<script>window.top.location.href='".$loginUrl."'</script>";
exit;
}
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
// getting basic info about user
try {
$requestPicture = $fb->get('/me/picture?redirect=false&width=400&height=400');
$profile_request = $fb->get('/me?fields=name,first_name,last_name,email');
$picture = $requestPicture->getGraphUser();
$profile = $profile_request->getGraphNode()->asArray();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
unset($_SESSION['facebook_access_token']);
echo "<script>window.top.location.href='https://apps.facebook.com/my-first-qode/'</script>";
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
// priting basic info about user on the screen
print_r($profile);
echo $profile['name'];
echo "<img src='".$picture['url']."'/>";
//image create
$base_image = imagecreatefrompng("mark.png");
// Get the facebook profile image in 200x200 pixels
$photo = imagecreatefromjpeg($picture['url']);
//$photo = imagecreatefromjpeg("http://graph.facebook.com/".$id."/picture?width=200&height=200");
//resizeImage($photo,920,920);
// read overlay
$overlay = imagecreatefrompng("mark.png");
// keep transparency of base image
imagesavealpha($base_image, true);
imagealphablending($base_image, true);
$lwidth = imagesx($photo);
$lheight = imagesy($photo);
// place photo onto base (reading all of the photo and pasting unto all of the base)
imagecopyresampled($base_image, $photo, 0, 0, 0,0, 200, 200, $lwidth, $lheight);
// place overlay on top of base and photo
imagecopy($base_image, $overlay, 0, 0, 0, 0, 200, 200);
// Save as jpeg
imagejpeg($base_image,'sample3.jpg');
//image create
echo '<img src="sample3.jpg" alt="photo_stamp1" />';
/*$data = [
'message' => 'My awesome photo upload example.',
'source' => $fb->fileToUpload('252676_368007166708864_8124350312635670437_n.jpg'),
// Or you can provide a remote file location
//'source' => $fb->fileToUpload('https://example.com/photo.jpg'),
];
try {
$response = $fb->post('/me/photos', $data);
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Error: ' . $e->getMessage();
exit;
}
$graphNode = $response->getGraphNode();
echo 'Photo ID: ' . $graphNode['id'];
*/
// Now you can redirect to another page and use the access token from $_SESSION['facebook_access_token']
} else {
$helper = $fb->getRedirectLoginHelper();
$loginUrl = $helper->getLoginUrl('https://apps.facebook.com/my-first-qode/', $permissions);
echo "<script>window.top.location.href='".$loginUrl."'</script>";
}
You canĀ“t change the profile picture directly, like it is done in that other App. You MUST redirect the user to the uploaded photo and he can then choose to make it his profile picture on his own. The celebrate pride App is from Facebook.

Graph returned an error: (#100) source is not properly formatted for uploading with image

I have code for uploading image using SDK v4 to post on fb timeline
$linkData = [
// 'link' => 'http://www.example.com',
'message' => 'User provided message',
'source' => $fb->fileToUpload('/img/about_two.jpg'),
];
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->post('/me/photos', $linkData, 'XXXXXXXXXXX');
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
I got error msg 'Graph returned an error: (#100) source is not properly formatted'. I tried with live link for the image path,set image size to 200 X 00. But again shows same error.
Anyone can help.
Thanks

How to get profile-picture from facebook sdk?

I need get the profile-picture from facebook then user login into web app with facebook login php sdk. And then show this into
I'm using this code:
$fb = new Facebook\Facebook([
'app_id' => 'xxx...x',
'app_secret' => 'xx....xx',
'default_graph_version' => 'v2.4',
'default_access_token' => isset($_SESSION['facebook_access_token']) ? $_SESSION['facebook_access_token'] : 'xx..xx'
]);
try {
$response = $fb->get('/me?fields=id,name,email,picture');
$user = $response->getGraphUser();
$image = $user['picture'];
echo "<img src='$image' height='42' width='42'>";
exit; //redirect, or do whatever you want
} catch(Facebook\Exceptions\FacebookResponseException $e) {
//echo 'Graph returned an error: ' . $e->getMessage();
} catch(Facebook\Exceptions\FacebookSDKException $e) {
//echo 'Facebook SDK returned an error: ' . $e->getMessage();
}
How can I get the profile-picture and show this correctly?
Some error? I was checking the example of facebook docs and the access token is passing as argument with the call get()
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->get('/me?fields=id,name', '{access-token}');
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$user = $response->getGraphUser();
echo 'Name: ' . $user['name'];
https://developers.facebook.com/docs/php/howto/example_retrieve_user_profile/5.0.0
Edit. If doesn't work try this example: http://www.howtobloger.com/2013/10/how-to-access-app-user-name-and-profile.html
You assigned at $image the array picture:
"picture": {
"data": {
"height": 50,
"is_silhouette": false,
"url": "...",
"width": 50
}
}
So to retrieve the url you should use $image['url'].
PS The question is from 4 years ago, this answer is for reference

Categories