Skip to content

Commit

Permalink
Fix Facebook api class
Browse files Browse the repository at this point in the history
  • Loading branch information
alihesari authored Jan 9, 2018
1 parent 63f9d31 commit e16da51
Showing 1 changed file with 69 additions and 4 deletions.
73 changes: 69 additions & 4 deletions src/Facebook/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,73 @@ class Api
*/
private static $app_secret;

/**urce' => self::$fb->fileToUpload($photo)
/**
* API Version
*
* @var string
*/
private static $default_graph_version;

/**
* Page access Token
*
* @var string
*/
private static $page_access_token;

private static $fb;

public static function initialize()
{
self::$app_id = Config::get('larasap.facebook.app_id');
self::$app_secret = Config::get('larasap.facebook.app_secret');
self::$default_graph_version = Config::get('larasap.facebook.default_graph_version');
self::$page_access_token = Config::get('larasap.facebook.page_access_token');

self::$fb = new \Facebook\Facebook([
'app_id' => self::$app_id,
'app_secret' => self::$app_secret,
'default_graph_version' => self::$default_graph_version,
]);
}

/**
* Send link and text message
*
* @param $link
* @param $message
* @return bool
* @throws \Exception
*/
public static function sendLink($link, $message = '')
{
self::initialize();
$data = compact('link', 'message');
try {
$response = self::$fb->post('/me/feed', $data, self::$page_access_token);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
throw new \Exception('Graph returned an error: '.$e->getMessage());
} catch(Facebook\Exceptions\FacebookSDKException $e) {
throw new \Exception('Facebook SDK returned an error: '.$e->getMessage());
}
$graphNode = $response->getGraphNode();

return $graphNode['id'];
}

/**
* Send photo and text message
*
* @param $photo
* @param string $message
* @return bool
* @throws \Exception
*/
public static function sendPhoto($photo ,$message = ''){
self::initialize();
$data = [
'message' => $message,
'source' => self::$fb->fileToUpload($photo)
];
try {
$response = self::$fb->post('/me/photos', $data, self::$page_access_token);
Expand All @@ -44,8 +110,7 @@ class Api
* @return mixed
* @throws \Exception
*/
public static function sendVideo($video , $title = '', $description = '')
{
public static function sendVideo($video , $title = '', $description = ''){
self::initialize();
$data = compact('title','description');
try {
Expand All @@ -59,4 +124,4 @@ public static function sendVideo($video , $title = '', $description = '')

return $graphNode['id'];
}
}
}

0 comments on commit e16da51

Please sign in to comment.