I am trying to upload the photo to user's profile through my code. Below is the code I am using:
echo $imageName;
$data = [
'message' => 'Visit http://www.mywebsite.com',
'source' => $fb->fileToUpload($imageName),
];
echo 'hi1';
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->post('/me/photos', $data, $accessToken);
} 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;
}
echo 'hi';
In above code echo $imageName is printing the required image name (variable contains desired value say images/abc.jpg) but when use the following it is not working:
$fb->fileToUpload($imageName)
If I remove variable name and give direct value like so it is working fine:
$fb->fileToUpload("images/abc.jpg") .
Can you please help me with whats wrong with my code.
Provide full path of image(http://example.com/image/1.jpg)
Try below one.
This is tested in version 5
require_once __DIR__ . '/src/Facebook/autoload.php';
$app_ID = 'xxxxxxxxx';
$APP_SECRET = 'xxxxxxxxxx';
$access_token = 'xxxxxxxxxx';
$fb = new Facebook\Facebook([
'app_id' => $app_ID,
'app_secret' => $APP_SECRET,
'default_graph_version' => 'v2.5',
]);
$data = [
'message' => 'My photo upload example.',
'source' => $fb->fileToUpload('http://example.com/images/1.jpg'),
];
try {
$response = $fb->post('/me/photos', $data, $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();
echo 'Photo ID: ' . $graphNode['id'];
just try with concatenation like $fb->fileToUpload('"'.$imageName.'"');
Related
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);
}
I have created a page with this code:
require_once __DIR__ . '/facebook/src/Facebook/autoload.php'; // change path as needed
$config = array();
$fb = new \Facebook\Facebook([
'app_id' => 'myappid',
'app_secret' => 'myappsecret',
'default_graph_version' => 'v2.10',
'default_access_token' => 'myaccesstoken', // optional
]);
$page_id='mypageid';
$request = $fb->request('GET', '/'.$page_id.'/');
// Send the request to Graph
/* PHP SDK v5.0.0 */
/* make the API call */
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->get(
'mypageid/ratings?fields=open_graph_story&access_token=myaccesstoken'
);
} 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;
}
$graphEdge = $request->getGraphEdge();
If I print with echo graphEdge results are: https://allevamentochihuahua.com/recensioni.php
How can print all reviews in a table?
I've try to print an array but not work...
foreach ($graphEdge as $reviews) {
echo "<ul>\n<li>" . $contact['review_text'] . " : " . $review['value'] . "</li></ul>\n";
}
But not work.. also I need insert a name of user that review my page, but I don't find in grapEdge..
Can you help me?
i'm creating function to share a post to facebook and i'm following docs : https://developers.facebook.com/docs/php/howto/postwithgraphapi. but it's not work.
This is my function
public function shareFB(Request $request, $slugify)
{
$petition = Petitions::where('slug', $slugify)->first();
$access_token = auth()->user()->social_account->access_token;
$fb = new Facebook\Facebook([
'app_id' => config("services.facebook.client_id"),
'app_secret' => config("services.facebook.client_secret"),
'default_graph_version' => 'v2.2',
]);
$linkData = [
'link' => route('petitions.index',$petition->slug),
'message' => $request->get('shareFB'),
];
try {
$response = $fb->post('/me/feed','post', $linkData, $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;
}
$share = $response->getGraphNode();
}
and here is my route in laravel
Route::post('share/p/{slugify}','Frontend\SocialController#shareFB')->name('social.shareFB');
When i clicked a button have href above, it's show MethodNotAllowedHttpException . somebody help me solve that!!!
I don't why I am getting this error.
My appid and secret id are all correct. Don't know what went wrong.
<?PHP
$fb = new Facebook\Facebook([
'app_id' => '',
'app_secret' => '',
'default_graph_version' => 'v2.5',
]);
$data = [
'message' => 'My awesome photo upload example.',
'source' => $fb->fileToUpload('C:/xampp/htdocs/laravel5-learning/public/image/share/'.$name),
];
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->post('/me/photos', $data, '{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();
echo 'Photo ID: ' . $graphNode['id'];
?>
It is showing this error:
Graph returned an error: Invalid OAuth access token.
I have trying to retrieve user profile in my website.
But after I get the access token, I can't use it. The result it show is
Graph returned an error: Invalid OAuth access token
I can get the access token by below code
Login.php
session_start();
require_once __DIR__ . '/facebook-php-sdk-v4-5.0-dev/src/Facebook/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => 'I have insert correct app id',
'app_secret' => 'and app secret',
'default_graph_version' => 'v2.0',
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email'];
$loginUrl = $helper->getLoginUrl('http://myweb.com/login-callback.php', $permissions);
echo 'Log in with Facebook!';
?>
And below is login-callback.php
<?php
session_start();
require_once __DIR__ . '/facebook-php-sdk-v4-5.0-dev/src/Facebook/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => 'I have insert correct app id',
'app_secret' => 'and app secret',
'default_graph_version' => 'v2.0',
]);
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
} 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;
}
if (! isset($accessToken)) {
if ($helper->getError()) {
header('HTTP/1.0 401 Unauthorized');
echo "Error: " . $helper->getError() . "\n";
echo "Error Code: " . $helper->getErrorCode() . "\n";
echo "Error Reason: " . $helper->getErrorReason() . "\n";
echo "Error Description: " . $helper->getErrorDescription() . "\n";
} else {
header('HTTP/1.0 400 Bad Request');
echo 'Bad request';
}
exit;
}
$_SESSION['fb_access_token'] = (string) $accessToken;
try {
$response = $fb->get('/me?fields=id,name', "'".$_SESSION['fb_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'];
?>
Am I doing something wrong? Please help. Thanks.
I only have:
$response = $fb->get('/me?fields=id,name', $token);
$user = $response->getGraphUser();
It works perfectly
I think You should change your code :
"'".$_SESSION['fb_access_token']."'" to $accessToken
and try again