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);
}
Related
I need help. i have a group of photos ususally selected from the database. I loop around an array say $piclinks then look around it to post the pictures. But i need it to display as one post as shown in the image.
Those any one have an idea what i can do
<?php
define('FACEBOOK_SDK_V4_SRC_DIR', __DIR__.'/src/Facebook/');
require_once(__DIR__.'/src/Facebook/autoload.php');
$fb = new Facebook\Facebook([
'app_id' => '2082153322064454',
'app_secret' => '7355d8111164630537a35b43a1bbd336',
'default_graph_version' => 'v2.2',
]);
$piclinks = array("http://d24w6bsrhbeh9d.cloudfront.net/photo/agydwb6_460s.jpg", "https://greenparrotnews.com/wp-content/uploads/2017/10/Boss-Mustapha.jpg");
foreach($piclinks as $selectedlink){
//Post property to Facebook
$linkData = [
'message' => 'Your message here',
'url' => $selectedlink
];
$pageAccessToken ='EAAdltRkhKkYBAHbLBwoKDZBSat5ulIJZBbMz4gZBQSZCZCY7oH7vRgN16QBuYZBZAjSzZCvt04ypoRaTG4o5jdhXA9bjIV8ZB0ZAJ8lsVNZBxYRInwn7tc8ZBsmdhxKZBnmHCA0n3k3wOOlQRcpDPQyXa1RZBoSb3ZAasDhHHxQfTeRdPObE68OZAAxMoaDcs';
try {
$response = $fb->post('/me/photos', $linkData, $pageAccessToken);
} 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 'done';
}
?>
You have to upload your photos first, then you will get photo id. After that you have to create post with all photo ids. It may be helpful for you. Example code:
$images = []; // say your all images is in array.
foreach($images as $image){
try {
$response = $fb->post("/PAGEID/photos", [
'source' => $fb->fileToUpload($image),
'published' => 'false'
], PAGE_TOKEN);
} catch(Facebook\Exceptions\FacebookSDKException $e) {
$errors[]= 'Error: ' . $e->getMessage();
}
$graphNode = $response->getGraphNode();
$post_images[]= $graphNode['id'];
}
foreach ($post_images as $key => $post_image) {
$attachMedia[$key] = ['media_fbid' => $post_image];
}
try {
$response = $fb->post(
"/PAGEID/feed",
[
'message' => YOUR_MESSAGE,
'attached_media' => $attachMedia
],
PAGE_TOKEN
);
} catch(Facebook\Exceptions\FacebookSDKException $e) {
$errors[]= 'Error: ' . $e->getMessage();
}
$post = json_decode($response->getBody());
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 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.'"');
I would like to use the Facebook PHP SDK v5 to automatically post to a FAN PAGE. I have the code below which successfully posts to my own wall, but how do I alter the code to post to my fan page? From what I've read, I need to pass in the page id of the fan page?
$params["message"] = 'test';
$params["link"] = 'https://example.com';
$params["picture"] = 'https://example.com/images/logo_hod.jpg';
$params["description"] = 'testtt';
$access_token = $accessToken;
try {
$response = $fb->post('/me/feed', $params, $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 'Posted with id: ' . $graphNode['id'];
I tried replacing this line:
$response = $fb->post('/me/feed', $params, $access_token);
with this to substitute in the fan page id:
$response = $fb->post('/229107363768165/feed', $params, $access_token);
and got the error:
Graph returned an error: Unsupported post request.
UPDATE: I also made the app "public" in an attempt to get past the "unsupported post request" error. No luck.
i think your $params is not right.
I use this:
$fb = new Facebook\Facebook([
'app_id' => '{app-id}',
'app_secret' => '{app-secret}',
'default_graph_version' => 'v2.2',
]);
$params = [
'link' => 'https://example.com',
'photo' => 'https://example.com/images/logo_hod.jpg',
'message' => 'A test post!',
];
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->post('/me/feed', $params, '{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;
}
Post Links Using the Graph API
I assumes that you've already obtained an access token and the access token must have the publish_actions permission for this to work.
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