Cheetah
Facebook.php
Go to the documentation of this file.
1 <?php
24 namespace Facebook;
25 
45 
51 class Facebook
52 {
56  const VERSION = '5.6.2';
57 
61  const DEFAULT_GRAPH_VERSION = 'v2.10';
62 
66  const APP_ID_ENV_NAME = 'FACEBOOK_APP_ID';
67 
71  const APP_SECRET_ENV_NAME = 'FACEBOOK_APP_SECRET';
72 
76  protected $app;
77 
81  protected $client;
82 
86  protected $oAuth2Client;
87 
92 
97 
102 
107 
112 
116  protected $lastResponse;
117 
125  public function __construct(array $config = [])
126  {
127  $config = array_merge([
128  'app_id' => getenv(static::APP_ID_ENV_NAME),
129  'app_secret' => getenv(static::APP_SECRET_ENV_NAME),
130  'default_graph_version' => static::DEFAULT_GRAPH_VERSION,
131  'enable_beta_mode' => false,
132  'http_client_handler' => null,
133  'persistent_data_handler' => null,
134  'pseudo_random_string_generator' => null,
135  'url_detection_handler' => null,
136  ], $config);
137 
138  if (!$config['app_id']) {
139  throw new FacebookSDKException('Required "app_id" key not supplied in config and could not find fallback environment variable "' . static::APP_ID_ENV_NAME . '"');
140  }
141  if (!$config['app_secret']) {
142  throw new FacebookSDKException('Required "app_secret" key not supplied in config and could not find fallback environment variable "' . static::APP_SECRET_ENV_NAME . '"');
143  }
144 
145  $this->app = new FacebookApp($config['app_id'], $config['app_secret']);
146  $this->client = new FacebookClient(
147  HttpClientsFactory::createHttpClient($config['http_client_handler']),
148  $config['enable_beta_mode']
149  );
151  $config['pseudo_random_string_generator']
152  );
153  $this->setUrlDetectionHandler($config['url_detection_handler'] ?: new FacebookUrlDetectionHandler());
154  $this->persistentDataHandler = PersistentDataFactory::createPersistentDataHandler(
155  $config['persistent_data_handler']
156  );
157 
158  if (isset($config['default_access_token'])) {
159  $this->setDefaultAccessToken($config['default_access_token']);
160  }
161 
162  // @todo v6: Throw an InvalidArgumentException if "default_graph_version" is not set
163  $this->defaultGraphVersion = $config['default_graph_version'];
164  }
165 
171  public function getApp()
172  {
173  return $this->app;
174  }
175 
181  public function getClient()
182  {
183  return $this->client;
184  }
185 
191  public function getOAuth2Client()
192  {
193  if (!$this->oAuth2Client instanceof OAuth2Client) {
194  $app = $this->getApp();
195  $client = $this->getClient();
196  $this->oAuth2Client = new OAuth2Client($app, $client, $this->defaultGraphVersion);
197  }
198 
199  return $this->oAuth2Client;
200  }
201 
207  public function getLastResponse()
208  {
209  return $this->lastResponse;
210  }
211 
217  public function getUrlDetectionHandler()
218  {
219  return $this->urlDetectionHandler;
220  }
221 
227  private function setUrlDetectionHandler(UrlDetectionInterface $urlDetectionHandler)
228  {
229  $this->urlDetectionHandler = $urlDetectionHandler;
230  }
231 
237  public function getDefaultAccessToken()
238  {
239  return $this->defaultAccessToken;
240  }
241 
249  public function setDefaultAccessToken($accessToken)
250  {
251  if (is_string($accessToken)) {
252  $this->defaultAccessToken = new AccessToken($accessToken);
253 
254  return;
255  }
256 
257  if ($accessToken instanceof AccessToken) {
258  $this->defaultAccessToken = $accessToken;
259 
260  return;
261  }
262 
263  throw new \InvalidArgumentException('The default access token must be of type "string" or Facebook\AccessToken');
264  }
265 
271  public function getDefaultGraphVersion()
272  {
273  return $this->defaultGraphVersion;
274  }
275 
281  public function getRedirectLoginHelper()
282  {
283  return new FacebookRedirectLoginHelper(
284  $this->getOAuth2Client(),
285  $this->persistentDataHandler,
286  $this->urlDetectionHandler,
287  $this->pseudoRandomStringGenerator
288  );
289  }
290 
296  public function getJavaScriptHelper()
297  {
298  return new FacebookJavaScriptHelper($this->app, $this->client, $this->defaultGraphVersion);
299  }
300 
306  public function getCanvasHelper()
307  {
308  return new FacebookCanvasHelper($this->app, $this->client, $this->defaultGraphVersion);
309  }
310 
316  public function getPageTabHelper()
317  {
318  return new FacebookPageTabHelper($this->app, $this->client, $this->defaultGraphVersion);
319  }
320 
333  public function get($endpoint, $accessToken = null, $eTag = null, $graphVersion = null)
334  {
335  return $this->sendRequest(
336  'GET',
337  $endpoint,
338  $params = [],
339  $accessToken,
340  $eTag,
341  $graphVersion
342  );
343  }
344 
358  public function post($endpoint, array $params = [], $accessToken = null, $eTag = null, $graphVersion = null)
359  {
360  return $this->sendRequest(
361  'POST',
362  $endpoint,
363  $params,
364  $accessToken,
365  $eTag,
366  $graphVersion
367  );
368  }
369 
383  public function delete($endpoint, array $params = [], $accessToken = null, $eTag = null, $graphVersion = null)
384  {
385  return $this->sendRequest(
386  'DELETE',
387  $endpoint,
388  $params,
389  $accessToken,
390  $eTag,
391  $graphVersion
392  );
393  }
394 
404  public function next(GraphEdge $graphEdge)
405  {
406  return $this->getPaginationResults($graphEdge, 'next');
407  }
408 
418  public function previous(GraphEdge $graphEdge)
419  {
420  return $this->getPaginationResults($graphEdge, 'previous');
421  }
422 
433  public function getPaginationResults(GraphEdge $graphEdge, $direction)
434  {
435  $paginationRequest = $graphEdge->getPaginationRequest($direction);
436  if (!$paginationRequest) {
437  return null;
438  }
439 
440  $this->lastResponse = $this->client->sendRequest($paginationRequest);
441 
442  // Keep the same GraphNode subclass
443  $subClassName = $graphEdge->getSubClassName();
444  $graphEdge = $this->lastResponse->getGraphEdge($subClassName, false);
445 
446  return count($graphEdge) > 0 ? $graphEdge : null;
447  }
448 
463  public function sendRequest($method, $endpoint, array $params = [], $accessToken = null, $eTag = null, $graphVersion = null)
464  {
465  $accessToken = $accessToken ?: $this->defaultAccessToken;
466  $graphVersion = $graphVersion ?: $this->defaultGraphVersion;
467  $request = $this->request($method, $endpoint, $params, $accessToken, $eTag, $graphVersion);
468 
469  return $this->lastResponse = $this->client->sendRequest($request);
470  }
471 
483  public function sendBatchRequest(array $requests, $accessToken = null, $graphVersion = null)
484  {
485  $accessToken = $accessToken ?: $this->defaultAccessToken;
486  $graphVersion = $graphVersion ?: $this->defaultGraphVersion;
487  $batchRequest = new FacebookBatchRequest(
488  $this->app,
489  $requests,
490  $accessToken,
491  $graphVersion
492  );
493 
494  return $this->lastResponse = $this->client->sendBatchRequest($batchRequest);
495  }
496 
505  public function newBatchRequest($accessToken = null, $graphVersion = null)
506  {
507  $accessToken = $accessToken ?: $this->defaultAccessToken;
508  $graphVersion = $graphVersion ?: $this->defaultGraphVersion;
509 
510  return new FacebookBatchRequest(
511  $this->app,
512  [],
513  $accessToken,
514  $graphVersion
515  );
516  }
517 
532  public function request($method, $endpoint, array $params = [], $accessToken = null, $eTag = null, $graphVersion = null)
533  {
534  $accessToken = $accessToken ?: $this->defaultAccessToken;
535  $graphVersion = $graphVersion ?: $this->defaultGraphVersion;
536 
537  return new FacebookRequest(
538  $this->app,
539  $accessToken,
540  $method,
541  $endpoint,
542  $params,
543  $eTag,
544  $graphVersion
545  );
546  }
547 
557  public function fileToUpload($pathToFile)
558  {
559  return new FacebookFile($pathToFile);
560  }
561 
571  public function videoToUpload($pathToFile)
572  {
573  return new FacebookVideo($pathToFile);
574  }
575 
590  public function uploadVideo($target, $pathToFile, $metadata = [], $accessToken = null, $maxTransferTries = 5, $graphVersion = null)
591  {
592  $accessToken = $accessToken ?: $this->defaultAccessToken;
593  $graphVersion = $graphVersion ?: $this->defaultGraphVersion;
594 
595  $uploader = new FacebookResumableUploader($this->app, $this->client, $accessToken, $graphVersion);
596  $endpoint = '/'.$target.'/videos';
597  $file = $this->videoToUpload($pathToFile);
598  $chunk = $uploader->start($endpoint, $file);
599 
600  do {
601  $chunk = $this->maxTriesTransfer($uploader, $endpoint, $chunk, $maxTransferTries);
602  } while (!$chunk->isLastChunk());
603 
604  return [
605  'video_id' => $chunk->getVideoId(),
606  'success' => $uploader->finish($endpoint, $chunk->getUploadSessionId(), $metadata),
607  ];
608  }
609 
622  private function maxTriesTransfer(FacebookResumableUploader $uploader, $endpoint, FacebookTransferChunk $chunk, $retryCountdown)
623  {
624  $newChunk = $uploader->transfer($endpoint, $chunk, $retryCountdown < 1);
625 
626  if ($newChunk !== $chunk) {
627  return $newChunk;
628  }
629 
630  $retryCountdown--;
631 
632  // If transfer() returned the same chunk entity, the transfer failed but is resumable.
633  return $this->maxTriesTransfer($uploader, $endpoint, $chunk, $retryCountdown);
634  }
635 }
Facebook\Facebook\request
request($method, $endpoint, array $params=[], $accessToken=null, $eTag=null, $graphVersion=null)
Definition: Facebook.php:532
Facebook\Facebook\$urlDetectionHandler
$urlDetectionHandler
Definition: Facebook.php:91
$config
$config
Definition: Filter.ExtractStyleBlocks.txt:33
Facebook\Facebook\getJavaScriptHelper
getJavaScriptHelper()
Definition: Facebook.php:296
Facebook\Facebook\getUrlDetectionHandler
getUrlDetectionHandler()
Definition: Facebook.php:217
Facebook\Url\UrlDetectionInterface
Definition: UrlDetectionInterface.php:32
Facebook\Helpers\FacebookPageTabHelper
Definition: FacebookPageTabHelper.php:35
Facebook\FacebookBatchRequest
Definition: FacebookBatchRequest.php:38
Facebook\Facebook\setDefaultAccessToken
setDefaultAccessToken($accessToken)
Definition: Facebook.php:249
Facebook\Facebook\getDefaultGraphVersion
getDefaultGraphVersion()
Definition: Facebook.php:271
Facebook\PseudoRandomString\PseudoRandomStringGeneratorFactory
Definition: PseudoRandomStringGeneratorFactory.php:30
Facebook\Exceptions\FacebookSDKException
Definition: FacebookSDKException.php:32
Facebook\Facebook\$app
$app
Definition: Facebook.php:76
Facebook\GraphNodes\GraphEdge
Definition: GraphEdge.php:36
Facebook\PseudoRandomString\PseudoRandomStringGeneratorFactory\createPseudoRandomStringGenerator
static createPseudoRandomStringGenerator($generator)
Definition: PseudoRandomStringGeneratorFactory.php:45
use
GNU LESSER GENERAL PUBLIC LICENSE February Free Software Inc Franklin Fifth MA USA Everyone is permitted to copy and distribute verbatim copies of this license but changing it is not allowed[This is the first released version of the Lesser GPL. It also counts as the successor of the GNU Library Public License, version 2, hence the version number 2.1.] Preamble The licenses for most software are designed to take away your freedom to share and change it By the GNU General Public Licenses are intended to guarantee your freedom to share and change free software to make sure the software is free for all its users This the Lesser General Public applies to some specially designated software packages typically libraries of the Free Software Foundation and other authors who decide to use it You can use it but we suggest you first think carefully about whether this license or the ordinary General Public License is the better strategy to use in any particular based on the explanations below When we speak of free we are referring to freedom of use
Definition: license.txt:27
Facebook\Facebook\getApp
getApp()
Definition: Facebook.php:171
Facebook\Facebook\fileToUpload
fileToUpload($pathToFile)
Definition: Facebook.php:557
Facebook\Facebook\previous
previous(GraphEdge $graphEdge)
Definition: Facebook.php:418
php
Facebook\Facebook\getCanvasHelper
getCanvasHelper()
Definition: Facebook.php:306
Facebook\Facebook\$oAuth2Client
$oAuth2Client
Definition: Facebook.php:86
Facebook\Facebook\$pseudoRandomStringGenerator
$pseudoRandomStringGenerator
Definition: Facebook.php:96
Facebook\Facebook\next
next(GraphEdge $graphEdge)
Definition: Facebook.php:404
Facebook\Facebook\sendBatchRequest
sendBatchRequest(array $requests, $accessToken=null, $graphVersion=null)
Definition: Facebook.php:483
Facebook\Facebook\__construct
__construct(array $config=[])
Definition: Facebook.php:125
Facebook\Facebook\getOAuth2Client
getOAuth2Client()
Definition: Facebook.php:191
Facebook\Facebook\getDefaultAccessToken
getDefaultAccessToken()
Definition: Facebook.php:237
Facebook\Authentication\AccessToken
Definition: AccessToken.php:32
Facebook\Url\FacebookUrlDetectionHandler
Definition: FacebookUrlDetectionHandler.php:32
Facebook\Facebook\uploadVideo
uploadVideo($target, $pathToFile, $metadata=[], $accessToken=null, $maxTransferTries=5, $graphVersion=null)
Definition: Facebook.php:590
Facebook\Helpers\FacebookRedirectLoginHelper
Definition: FacebookRedirectLoginHelper.php:43
Facebook\Facebook\$client
$client
Definition: Facebook.php:81
Facebook\GraphNodes\GraphEdge\getSubClassName
getSubClassName()
Definition: GraphEdge.php:91
Facebook\GraphNodes\GraphEdge\getPaginationRequest
getPaginationRequest($direction)
Definition: GraphEdge.php:186
Facebook\PseudoRandomString\PseudoRandomStringGeneratorInterface
Definition: PseudoRandomStringGeneratorInterface.php:32
Facebook\PersistentData\PersistentDataFactory\createPersistentDataHandler
static createPersistentDataHandler($handler)
Definition: PersistentDataFactory.php:44
Facebook\HttpClients\HttpClientsFactory
Definition: HttpClientsFactory.php:31
Facebook\Facebook\getLastResponse
getLastResponse()
Definition: Facebook.php:207
Facebook\FileUpload\FacebookVideo
Definition: FacebookVideo.php:32
Facebook\FacebookApp
Definition: FacebookApp.php:30
Facebook\Authentication\OAuth2Client
Definition: OAuth2Client.php:40
Facebook\Facebook\$lastResponse
$lastResponse
Definition: Facebook.php:116
Facebook\FileUpload\FacebookResumableUploader
Definition: FacebookResumableUploader.php:40
Facebook\PersistentData\PersistentDataInterface
Definition: PersistentDataInterface.php:32
Facebook\FileUpload\FacebookResumableUploader\transfer
transfer($endpoint, FacebookTransferChunk $chunk, $allowToThrow=false)
Definition: FacebookResumableUploader.php:107
Facebook
Facebook\Facebook\getPaginationResults
getPaginationResults(GraphEdge $graphEdge, $direction)
Definition: Facebook.php:433
Facebook\Facebook\$persistentDataHandler
$persistentDataHandler
Definition: Facebook.php:111
Facebook\Facebook\videoToUpload
videoToUpload($pathToFile)
Definition: Facebook.php:571
Facebook\FacebookClient
Definition: FacebookClient.php:37
Facebook\Helpers\FacebookJavaScriptHelper
Definition: FacebookJavaScriptHelper.php:32
Facebook\Facebook\getRedirectLoginHelper
getRedirectLoginHelper()
Definition: Facebook.php:281
Facebook\Facebook\post
post($endpoint, array $params=[], $accessToken=null, $eTag=null, $graphVersion=null)
Definition: Facebook.php:358
Facebook\FileUpload\FacebookTransferChunk
Definition: FacebookTransferChunk.php:32
Facebook\PersistentData\PersistentDataFactory
Definition: PersistentDataFactory.php:29
Facebook\Facebook\getClient
getClient()
Definition: Facebook.php:181
Facebook\Facebook\newBatchRequest
newBatchRequest($accessToken=null, $graphVersion=null)
Definition: Facebook.php:505
Facebook\Facebook\$defaultAccessToken
$defaultAccessToken
Definition: Facebook.php:101
Facebook\HttpClients\HttpClientsFactory\createHttpClient
static createHttpClient($handler)
Definition: HttpClientsFactory.php:47
Facebook\Facebook\$defaultGraphVersion
$defaultGraphVersion
Definition: Facebook.php:106
Facebook\Helpers\FacebookCanvasHelper
Definition: FacebookCanvasHelper.php:32
Facebook\Facebook\sendRequest
sendRequest($method, $endpoint, array $params=[], $accessToken=null, $eTag=null, $graphVersion=null)
Definition: Facebook.php:463
Facebook\FacebookRequest
Definition: FacebookRequest.php:40
Facebook\Facebook\getPageTabHelper
getPageTabHelper()
Definition: Facebook.php:316
Facebook\FileUpload\FacebookFile
Definition: FacebookFile.php:34