Advertisement
rAthus

Facebook PHP SDK : _facebook_functions.php

Oct 1st, 2015
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.84 KB | None | 0 0
  1. session_start();
  2.  
  3. date_default_timezone_set('Europe/Paris');
  4.  
  5. define('FACEBOOK_SDK_V4_SRC_DIR', '/fb-php-sdk-v4/src/Facebook/');
  6. require __DIR__ . '/facebook-php-sdk-v4/autoload.php';
  7.  
  8. require_once( __DIR__ . '/facebook-php-sdk-v4/src/Facebook/FacebookSession.php' );
  9. require_once( __DIR__ . '/facebook-php-sdk-v4/src/Facebook/FacebookRedirectLoginHelper.php' );
  10. require_once( __DIR__ . '/facebook-php-sdk-v4/src/Facebook/FacebookRequest.php' );
  11. require_once( __DIR__ . '/facebook-php-sdk-v4/src/Facebook/FacebookResponse.php' );
  12. require_once( __DIR__ . '/facebook-php-sdk-v4/src/Facebook/FacebookSDKException.php' );
  13. require_once( __DIR__ . '/facebook-php-sdk-v4/src/Facebook/FacebookRequestException.php' );
  14. require_once( __DIR__ . '/facebook-php-sdk-v4/src/Facebook/FacebookAuthorizationException.php' );
  15. require_once( __DIR__ . '/facebook-php-sdk-v4/src/Facebook/GraphObject.php' );
  16. require_once( __DIR__ . '/facebook-php-sdk-v4/src/Facebook/Entities/AccessToken.php' );
  17. require_once( __DIR__ . '/facebook-php-sdk-v4/src/Facebook/HttpClients/FacebookHttpable.php' );
  18. require_once( __DIR__ . '/facebook-php-sdk-v4/src/Facebook/HttpClients/FacebookCurlHttpClient.php' );
  19. require_once( __DIR__ . '/facebook-php-sdk-v4/src/Facebook/HttpClients/FacebookCurl.php' );
  20.  
  21. use Facebook\FacebookSession;
  22. use Facebook\FacebookRedirectLoginHelper;
  23. use Facebook\FacebookRequest;
  24. use Facebook\FacebookResponse;
  25. use Facebook\FacebookSDKException;
  26. use Facebook\FacebookRequestException;
  27. use Facebook\FacebookAuthorizationException;
  28. use Facebook\GraphObject;
  29. use Facebook\Entities\AccessToken;
  30. use Facebook\HttpClients\FacebookHttpable;
  31. use Facebook\HttpClients\FacebookCurlHttpClient;
  32. use Facebook\HttpClients\FacebookCurl;
  33.  
  34. FacebookSession::setDefaultApplication('XXXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'); // app_id et app_secret de l'application Facebook
  35.  
  36. // facebook_publish()
  37. // $page_id = id de la page facebook au nom de laquelle on veut publier
  38. // $access_token = access token valide pour la page
  39. // $publish_timestamp = timestamp de l'heure programmée de publication, entre 10 minutes et 6 mois (facultatif)
  40. // $message = message posté (facultatif si on partage un lien)
  41. // $link = lien partagé (facultatif si on a mis un message)
  42. // $link_name = titre du lien (possible si lien partagé)
  43. // $link_description = description du lien (possible si lien partagé)
  44. function facebook_publish($page_id,$access_token,$publish_timestamp=false,$message='',$link='',$link_name='',$link_description='')
  45. {
  46.     if (!$page_id or !$access_token or (!$message and !$link))
  47.         return array('error'=>'You must provide at lease a page_id, an access_token and a message or a link.');
  48.     $session = new FacebookSession($access_token);
  49.     $request = new FacebookRequest
  50.     (
  51.         $session,
  52.         'POST',
  53.         '/'.$page_id.'/feed',
  54.         array
  55.         (
  56.             'published' => ($publish_timestamp)?false:true,
  57.             'scheduled_publish_time' => ($publish_timestamp)?$publish_timestamp:false,
  58.             'message' => $message,
  59.             'link' => $link,
  60.             'name' => $link_name,
  61.             'description' => $link_description
  62.         )
  63.     );
  64.     try
  65.     {
  66.         $response = $request->execute();
  67.         $graphObject = $response->getRawResponse();
  68.         return $graphObject; // si la requête ok, retourne {"id":"XXXXXXXXXXX_XXXXXXXXXXXXXXXXX"}
  69.     }
  70.     catch(Exception $e)
  71.     {
  72.         return $e;
  73.     }
  74. }
  75.  
  76. // facebook_delete()
  77. // $post_id = id du post facebook à supprimer sous la forme XXXXXXXXXXX_XXXXXXXXXXXXXXXXX
  78. // $access_token = assess token valide pour la page
  79. function facebook_delete($post_id,$access_token)
  80. {
  81.     $session = new FacebookSession($access_token);
  82.     $request = new FacebookRequest
  83.     (
  84.         $session,
  85.         'DELETE',
  86.         '/'.$post_id
  87.     );
  88.     try
  89.     {
  90.         $response = $request->execute();
  91.         $graphObject = $response->getRawResponse();
  92.         return $graphObject; // si la requête ok, retourne {"success":true}
  93.     }
  94.     catch(Exception $e)
  95.     {
  96.         return $e;
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement