html ALL-IN-ONE - Facebook PHP SDK 4.0.x

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了html ALL-IN-ONE - Facebook PHP SDK 4.0.x相关的知识,希望对你有一定的参考价值。

<?php
 
// requires Facebook PHP SDK 4.0.x or later
 
// user must be logged-in prior to API call
 
// publish story, requires 'places' attribute
// use a page_id with no address to tag hidden location
// $tags is a comma-separated string of IDs
$story = (new FacebookRequest( $session, 'POST', '/me/feed', array(
              'message' => 'test', 'tags' => $tags, 'place' => '195383960551614'
            ) ))->execute()->getGraphObject()->asArray();
 
// returns post_id
echo '<pre>' . print_r( $story, 1 ) . '</pre>';

?>
<?php
 
// post to facebook - requires publish_actions permission
$post = $facebook->api( "me/feed", "POST", array(
    'message' => 'Testing custom search action',
    'actions' => array(
        'name' => 'Search',
        'link' => 'http://www.google.com'
      )
  ) );
 
// success will return id of post
print_r( $post );

?>
<?php 
 
include "facebook/facebook.php";
 
$facebook = new Facebook( array( 'appId' => APP_ID, 'secret' => APP_SECRET ) );
 
// enable upload support
$facebook->setFileUploadSupport( true );
 
// set access token for user / page here (not needed if you intend to use the login flow beforehand)
$facebook->setAccessToken( ACCESS_TOKEN );
 
// object you want to comment on (can be status update, photo, link etc)
$object_id = '60506094_10100351413616696';
 
$comment_with_url = $facebook->api( $object_id . "/comments", "POST", array(
    'attachment_url' => 'http://healthhub.co/wp-content/uploads/2014/02/Group-Slider.jpg',
    'message' => "Join the team!"
  ) );
 
// will return id of comment on success
print_r( $comment_with_url );

?>
<?php 
 
include "facebook/facebook.php";
 
$facebook = new Facebook( array( 'appId' => APP_ID, 'secret' => APP_SECRET ) );
 
// enable upload support
$facebook->setFileUploadSupport( true );
 
// set access token for user / page here (not needed if you intend to use the login flow beforehand)
$facebook->setAccessToken( ACCESS_TOKEN );
 
// object you want to comment on (can be status update, photo, link etc)
$object_id = '60506094_10100351413616696';
 
$comment_with_url = $facebook->api( $object_id . "/comments", "POST", array(
    'attachment_url' => 'http://healthhub.co/wp-content/uploads/2014/02/Group-Slider.jpg',
    'message' => "Join the team!"
  ) );
  
print_r( $comment_with_photo );
 
$photo_upload = $facebook->api( $object_id . "/comments", "POST", array(
    // no message!
    'source' => '@cover.png'
  ) );
  
print_r( $photo_upload );

?>
<?php
// include lines 1-65 from https://gist.github.com/niraj-shah/fcd17411def017e3aefc here
 
// see if the viewer has liked the page
if ( $pageHelper->isLiked() ) {
 
  // see if we have a session
  if ( isset( $session ) ) {
    
    // show logged-in user id
    echo 'User Id: ' . $pageHelper->getUserId();
    
    // graph api request for user data
    $request = new FacebookRequest( $session, 'GET', '/me' );
    $response = $request->execute();
    // get response
    $graphObject = $response->getGraphObject()->asArray();
    
    // print profile data
    echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';
    
    // print logout url, target = _top to break out of page frame
    echo '<a href="' . $helper->getLogoutUrl( $session, 'http://sites.local/php-sdk-4.0/redirect.php' ) . '" target="_top">Logout</a>';
    
  } else {
    // show login url, target = _top to break out of page frame
    echo '<a href="' . $helper->getLoginUrl( array( 'email', 'user_friends' ) ) . '" target="_top">Login</a>';
  }
 
} else {
  // show likegate
  echo '<h1>Please Like the page to continue</h1>';
}

?>
<?php
// required Facebook PHP SDK v4.0.9 or later.
 
// include required files form Facebook SDK
require_once( 'Facebook/HttpClients/FacebookHttpable.php' );
require_once( 'Facebook/HttpClients/FacebookCurl.php' );
require_once( 'Facebook/HttpClients/FacebookCurlHttpClient.php' );
 
require_once( 'Facebook/Entities/AccessToken.php' );
require_once( 'Facebook/Entities/SignedRequest.php' );
 
require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'Facebook/FacebookSignedRequestFromInputHelper.php' ); // added in v4.0.9
require_once( 'Facebook/FacebookRequest.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/FacebookOtherException.php' );
require_once( 'Facebook/FacebookAuthorizationException.php' );
 
// these two classes required for canvas and tab apps
require_once( 'Facebook/FacebookCanvasLoginHelper.php' );
require_once( 'Facebook/FacebookPageTabHelper.php' );
 
require_once( 'Facebook/GraphObject.php' );
require_once( 'Facebook/GraphSessionInfo.php' );
 
use Facebook\HttpClients\FacebookHttpable;
use Facebook\HttpClients\FacebookCurl;
use Facebook\HttpClients\FacebookCurlHttpClient;
 
use Facebook\Entities\AccessToken;
use Facebook\Entities\SignedRequest;
 
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookSignedRequestFromInputHelper; // added in v4.0.9
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookOtherException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphSessionInfo;
 
// these two classes required for canvas and tab apps
use Facebook\FacebookCanvasLoginHelper;
use Facebook\FacebookPageTabHelper;
 
// start session
session_start();
 
// init app with app id and secret
FacebookSession::setDefaultApplication( 'xxx','yyy' );
 
// init login helper
$helper = new FacebookRedirectLoginHelper( 'http://sites.local/php-sdk-4.0/redirect.php' );
 
// init page tab helper
$pageHelper = new FacebookPageTabHelper();
 
// get session from the page
$session = $pageHelper->getSession();
 
// get page_id
echo '<p>You are currently viewing page: '. $pageHelper->getPageId() . '</p>';
 
// get like status - use for likegates
echo '<p>You have '. ( $pageHelper->isLiked() ? 'LIKED' : 'NOT liked' ) . ' this page</p>';
 
// get admin status
echo '<p>You are '. ( $pageHelper->isAdmin() ? 'an ADMIN' : 'NOT an ADMIN' ) . '</p>';
 
// see if we have a session
if ( isset( $session ) ) {
  
  // show logged-in user id
  echo 'User Id: ' . $pageHelper->getUserId();
  
  // graph api request for user data
  $request = new FacebookRequest( $session, 'GET', '/me' );
  $response = $request->execute();
  // get response
  $graphObject = $response->getGraphObject()->asArray();
  
  // print profile data
  echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';
  
  // print logout url, target = _top to break out of page frame
  echo '<a href="' . $helper->getLogoutUrl( $session, 'http://sites.local/php-sdk-4.0/redirect.php' ) . '" target="_top">Logout</a>';
  
} else {
  // show login url, target = _top to break out of page frame
  echo '<a href="' . $helper->getLoginUrl( array( 'email', 'user_friends' ) ) . '" target="_top">Login</a>';
}

?>
.
└── app
    ├── Facebook
    │   ├── FacebookCanvasLoginHelper.php
    │   ├── FacebookCurl.php
    │   ├── FacebookHttpable.php
    │   ├── FacebookCurlHttpClient.php
    │   ├── FacebookJavaScriptLoginHelper.php
    │   ├── FacebookPageTabHelper.php
    │   ├── FacebookRedirectLoginHelper.php
    │   ├── FacebookRequest.php
    │   ├── FacebookResponse.php
    │   ├── FacebookSession.php
    │   ├── GraphObject.php
    │   ├── GraphAlbum.php
    │   ├── GraphLocation.php
    │   ├── GraphSessionInfo.php
    │   ├── GraphUser.php
    │   ├── FacebookSDKException.php
    │   ├── FacebookRequestException.php
    │   ├── FacebookClientException.php
    │   ├── FacebookAuthorizationException.php
    │   ├── FacebookPermissionException.php
    │   ├── FacebookServerException.php
    │   ├── FacebookThrottleException.php
    │   ├── FacebookOtherException.php
    ├── assets
    │   ├── css
    │   ├── js
    │   ├── img
    └── index.php
<?php
// include code from https://gist.github.com/niraj-shah/ab1c74ad83df172e6075
 
// generate login url with scope, each permission as element in array
$loginUrl = $helper->getLoginUrl( array( 'email', 'user_friends' ) );
 
// output login link
echo '<a href="' . $loginUrl . '">Login</a>';

?>
<?php
 
// include required files form Facebook SDK
 
// added in v4.0.5
require_once( 'Facebook/FacebookHttpable.php' );
require_once( 'Facebook/FacebookCurl.php' );
require_once( 'Facebook/FacebookCurlHttpClient.php' );
 
// added in v4.0.0
require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'Facebook/FacebookRequest.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/FacebookOtherException.php' );
require_once( 'Facebook/FacebookAuthorizationException.php' );
require_once( 'Facebook/GraphObject.php' );
require_once( 'Facebook/GraphSessionInfo.php' );
 
// added in v4.0.5
use Facebook\FacebookHttpable;
use Facebook\FacebookCurl;
use Facebook\FacebookCurlHttpClient;
 
// added in v4.0.0
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookOtherException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphSessionInfo;
 
// start session
session_start();
 
// init app with app id and secret
FacebookSession::setDefaultApplication( 'xxx','yyy' );
 
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( 'http://yourwebsite.com/app/' );
 
// see if a existing session exists
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
  // create new session from saved access_token
  $session = new FacebookSession( $_SESSION['fb_token'] );
  
  // validate the access_token to make sure it's still valid
  try {
    if ( !$session->validate() ) {
      $session = null;
    }
  } catch ( Exception $e ) {
    // catch any exceptions
    $session = null;
  }
}  
 
if ( !isset( $session ) || $session === null ) {
  // no session exists
  
  try {
    $session = $helper->getSessionFromRedirect();
  } catch( FacebookRequestException $ex ) {
    // When Facebook returns an error
    // handle this better in production code
    print_r( $ex );
  } catch( Exception $ex ) {
    // When validation fails or other local issues
    // handle this better in production code
    print_r( $ex );
  }
  
}
 
// see if we have a session
if ( isset( $session ) ) {
  
  // save the session
  $_SESSION['fb_token'] = $session->getToken();
  // create a session using saved token or the new one we generated at login
  $session = new FacebookSession( $session->getToken() );
  
  // graph api request for user data
  $request = new FacebookRequest( $session, 'GET', '/me' );
  $response = $request->execute();
  // get response
  $graphObject = $response->getGraphObject()->asArray();
  
  // print profile data
  echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';
  
  // print logout url using session and redirect_uri (logout.php page should destroy the session)
  echo '<a href="' . $helper->getLogoutUrl( $session, 'http://yourwebsite.com/app/logout.php' ) . '">Logout</a>';
  
} else {
  // show login url
  echo '<a href="' . $helper->getLoginUrl( array( 'email', 'user_friends' ) ) . '">Login</a>';
}


// Why do you create another $session object just to replace the previous one with the exact same fb_token?

// Also, may just be personal preference, but shouldn't you getGraphObject typed as a GraphUser object, considering you are accessing /me? Then you have an object, not an array. For example:

// $me = (new FacebookRequest(
// $session, 'GET', '/me'
// ))->execute()->getGraphObject(GraphUser::className());

// And you really should use Composer, having to manually require 10+ classes is insane. :)



?>
<?php
 
// turn this:
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
$graphObject = $response->getGraphObject();
 
// into this:
$graphObject = (new FacebookRequest( $session, 'GET', '/me' ))->execute()->getGraphObject()->asArray();

?>
<?php
 
// make api call
$response = (new FacebookRequest(
    $session, 'POST', '/me/feed', array(
      'name' => 'Facebook API: Posting a Status Update Using PHP SDK 4.0.x',
      'caption' => "I'm rewriting old tutorials to work with the new PHP SDK 4.0 and Graph API 2.0.",
      'link' => 'https://www.webniraj.com/2014/05/29/facebook-api-p…-php-sdk-4-0-x/',
      'message' => 'Check out my new tutorial'
    )
  ))->execute()->getGraphObject()->asArray();
 
// output response - should include post_id
print_r( $response );

?>
<?php
 
// make api call
$response = (new FacebookRequest(
    $session, 'POST', '/me/feed', array(
      'message' => 'testing'
    )
  ))->execute()->getGraphObject()->asArray();
 
// output response - should include post_id
print_r( $response );

?>
<div class="well">
  <button class="btn btn-info share-btn">Share</button>
</div>
  
<script type="text/javascript">
function fb_share() {
  FB.ui( {
    method: 'feed',
    name: "Search Google",
    link: "https://www.google.com",
    picture: "https://www.google.co.uk/images/srpr/logo11w.png",
    caption: "The world's most popular search engine",
    actions: {"name":"Search", "link":"http://www.google.com"}
  }, function( response ) {
    // do nothing
  } );
 
}
 
$(document).ready(function(){
  $('button.share-btn').on( 'click', fb_share );
});
</script>

以上是关于html ALL-IN-ONE - Facebook PHP SDK 4.0.x的主要内容,如果未能解决你的问题,请参考以下文章

OpenStack 初探 -- All-In-One模式部署(初学OpenStack必备)

Ubuntu系统上All-in-one部署OpenStack

The All-in-One Note

虚拟机上以 All-in-One 模式安装 KubeSphere

OpenStack Queens All-in-one Single-node Deployment

通讯聚合类Chrome 扩展:All-in-One Messenger,把主流聊天服务聚合在一个应用窗口内