how to print facebook reviews - php

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?

Related

Facebook graph return only the id

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

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

How to get local insights of Facebook page using graph API?

I want to get following local insights of Facebook page using graph API
People Nearby:Hourly
Weekly
Overall
Check-ins
Please see the screen shot.
Image
You can get page insights by using this code and ask for page permissions ('manage_pages','pages_manage_cta') , and get page id first from your likes or managed pages and replace you page id in code with YOUR_PAGE_ID.
use FB php sdk version: facebook-php-sdk-v4-5.0-dev.
$fb = new Facebook\Facebook([
'app_id' => APP_ID,
'app_secret' => APP_SECRET,
'default_graph_version' => 'v2.4', // or use v2.5 latest version
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['manage_pages','pages_manage_cta'];
$redirectUrl = 'http://localhost/fbapp.php';
$loginUrl = $helper->getLoginUrl($redirectUrl, $permissions);
echo 'Log in with Facebook!';
After generating login url implement the code for response handling and getting required data.
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
setcookie('accessToken',$accessToken);
} 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;
}
$fb->setDefaultAccessToken($accessToken);
// Get user groups detail
$requestPageInsights = $fb->request('GET', '/YOUR_PAGE_ID/insights');
//Make a batch request
$batch = ['page-insights' => $requestPageInsights];
try {
$responses = $fb->sendBatchRequest($batch);
} 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;
}
for a better view can use this:
foreach ($responses as $key => $response) {
if ($response->isError()) {
$e = $response->getThrownException();
echo '<p>Error! Facebook SDK Said: ' . $e->getMessage() . "\n\n";
echo '<p>Graph Said: ' . "\n\n";
var_dump($e->getResponse());
} else {
echo "<p>(" . $key . ") HTTP status code: " . $response->getHttpStatusCode() . "<br />\n";
echo "Response: " . $response->getBody() . "</p>\n\n";
echo "<hr />\n\n";
}
}
And for getting other details visit on and use your related params/scope https://developers.facebook.com/docs/graph-api/reference/v2.5/insights

$fb->fileToUpload($imageName) not working properly

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.'"');

Trouble in facebook PHP SDK, can't make batch requests

I am trying to build an application that can retrieve these data from a group that I manage:
-> member's name
-> the admin who added each member
I've 3 separate files :
index.php: contains the logging using group permissions:
<?php
session_start();
require_once __DIR__ . '/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => censored,
'app_secret' => censored,
'default_graph_version' => 'v2.4', // or use v2.5 latest version
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['user_managed_groups'];
$redirectUrl = 'http://localhost/test/fb/vendor/fbapp.php';
$loginUrl = $helper->getLoginUrl($redirectUrl, $permissions);
echo 'Log in with Facebook!';
?>
fbapp.php: retrieve the Facebook Access Token.
<?php
session_start();
require_once __DIR__ . '/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => censored,
'app_secret' => "censored",
'default_graph_version' => 'v2.4', // or use v2.5 latest version
]);
helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
} 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;
}
if (isset($accessToken)) {
// Logged in!
$_SESSION['facebook_access_token'] = (string) $accessToken;
echo "Logged in \n";
$redirectUrl = 'http://localhost/test/fb/vendor/traitement.php';
echo 'nextStep';
// Now you can redirect to another page and use the
// access token from $_SESSION['facebook_access_token']
}
?>
traitement.php: perform graph requests (here where problems appears)
<?php
session_start();
require_once __DIR__ . '/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => censored,
'app_secret' => "censored",
'default_graph_version' => 'v2.4', // or use v2.5 latest version
]);
$accessToken = $_SESSION['facebook_access_token'];
$fb->setDefaultAccessToken($accessToken);
// Get user groups detail
$requestUserManagedGroups = $fb->request('GET', '/me/groups?fields=members,from');
//Make a batch request, this is not working, why ?
$batch = ['user-groups' => $requestUserLikes];// anyother request is not working (tried to change $requestUserLikes by other values..)
try {
$responses = $fb->sendBatchRequest($batch);
} 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;
foreach ($responses as $key => $response) {
if ($response->isError()) {
$e = $response->getThrownException();
echo '<p>Error! Facebook SDK Said: ' . $e->getMessage() . "\n\n";
echo '<p>Graph Said: ' . "\n\n";
var_dump($e->getResponse());
} else {
echo "<p>(" . $key . ") HTTP status code: " . $response->getHttpStatusCode() . "<br />\n";
echo "Response: " . $response->getBody() . "</p>\n\n";
echo "<hr />\n\n";
}
}
?>
I come up with this error msg:
Notice: Undefined variable: requestUserLikes in/home/yassine/srv/test/fb/vendor/traitement.php on line 20
Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Argument for add() must be of type array or FacebookRequest.' in /home/yassine/srv/test/fb/vendor/facebook/php-sdk-v4/src/Facebook/FacebookBatchRequest.php:85 Stack trace: #0 /home/yassine/srv/test/fb/vendor/facebook/php-sdk-v4/src/Facebook/FacebookBatchRequest.php(78): Facebook\FacebookBatchRequest->add(NULL, 'user-groups') #1 /home/yassine/srv/test/fb/vendor/facebook/php-sdk-v4/src/Facebook/FacebookBatchRequest.php(61): Facebook\FacebookBatchRequest->add(Array) #2 /home/yassine/srv/test/fb/vendor/facebook/php-sdk-v4/src/Facebook/Facebook.php(492): Facebook\FacebookBatchRequest->__construct(Object(Facebook\FacebookApp), Array, Object(Facebook\Authentication\AccessToken), 'v2.4') #3 /home/yassine/srv/test/fb/vendor/traitement.php(23): Facebook\Facebook->sendBatchRequest(Array) #4 {main} thrown in /home/yassine/srv/test/fb/vendor/facebook/php-sdk-v4/src/Facebook/FacebookBatchRequest.php on line 85
EDIT: I fixed this error by simply noticing that i needed to change $requestUserLikes by $requestUserManagedGroups, !!
I come up with a new problem is that I'm not being able to see the groups that I manage using this request: "me?fields=groups"
I'm not able to see some good results even in https://developers.facebook.com/tools/explorer/ can anyone please try if they can see their groups using the facebook explorer ?

Categories