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!!!
Related
i'm using sdk php to post a link. here is my function (https://developers.facebook.com/docs/php/howto/postwithgraphapi/)
public function shareFB(Request $request, $slugify)
{
$petition = Petitions::where('slug', $slugify)->first();
// $access_token = auth()->user()->social_account->access_token;
$fb = new Facebook([
'app_id' => config("services.facebook.client_id"),
'app_secret' => config("services.facebook.client_secret"),
'default_graph_version' => 'v2.2',
]);
$helper = $fb->getRedirectLoginHelper();
$accessToken = $helper->getAccessToken();
$linkData = [
'link' => route('petitions.index',$petition->slug),
'message' => $request->get('comment-fb'),
];
try {
$response = $fb->post('/me/feed', $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();
return redirect()->route('petitions.index',$petition->slug);
}
and error is Invalid OAuth access token, maybe i get wrong access token. Anyone help me solve that/ Thank u
first i want to ask if its possible to post on a facebook page all day a post with the PHP SDK.
Here is my version (it works but the token expires after some hours)
<?php
define('FACEBOOK_SDK_V4_SRC_DIR','../sdks/facebook/');
require_once("../sdks/facebook/autoload.php");
$access_token ="EAACEdEose0cBAGVFnr3vE8U6FYXxsJbRadxZB4ynIWaG5aFzDo0bx6vdIWysh4sPvnOPu3CHZCfLSM9MYLRRlvdeyJazIXgP0h3fqbbNc3oxg24b4I7rZChUS1FNHqm1HsWbym4IjsaoVw4fAqRlYfPsw2iX4nFZB7KDeAu1BglrRg2qGug8";
$app_secret ="c19ca5fc56bc0ddce32c75a3cb53059f";
$fb = new Facebook\Facebook([
'app_id' => '17852532523721',
'app_secret' => $app_secret,
'default_graph_version' => 'v2.4',
]);
$linkData = [
'link' => 'http://www.url.com/',
'message' => "Test"
];
try {
$response = $fb->post("/42935243503912968/feed",$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;
}
?>
It's possibole to get the actual acces_token of my users. Maybe someone now a solution.
Big thanks
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 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 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.