prblem with post on page of facebook with api - php

I wrote a code for post a message on my page (not own timeline)
post.php
<?php
require_once __DIR__ . '/vendor/autoload.php';
session_start();
$fb = new Facebook\Facebook([
'app_id' => 'xxxxxxxxxxxxxx',
'app_secret' => 'xxxxxxxxxxxxxx',
'default_graph_version' => 'v2.5',
]);
$data = [
'message' => 'My awesome photo upload example.',
];
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->post('/xxxxxxxxxxxxxx/feed', $data, $_SESSION['facebook_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'];
index.php
<?php
require_once __DIR__ . '/vendor/autoload.php';
session_start();
$app_id = 'xxxxxxxxxxxxxx';
$app_secret = 'xxxxxxxxxxxxxx';
$fb = new Facebook\Facebook([
'app_id' => $app_id,
'app_secret' => $app_secret,
'default_graph_version' => 'v2.4',
'default_access_token' => isset($_SESSION['facebook_access_token']) ? $_SESSION['facebook_access_token'] : 'APP-ID|APP-SECRET'
]);
try {
$response = $fb->get('/xxxxxxxxxxxxxx?fields=id,name');
$user = $response->getGraphUser();
echo 'Name: ' . $user['name'];
//exit; //redirect, or do whatever you want
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
}
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email', 'user_likes', 'manage_pages', 'publish_actions'];
$loginUrl = $helper->getLoginUrl('http://xxxxxxxxxxxxxx/facebook/login-callback.php', $permissions);
echo 'Log in with Facebook!';
login-callback.php
<?php
require_once __DIR__ . '/vendor/autoload.php';
session_start();
$app_id = 'xxxxxxxxxxxxxx';
$app_secret = 'xxxxxxxxxxxxxx';
$fb = new Facebook\Facebook([
'app_id' => $app_id,
'app_secret' => $app_secret,
'default_graph_version' => 'v2.4',
'default_access_token' => 'APP-ID|APP-SECRET'
]);
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
} 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();
}
if (isset($accessToken)) {
// Logged in!
$_SESSION['facebook_access_token'] = (string) $accessToken;
} elseif ($helper->getError()) {
// The user denied the request
}
header('Location: index.php');
It's just work when in file post.php use this code:
$response = $fb->post('/me/feed', $data, $_SESSION['facebook_access_token']);
And then post show on my timeline.
But when I want to post on page and use this:
$response = $fb->post('/xxxxxxxxxxxxxx/feed', $data, $_SESSION['facebook_access_token']);
don't work and not appears anythings, and don't show my post on the page or timeline!

Related

facebook login graph returned an error

login.php codes
if(!session_id()) {
session_start();
}
require_once __DIR__ . '/vendor/autoload.php';
$facebook_appId = '123';
$facebook_appSecret = '123';
$fb = new Facebook\Facebook([
'app_id' => $facebook_appId,
'app_secret' => $facebook_appSecret,
'default_graph_version' => 'v2.11',
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email']; // Optional permissions
$loginUrl = $helper->getLoginUrl('http://www.123.com/login-callback.php', $permissions);
echo 'Login via Facebook!';
login-callback.php codes
// error_reporting (E_ALL & ~E_NOTICE);
if(!session_id()) {
session_start();
}
require_once __DIR__ . '/vendor/autoload.php';
$facebook_appId = '123';
$facebook_appSecret = '123';
$fb = new \Facebook\Facebook([
'app_id' => $facebook_appId,
'app_secret' => $facebook_appSecret,
'default_graph_version' => 'v2.11',
]);
// Use one of the helper classes to get a Facebook\Authentication\AccessToken entity.
// $helper = $fb->getRedirectLoginHelper();
// $helper = $fb->getJavaScriptHelper();
// $helper = $fb->getCanvasHelper();
// $helper = $fb->getPageTabHelper();
try {
// Get the \Facebook\GraphNodes\GraphUser object for the current user.
// If you provided a 'default_access_token', the '{access-token}' is optional.
$helper = $fb->getRedirectLoginHelper();
$accessToken = $helper->getAccessToken();
$response = $fb->get('/me?fields=id,name', $accessToken );
// $response = $fb->get('/me', $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;
}
$me = $response->getGraphUser();
echo 'Logged in as ' . $me->getName();
When I click on the "login.php" output link, it jumps to the "login-callback.php" and prints the following error message. How do I solve this problem?
Graph returned an error: 无法加载网址: 这个 URL 的域名未包含应用的域名。为了能够加载 URL,请在应用设置的应用域名字段添加应用的所有域和子域。

error FacebookResponseException when use SDK PHP to post a link

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

Access Token not generated in facebook PHP sdk5?

I am trying from last day to post on user timeline and for this I am using facebook PHP SDK5. I have two files one is fb-integration.php and second is login-callback.php
In fb-intergration.php - I have following code which is basically to check user login or not.
<?php
session_start();
require_once __DIR__ . '/src/Facebook/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => '2426534xxxxxxxx',
'app_secret' => 'dc36a92xxxxxxxxxxxxxxxxxxxxxxx',
'default_graph_version' => 'v2.8',
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email', 'publish_actions']; // optional
$loginUrl = $helper->getLoginUrl('https://example.com/events/test/login-callback.php', $permissions);
echo "<script>window.top.location.href='".$loginUrl."'</script>";
?>
In login-callback file - I have following script:
<?php
session_start();
require_once __DIR__ . '/src/Facebook/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => '2426534xxxxxxxx',
'app_secret' => 'dc36a92xxxxxxxxxxxxxxxxxxxxxxx',
'default_graph_version' => 'v2.8',
]);
$helper = $fb->getCanvasHelper();
try {
if (isset($_SESSION['facebook_access_token'])) {
$accessToken = $_SESSION['facebook_access_token'];
} else {
$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 ($accessToken) {
echo 'Successfully logged in!';
}else{
echo 'Sorry not generated';
}
?>
Every time, I am getting result Sorry not generated.
Where am i doing wrong. why is not my accesToken generating?

Failed to connect to graph.facebook.com port 443: Connection refused

I am new to FB graph API and I try to include the FB login in my website, I following the official FB login tutorial from their website, But after login I get the following error:
Failed to connect to graph.facebook.com port 443: Connection refused
Login.php
<?php
session_start();
require_once __DIR__ . '/fbsdk/src/Facebook/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => 'xxx',
'app_secret' => 'xxx',
'default_graph_version' => 'v2.3',
// . . .
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['public_profile', 'email']; // optional
$loginUrl = $helper->getLoginUrl('http://-myserver-/login-callback.php', $permissions);
echo 'Log in with Facebook!';
?>
login-callback.php
<?php
session_start();
require_once __DIR__ . '/fbsdk/src/Facebook/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => 'xxx',
'app_secret' => 'xxx',
'default_graph_version' => 'v2.3',
// . . .
]);
$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;
// Now you can redirect to another page and use the
// access token from $_SESSION['facebook_access_token']
}
?>
example-canvas-app.php
<?php
session_start();
require_once __DIR__ . '/fbsdk/src/Facebook/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => 'xxx',
'app_secret' => 'xxx',
'default_graph_version' => 'v2.3',
// . . .
]);
$helper = $fb->getCanvasHelper();
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.
}
?>
I already searched in stack and google but I can't find any solution
Hostinger's free server does not allow a connection to Fb's SDK as mentioned by their customer service.

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