Facebook SDK 5.3 returning empty 'data' array in the body - facebook

Using the following code:
$postId = "<userid>_<post_id>";
$this->facebook = new Facebook([
'app_id' => $container->getParameter('facebook_client_id'),
'app_secret' => $container->getParameter('facebook_client_secret'),
'default_graph_version' => 'v2.5',
]);
$app = $this->facebook->getApp();
$request = new FacebookRequest(
$app,
'<access_token>',
'GET',
'/' . $postId . '/sharedposts?fields=from,via'
);
$response = $this->facebook->getClient()->sendRequest($request);
print_R($response); exit();
returns the following:
[body:protected] => {"data":[]}
[decodedBody:protected] => Array
(
[data] => Array
(
)
)
But I can clearly see, within Facebook, that there is a comment, a share and a like on the post Id i'm querying
My end goal is to view how many times a post to FB has been shared by other FB users

Related

How do I output the campaign status from the ad account insight level (Facebook marketing api)

I am trying to output a list of all campaigns in an ad account with a few insights with it.
I was able to get all the data I wanted except the campaign effective status, for some reason its not being called out and is showing an error.
The Facebook quick start compiler shows that 'delivery' is the proper keyword to get the status.
Any help would be appreciated.
use FacebookAds\Object\AdAccount;
use FacebookAds\Object\AdsInsights;
use FacebookAds\Api;
use FacebookAds\Logger\CurlLogger;
$access_token = 'EAAXYydBVCvUBAHBsZCegoqntZ234234ZD';
$ad_account_id = 'act_123';
$app_secret = '1123123';
$app_id = '123123123';
$api = Api::init($app_id, $app_secret, $access_token);
$api->setLogger(new CurlLogger());
$fields = array(
'campaign_id',
'delivery',
'clicks',
'campaign_name',
);
$params = array(
'level' => 'campaign',
'filtering' => array(),
'breakdowns' => array(),
'time_range' => array('since' => '2017-08-08','until' => '2019-09-26'),
);
return (new AdAccount($ad_account_id))->getInsights(
$fields,
$params
)->getResponse()->getContent();

Facebook star ratings on website

I'm new to Facebook API and trying to get a page's star ratings on a web page, but not getting far...
<?php
require_once __DIR__ . '/vendor/autoload.php'; // change path as needed
$fb = new \Facebook\Facebook([
'app_id' => '{API_ID_HERE}',
'app_secret' => '{API_SECRET_HERE}',
'default_graph_version' => 'v2.10',
//'default_access_token' => '{ACCESS_TOKEN_HERE}', // optional
]);
/* PHP SDK v5.0.0 */
/* make the API call */
$request = new FacebookRequest(
$session,
'GET',
'/PAGE_ID_HERE/ratings'
);
$response = $request->execute();
$graphObject = $response->getGraphObject();
/* handle the result */
?>
I don't expect it too be right 100% but trying to a connection at least, but I keep getting;
Class 'FacebookRequest' not found
Which I've googled but not found anything constructive, I've got composer etc. but not sure what I'm missing
Cheers
Update
I found a solution to that error, but not getting a whole new one which doesn't make sense...
Notice: Undefined variable: session in C:\xampp\htdocs\phptests\php-graph-sdk\index.php on line 33
Warning: parse_url() expects parameter 1 to be string, array given in C:\xampp\htdocs\phptests\php-graph-sdk\src\Facebook\Url\FacebookUrlManipulator.php on line 109
Warning: parse_url() expects parameter 1 to be string, array given in C:\xampp\htdocs\phptests\php-graph-sdk\src\Facebook\Url\FacebookUrlManipulator.php on line 43
Fatal error: Call to undefined method Facebook\FacebookRequest::execute() in C:\xampp\htdocs\phptests\php-graph-sdk\index.php on line 41
<?php
require_once __DIR__ . '/vendor/autoload.php'; // change path as needed
use Facebook\FacebookRequest;
// PHP GRAPH SDK 5.5
function QueryToRetrieveUserThroughID(string $id) {
$newFacebook = new Facebook\Facebook([
'app_id' => '',
'app_secret' => '',
'default_graph_version' => 'v2.5',
]);
$newFacebookApp = $newFacebook->getApp();
$response = $newFacebook->get($id, '');
// or $response = $newFacebook->get($id);
$graphObject = $response->getGraphObject();
}
$request = new FacebookRequest(
$session,
'GET',
'//ratings',
array(
'fields' => 'rating'
)
);
$response = $request->execute();
$graphObject = $response->getGraphObject();
/* handle the result */
?>

How can I get the pages that a Facebook account has with SDK 5.0?

From here: https://developers.facebook.com/docs/graph-api/reference/user/accounts/ I got to write this in my code
$request = new FacebookRequest(
$session,
'GET',
'/{user-id}/accounts'
);
$response = $request->execute();
The problem here is with version 2.8 of the API and SDK 5.0 execute() doesn't exist. How can I get a list of all the pages on a Facebook account.
#JayNCoke was correct with the approach.
$fb = new Facebook\Facebook([
'app_id' => '{app-id}',
'app_secret' => '{app-secret}',
'default_graph_version' => 'v2.5',
]);
// Sets the default fallback access token so we don't have to pass it to each request
$fb->setDefaultAccessToken('{access-token}');
$response = $fb->get('{user-id}/accounts');
$response returns a Facebook\FacebookResponse object
To get it in a usable data format, just call getDecodedBody() like below
$response = $response->getDecodedBody();
This returns an array.
You have the right endpoint, you just need to make sure you have the right call in PHP:
$fb = new Facebook\Facebook([
'app_id' => '{app-id}',
'app_secret' => '{app-secret}',
'default_graph_version' => 'v2.5',
]);
// Sets the default fallback access token so we don't have to pass it to each request
$fb->setDefaultAccessToken('{access-token}');
$response = $fb->get('{user-id}/accounts');
Also remember:
You'll need to make sure your app is asking for the manage_pages permission.
This only returns pages that the user is an Admin on. If they are of any other role, it won't return that page.

Facebook PHP SDK - An active access token must be used to query information about the current user

I have problem with Facebook PHP SDK. It always throws that exception. I tried many solutions listed here, but nothing works for me.
It seems that Facebook returns to me valid access token, because I tested it with Debug tool in dashboard of my application.
What's my scenario?
I want to post to publish simple content to user's wall by calling static function:
function social_publish($network, $title, $message, $link = '', $image = '') {
global $_config;
// Initialize Facebook SDK
$facebook = new Facebook(array(
'appId' => $_config['fb_app']['app_id'],
'secret' => $_config['fb_app']['app_security_key']
));
// Set data
$attachment = array(
'name' => $title,
'caption' => $title,
'message' => $message,
'link' => $link,
'picture' => $image,
'actions' => array('name' => 'Test', 'link' => 'Link')
);
try {
$access_token = $facebook->getAccessToken(); // returns valid access token
$uid = $facebook->getUser(); // always return 0
$result = $facebook->api( '/' . $_config['fb_profile'] . '/feed/', 'post', $attachment); // $_config['fb_profile'] procudes 'me' in this case
} catch (FacebookApiException $e) {
echo $e->getMessage();
}
}
Just to note: I am not working on local environment.
problem solve it as asked in scope request to Facebook for authentication, as I decided to use the JavaScript SDK-and then, here's the solution:
FB.getLoginStatus(function(response) {
if ( ! response.authResponse ) {
FB.login(function(response) {
// handle the response if needed
}, {scope: 'publish_actions, publish_stream'});
}
});
Thank you! :-)

Is Facebook batch request photo upload with no_story possible?

Is it?
Uploading works like a charm, but I don't want it to show up on timeline or feed.
I've tried adding no_story to each request, and to the $params which is commented out in following code:
foreach ($data['Ad']['Media'] as $medium) {
$file = $medium['tmp_name'];
$req = array(
'method' => 'POST',
'relative_url' => '/me/photos',
'message' => $data['Ad']['title'],
'attached_files' => 'file' . $count,
'no_story' => 1 //Also tried 'true'
);
//add this request to batch
$batch[] = json_encode($req);
$params['file' . $count] = '#' . realpath($file);
$count++;
}//foreach
$params['batch'] = '[' . implode(',', $batch) . ']';
//$params['no_story'] = true;
Edit ##
See comment below.
i know you may not need the answer any more, but i was facing same issue , and after a lot of try and error i figured it out so i will post the answer may be some one face this issue again
$req = array(
'method' => 'POST',
'relative_url' => '/me/photos',
'attached_files' => 'file' . $count,
'body' => 'no_story=1'
);