<?php
try{
require_once(dirname(__FILE__) . '\facebook-php-sdk\src\Facebook\facebook.php');
}
catch(Exception $o){
print_r($o);
}
$config = array(
'appId' => '123456',
'secret' => '5ca2b11ea4c8sdsdsd',
'allowSignedRequest' => false // optional but should be set to false for non-canvas apps
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$user_id = '123456';
if($user_id) {
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try {
$ret_obj = $facebook->api('/'.$user_id.'/feed', 'POST',
array(
'link' => 'www.example.com',
'message' => 'Posting with the PHP SDK!'
));
// $ret_obj = $facebook->api('/me/feed', 'POST',
// array(
// 'access_token' => $facebook->getAccessToken(),
// 'link' => 'www.example.com',
// 'message' => 'Posting with the PHP SDK!'
// ));
echo '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';
// Give the user a logout link
echo '<br /><a href="' . $facebook->getLogoutUrl() . '">logout</a>';
} catch(FacebookApiException $e) {
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll
// just ask the user to login again here.
echo "<pre>";var_dump($e);
$login_url = $facebook->getLoginUrl(array(
'scope' => 'publish_stream'
));
echo 'Please <a href="' . $login_url . '">login.</a>';
error_log($e->getType());
error_log($e->getMessage());
}
} else {
// No user, so print a link for the user to login
// To post to a user's wall, we need publish_stream permission
// We'll use the current URL as the redirect_uri, so we don't
// need to specify it here.
$login_url = $facebook->getLoginUrl(array('scope' => 'publish_stream'));
echo 'Please <a href="' . $login_url . '">login.</a>';
}?>
來源
2015-07-04 08:05:42
Sid
請編輯您的問題,並添加您嘗試使用的代碼段。這將幫助人們大大回答你的問題!閱讀更多關於[如何提出一個很好的問題](http://stackoverflow.com/help/how-to-ask)。 – methode