Facebook graph return only the id - php

I used Facebook SDK for get share count for posts in web site, but when I send the request the responses is only the id
$link = 'url-i-want-get-share-count';
$access_token="My-Access-token";
$app_id="My-app-id";
$app_secret="My-app-secret";
$fb = new \Facebook\Facebook([
'app_id' => $app_id,
'app_secret' => $app_secret,
'default_graph_version' => 'v2.5',
'default_access_token' => $access_token, // optional
]);
try {
$request = $fb->get('/' . $link . '/posts?fields=og_object{engagement}');
} catch(\Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
} catch(\Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
}
var_dump($request);
the response is

Related

how to post a link on facebook use sdk PHP

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!!!

Post daily on Facebook with the Facebook SDK

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

Post Image on Facebook Page Feed as Page

Using this example from fb-sdk:
require_once 'fb-sdk/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => '123456',
'app_secret' => 'abc1233',
'default_graph_version' => 'v2.6',
]);
$data = [
'message' => 'Testupload',
'source' => $fb->fileToUpload('image.jpg'),
];
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->post('/PageID/photos', $data, 'XXXXXXYYYYYY');
} 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'];
i am able to post as myself to the wall of my facebook-page.
'app_id' and 'app_secret' come from the app i created for this.
If i set $response = $fb->post('/me/photos', $data, 'XXXXXXYYYYYY'); the post shows up on my timeline.
What do i need to change, so get a photo, posted by "Page" on the feed of "Page"?
You need to use a Page Token with the publish_pages permission to post "as Page". You can get a Page Token with the following API call, after authorizing with manage_pages and publish_pages:
/page-id?fields=access_token
More information:
https://developers.facebook.com/docs/facebook-login/access-tokens
http://www.devils-heaven.com/facebook-access-tokens/

Facebook PHP SDK Graph returned an error: Invalid OAuth access token

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.

Graph API method not working

Just one day before the following codes working for me but now it's not working for me.
require 'facebook.php';
$facebook = new Facebook(array( 'appId' =>'APPID','secret' => 'some_secret'));
$user = $facebook->getUser();
if ($user){$user_profile = $facebook->api('/me');}
it's not showing the current loggged in user name or id. Is n't it working anymore ???
Looks like you are using some deprecated api/code examples, current facebook docs have this snippet:
$fb = new Facebook\Facebook([
'app_id' => '{app-id}',
'app_secret' => '{app-secret}',
'default_graph_version' => 'v2.2',
]);
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->getName();

Categories