Skip to content

Instantly share code, notes, and snippets.

@phwd
Created August 17, 2012 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phwd/3378724 to your computer and use it in GitHub Desktop.
Save phwd/3378724 to your computer and use it in GitHub Desktop.
<?php
/**
* Copyright 2011 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
require '../src/facebook.php';
// Create an Application instance (replace with your appId and secret).
$facebook = new Facebook(array(
'appId' => '191149314281714',
'secret' => '73b67bf1c825fa47efae70a46c18906b',
));
// Get User ID
$user = $facebook->getUser();
// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
if ($user) {
try {
//Process knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(array('scope' => 'publish_stream, read_stream'));
}
// Get permissions authorized by user
if ($user) {
try {
$permissions = $facebook->api('/me/permissions');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Post on a wall, the user must have authorized the applicaiton to publish
// on his/her wall with the publish_stream permission
if(isset($_POST['submit'])) {
if ($user) {
$attachment = array(
'message' => 'this is my message',
'name' => 'This is my demo Facebook application!',
'caption' => "Caption of the Post",
'link' => 'http://www.lycos.com',
'description' => 'Test de post depuis application PHP',
'picture' => 'http://www.lalibre.be/img/logoLaLibre.gif',
'actions' => array(array('name' => 'Get Search','link' => 'http://www.google.com'))
);
try {
// Proceed knowing you have a user who is logged in and authenticated
$result = $facebook->api('/me/feed/','post',$attachment);
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
}
// Fetch the feed to show the new post, needs the read_stream permission
if ($user) {
try {
$status = $facebook->api('me/feed?limit=1');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>PHP-SDK</title>
<style>
body {
font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
}
h1 a {
text-decoration: none;
color: #3b5998;
}
h1 a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<h1>PHP SDK with publish_stream and read_stream permissions</h1>
<?php if ($user): ?>
<a href="<?php echo $logoutUrl; ?>">Logout</a>
<?php else: ?>
<div>
Login using OAuth 2.0 handled by the PHP SDK:
<a href="<?php echo $loginUrl; ?>">Login with Facebook</a>
<div>
<?php endif ?>
<h3>PHP Session</h3>
<pre><?php print_r($_SESSION); ?></pre>
<?php if ($user): ?>
<h3>You</h3>
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
<h3>Your Permissions (/me/permissions)</h3>
<pre><?php print_r($permissions); ?></pre>
<h3>Your User Object (/me)</h3>
<pre><?php print_r($user_profile); ?></pre>
<?php else: ?>
<strong><em>You are not connected.</em></strong>
<?php endif ?>
<?php if($user): ?>
<h3>Most Recent Status</h3>
<pre><?php print_r($status); ?></pre>
<form id="wallpost" name="wallpost" method="post">
<input type="submit" name="submit" id="submit" value="Submit new wall post">
</form>
<?php endif ?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment