I'm playing around a bit with Facebook API + PHP SDK but I'm stuck..
I'm trying to fetch all my wall posts and (if attached) the images.
Fetching all my posts goes fine but it doesn't automatically include the image. So my main question is 'How can I add fields to the fetch command'
Here is the code I'm using now:
/* handle the result */
try {
$response = $fb->get('/me/posts', $token);
} 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;
}
// Page 1
$feedEdge = $response->getGraphEdge();
According to the facebook documents I should use this:
$request = new FacebookRequest(
$session,
'GET',
'/me/posts',
array(
'fields' => 'full_picture,message'
)
);
$response = $request->execute();
$graphObject = $response->getGraphObject();
/* handle the result */
But the execute and getGraphobject method don't even exist in my SDK files (which is the latest version)
To get specific fields, you can try changing
$response = $fb->get('/me/posts', $token);
to
$response = $fb->get('/me/posts?fields=full_picture,message', $token);
Related
how can I make a php code for facebook to automatically update the cache of a single url
I saw something similar in other threads, but did not manage to make it work:
Is there an API to force Facebook to scrape a page again?
Ask facebook to re-scrape URL
For example use this php code, adding the url of my web www.tolchx.net, but it still appears the same GIF when I go to debugging:
<?php
try {
$params = [
'id' => 'https://www.tolchx.net',
'scrape' => 'true',
];
$response = $fb->post('/', $params);
print_r($response);
} 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();
}
?>
With that enough to automatically update the link cache www.tolchx.net?
thanks for the reply
I am trying to list all videos from a Facebook Fan Page an i got some problems. I keep getting Facebook SDK returned an error: You must provide an access token.. I tried using the access token, but no use.
CODE :
use Facebook;
use Facebook\FacebookApp;
use Facebook\FacebookRequest;
use FileUpload\FacebookFile;
use FileUpload\FacebookVideo;
use Facebook\Authentication\AccessToken;
$expires = time() + 60 * 60 * 2;
$accessToken = new Facebook\Authentication\AccessToken('mytoken1234567890', $expires);
var_dump($accessToken);
$fb = new Facebook\Facebook([
'app_id' => 'XXX',
'app_secret' => 'XXX',
'access_token' => "mytoken1234567890",
'default_graph_version' => 'v2.5',
]);
$request = $fb->request('POST', '/uniladmag/videos');
try
{
$response = $fb->getClient()->sendRequest($request);
}
catch(Facebook\Exceptions\FacebookResponseException $e)
{
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;
}
$graphNode = $response->getGraphNode();
print_r($graphNode);
?>
OUTPUT :
object(Facebook\Authentication\AccessToken)#2 (2) {
["value":protected]=>
string(17) "mytoken1234567890"
["expiresAt":protected]=>
object(DateTime)#3 (3) {
["date"]=>
string(26) "2017-02-14 18:20:13.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(13) "Europe/Berlin"
}
}
Facebook SDK returned an error: You must provide an access token.
What solutions do i have? Do i miss something?
Thanks.
I'm using the tutorial here http://www.adamboother.com/blog/automatically-posting-to-a-facebook-page-using-the-facebook-sdk-v5-for-php-facebook-api/ to post to a facebook page using the PHP sdk.
I have create the app on Facebook, and I have created the Never Expire Page Access Token, which I have verified using the Access Token Debug Tool https://developers.facebook.com/tools/debug/accesstoken/
However, when I run my test code, it does NOT return any errors, but I get no posts on my page. I get a response back from my print_r($graphNode) of the following
Facebook\GraphNodes\GraphNode Object
(
[items:protected] => Array
(
[id] => 1204777049583245_1264619360265680
)
)
The code for my test post is below, can anyone explain to me WHY I am not getting any posts to show up?
define("FACEBOOK_APP_ID","MY APP ID");
define("FACEBOOK_APP_SECRET","MY APP SECRET");
define("FACEBOOK_APP_TOKEN","MY PAGE ACCESS TOKEN");
define("FACEBOOK_SDK_V4_SRC_DIR",$_SERVER['DOCUMENT_ROOT'].'/socialmedia/Facebook/src/Facebook/');
require_once($_SERVER['DOCUMENT_ROOT'].'/socialmedia/Facebook/src/Facebook/autoload.php');
$fb = new Facebook\Facebook([
'app_id' => FACEBOOK_APP_ID,
'app_secret' => FACEBOOK_APP_SECRET,
'default_graph_version' => 'v2.2',
]);
//Post property to Facebook
$linkData = [
'link' => 'www.yoururl.com',
'message' => 'test message'
];
$pageAccessToken = FACEBOOK_APP_TOKEN;
try {
$response = $fb->post('/me/feed', $linkData, $pageAccessToken);
} 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 '<pre>';
print_r($graphNode);
echo '</pre>';
exit;
been wrestling with the facebook graph api a while now and am still not able to get what I want.
What am I missing? I have this code just as a test:
<?php
// );
$fb = new Facebook\Facebook([
'app_id' => '{313084575421679}',
'app_secret' => '{APP_SECRET}',
'default_graph_version' => 'v2.2',
]);
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->get('/me?fields=id,name', '{APP_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['name'];
?>
it gives me this error Graph returned an error: Invalid OAuth access token signature.
later I want to retrieve al, the photo's links so I can create a collage of the pictures in my album.
How to post new feed on future date using graph API. If I add date in published in sense its throwing error as " with message: (#100) Param published must be a boolean "
if ( $session ) { try {
$request = new FacebookRequest(
$session,
'POST',
'/me/feed',
array (
'message' => 'Hi friends .... ',
'published' => "26/07/2015",
)
);
$response = $request->execute();
$graphObject = $response->getGraphObject();
/* handle the result */
echo "<pre>"; print_r($graphObject); echo "</pre>";
} catch(FacebookRequestException $e) {
// echo "Exception occured, code: " . $e->getCode();
echo " with message: " . $e->getMessage();
}
}
Well, the published field IS a boolean, so it can only be true or false.
Btw, scheduling posts is only possible for Pages, there is no published parameter for /me/feed: https://developers.facebook.com/docs/graph-api/reference/v2.4/user/feed#publish
For a Page, you would set published to true and use a date in the scheduled_publish_time field: https://developers.facebook.com/docs/graph-api/reference/v2.4/page/feed#publish