public function publishOnPage(Post $post, $message = null) { $response = new FacebookPostAsPageResponse(); $accessToken = $this->getUserLongAccessToken(); if (!$post->getPublished()) { return $response->setException(new \Exception('flash_batch_facebook_post_not_published')); } if ($accessToken->tokenIsEmpty()) { return $response->setException(new \Exception('flash_batch_facebook_access_token_empty')); } $this->application->setDefaultAccessToken($accessToken->getAccessToken()); try { $getPageAccessToken = $this->application->sendRequest('GET', '/' . $this->pageId, array('fields' => 'access_token'))->getDecodedBody(); $params = array('message' => null !== $message ? $message : '', 'name' => $post->getTitle(), 'caption' => $post->getDescription(), 'link' => $this->router->generate('front_article_view', array('slug' => $post->getSlug()), true)); if (count($post->getImages()) > 0) { $hompage = $this->router->generate('homepage', array(), true); $imgWebPath = $this->assetsHelper->getUrl($post->getPreviewImage()->getWebPath()); $params['picture'] = $hompage . $imgWebPath; } $endPoint = null === $post->getFbId() ? $this->pageId . '/feed' : $post->getFbId(); $postAsPage = $this->application->post('/' . $endPoint, $params, $getPageAccessToken['access_token'])->getDecodedBody(); $response->setId(isset($postAsPage['id']) ? $postAsPage['id'] : $post->getFbId()); } catch (\Exception $e) { return $response->setException($e); } return $response; }
public function cancelEvent($facebook_event_id) { $user = $this->container->get('security.token_storage')->getToken()->getUser(); $user_access_token = $user->getFacebookAccessToken(); if (!$user_access_token) { return json_encode(['message' => 'Not logged in', 'success' => false]); } $this->fb->setDefaultAccessToken($user_access_token); $attendResponse = null; try { $attendResponse = $this->fb->post($facebook_event_id . '/declined'); } catch (FacebookResponseException $ex) { return false; // @TODO: Log stack trace. } $attendResponse = $attendResponse->getGraphNode()->asArray(); if ($attendResponse['success'] === true) { /** * @var FacebookEvent $facebookEvent */ $facebookEvent = $this->container->get('doctrine')->getRepository('NFQKVKScraperBundle:FacebookEvent')->getByFacebookId($facebook_event_id); $this->container->get('doctrine')->getRepository('NFQKVKScraperBundle:UserEvent')->deleteAttendedEvent($user->getId(), $facebookEvent->getEvent()->getId()); } return $attendResponse; }
public function publish() { $fb = new Facebook(['app_id' => Config::get('facebook.app_id'), 'app_secret' => Config::get('facebook.app_secret'), 'default_graph_version' => Config::get('facebook.default_graph_version'), 'persistent_data_handler' => Config::get('facebook.persistent_data_handler')]); $helper = $fb->getRedirectLoginHelper(); if (Session::has('photo')) { if (Input::has('quePiensas')) { $data = ['message' => Input::get('quePiensas'), 'source' => $fb->fileToUpload(Session::get('photo'))]; try { // Returns a `Facebook\FacebookResponse` object $response = $fb->post('/me/photos', $data, Session::get('fb_access_token')); } catch (Facebook\Exceptions\FacebookResponseException $e) { echo 'Graph returned an error: ' . $e->getMessage(); exit; } catch (Facebook\Exceptions\FacebookSDKException $e) { echo 'Facebook SDK returned an error: ' . $e->getMessage(); exit; } $graphNode = $response->getGraphNode(); return Redirect::to('/')->with('mensaje', 'Publicado con exito!'); } else { return Redirect::to('/')->with('mensaje', 'Sin mensaje no hay amor'); } } else { return Redirect::to('/')->with('mensaje', 'Sin foto no hay amor'); } }
/** * @param string $method * @param string $endpoint * @param array $parameters * @param string $token * * @return \Facebook\FacebookResponse */ private function executeFacebookRequest($method, $endpoint, array $parameters = [], $token = null) { if (is_callable($this->logCallback)) { //used only for debugging: call_user_func($this->logCallback, 'Facebook API request', func_get_args()); } if (!$token) { $token = $this->appToken; } switch ($method) { case 'GET': $response = $this->fb->get($endpoint, $token); break; case 'POST': $response = $this->fb->post($endpoint, $parameters, $token); break; case 'DELETE': $response = $this->fb->delete($endpoint, $parameters, $token); break; default: throw new \Exception("Facebook driver exception, please add support for method: " . $method); break; } if (is_callable($this->logCallback)) { call_user_func($this->logCallback, 'Facebook API response', $response->getDecodedBody()); } return $response; }
/** * @param LiveBroadcast $liveBroadcast * @param OutputFacebook $outputFacebook * @return null|string * @throws LiveBroadcastOutputException */ public function createFacebookLiveVideo(LiveBroadcast $liveBroadcast, OutputFacebook $outputFacebook) { if (!$this->facebookSDK) { $this->initFacebook(); } try { $params = array('title' => $liveBroadcast->getName(), 'description' => $liveBroadcast->getDescription()); $this->facebookSDK->setDefaultAccessToken($outputFacebook->getAccessToken()); $response = $this->facebookSDK->post($outputFacebook->getEntityId() . '/live_videos', $params); } catch (FacebookResponseException $ex) { throw new LiveBroadcastOutputException('Facebook exception: ' . $ex->getMessage()); } catch (FacebookSDKException $ex) { throw new LiveBroadcastOutputException('Facebook SDK exception: ' . $ex->getMessage()); } $body = $response->getDecodedBody(); if (array_key_exists('stream_url', $body)) { return $body['stream_url']; } return null; }
/** * Execute the console command. * * @return mixed */ public function handle() { $link = $this->argument('link'); $message = $this->argument('message'); $users = User::all(); $fb = new Facebook(); foreach ($users as $user) { try { $fb->post("/{$user->facebook_id}/notifications", ['href' => $link, 'template' => $message], env('FACEBOOK_APP_ID') . '|' . env('FACEBOOK_APP_SECRET')); } catch (\Facebook\Exceptions\FacebookResponseException $e) { echo "Caught exception {$e->getMessage()}." . PHP_EOL; } } }
protected static function notify(string $facebook_id) { $fb = new Facebook(); $fb->post("/{$facebook_id}/notifications", ['href' => 'https://hackers-voting.herokuapp.com/', 'template' => 'You have been nominated for the HX admin election. Click here to register yourself as a candidate.'], env('FACEBOOK_APP_ID') . '|' . env('FACEBOOK_APP_SECRET')); }
public function sendMessage() { $session = new Session(); $session->start(); $appId = $this->container->getParameter("gfb_social_client.facebook.app_id"); $appSecret = $this->container->getParameter("gfb_social_client.facebook.app_secret"); $version = $this->container->getParameter("gfb_social_client.facebook.version"); $facebook = new Facebook(array("app_id" => $appId, "app_secret" => $appSecret, "default_graph_version" => $version)); //100003261022760 //100007644586543 $linkData = array("message" => "Test message", "name" => "sdfds jj jjjsdj j j ", "link" => "https://apps.facebook.com/xxxxxxxaxsa", "to" => "100008162627320"); $response = $facebook->post("/send", $linkData, $_SESSION[self::FB_ACCESS_TOKEN_SESSION], null, $version); $graphNode = $response->getGraphEdge(); echo "<pre>"; print_r($graphNode->asArray()); echo "</pre><br/><br/><br/>"; die; // ============================================================================ /*$linkData = [ "link" => "http://www.example.com", "message" => "Test message", ]; try { // Returns a `Facebook\FacebookResponse` object $response = $facebook->post("/me/feed", $linkData, $_SESSION["fb_access_token"]); } catch (FacebookResponseException $e) { echo 'Graph returned an error: ' . $e->getMessage(); exit; } catch (FacebookSDKException $e) { echo 'Facebook SDK returned an error: ' . $e->getMessage(); exit; } $graphNode = $response->getGraphNode(); echo "<pre>"; print_r($graphNode); echo "</pre><br/><br/><br/>"; die;*/ }
<?php session_start(); require_once 'vendor/autoload.php'; $config = (require_once 'vendor/config.php'); use Facebook\Exceptions\FacebookSDKException; use Facebook\Facebook; $api_id = $config['app_id']; $api_secret = $config['app_secret']; $url = "http://localhost:9090/permissions.php"; $fb = new Facebook(["app_id" => $api_id, "app_secret" => $api_secret, "default_graph_version" => "v2.2"]); $linkData = ['link' => 'http://www.localhost.com:9090', 'message' => 'I Like DaniWeb IT Discussion Forum']; $helper = $fb->getRedirectLoginHelper(); try { $token = $helper->getAccessToken(); if (isset($token)) { $response = $fb->post('/me/feed/1567710166830880', $linkData, $config['access_token']); $graphNode = $response->getGraphNode(); echo 'Posted with id: ' . $graphNode['id']; } else { $permision = array("scope" => "email,publish_pages,publish_actions,manage_pages"); echo "<a href='" . $helper->getLoginUrl($url, $permision) . "'>Click to post</a>"; } } catch (FacebookSDKException $e) { echo $e->getMessage(); }
} if ($notifications == 0) { //echo "<td>No notifications found</td>"; } else { // Check if there are notifications to send if ($user->facebookid != null && $notifications != 0) { if ($notifications == 1) { $template = "Tienes {$notifications} notificación de Webcursos."; } else { $template = "Tienes {$notifications} notificaciones de Webcursos."; } $data = array("link" => "", "message" => "", "template" => $template); $fb->setDefaultAccessToken($appid . '|' . $secretid); // Handles when the notifier throws an exception (couldn't send the notification) try { $response = $fb->post('/' . $user->facebookid . '/notifications', $data); $return = $response->getDecodedBody(); echo "Send " . $notifications . " notification to " . $user->name . " - " . $user->email . " | \n"; } catch (Exception $e) { $exception = $e->getMessage(); echo "Exception found: {$exception} \n"; // If the user hasn't installed the app, update it's record to status = 0 if (strpos($exception, "not installed") !== FALSE) { $updatequery = "UPDATE {facebook_user} \n\t\t\t\t\t\t\t\tSET status = ? \n\t\t\t\t\t\t\t\tWHERE moodleid = ?"; $updateparams = array(0, $user->id); if ($DB->execute($updatequery, $updateparams)) { echo "Record updated, set status to 0. \n"; } else { echo "Could not update the record. \n"; } //echo "</td>";