/** * Auto post to facebook from wordpress/website * Author -> Nasir Uddin Nobin */ function fb_update($ID, $post) { /** * download facebook php sdk https://github.com/facebookarchive/facebook-php-sdk * upload it to your directory and include it * if you paste this code to plugin file use (plugin_dir_path(__FILE__) */ require_once get_template_directory_uri() . '/facebook-php-sdk/autoload.php'; // cleaning html tags and bbcodes $content = strip_tags($post->post_content); $content = preg_replace("/\\[.+\\](.+)|\\s\\s+\\[\\/.+\\]/i", "\$1", $content); // getting $permalink from id $permalink = get_permalink($ID); $fb = new Facebook\Facebook(['app_id' => '{your-ap-id}', 'app_secret' => '{app-secret}', 'default_graph_version' => 'v2.2']); /** * generate your page long live access token * you may use https://developers.facebook.com/tools/explorer * Permissions: manage_pages, publish_pages */ $accessToken = '{give-your-long-live-page-access-token}'; $linkData = ['link' => $permalink, 'message' => short_content($content, $num = 35) . "\n \t\n \tRead more: {$permalink}\n \t\n \t"]; try { $response = $fb->post('/me/feed', $linkData, $accessToken); } catch (Facebook\Exceptions\FacebookResponseException $e) { } catch (Facebook\Exceptions\FacebookSDKException $e) { } }
public function postStatus($status) { $fb = new Facebook\Facebook(['app_id' => $this->_appID, 'app_secret' => $this->_appSecret, 'default_graph_version' => $this->_defaultGraphVersion]); try { // Post to https://www.facebook.com/bcs.kpasapp $response = $fb->post('/me/feed', $status, $this->_accessToken); // Post to https://www.facebook.com/master.kpasapp/ $response2 = $fb->post('/633059556824338/feed', $status, $this->_pageAccessToken); // Post to https://www.facebook.com/groups/eventsbcs/ $response3 = $fb->post('/723256584381647/feed', $status, $this->_accessToken); // Post to https://www.facebook.com/groups/1415807262056337/ $response4 = $fb->post('/1415807262056337/feed', $status, $this->_accessToken); } catch (Facebook\Exceptions\FacebookResponseException $e) { echo 'Graph returned an error: ' . $e->getMessage(); } catch (Facebook\Exceptions\FacebookSDKException $e) { echo 'Facebook SDK returned an error: ' . $e->getMessage(); } }
public static function facebook($url, $content) { $content = html_entity_decode($content); require_once addons . 'lib/SocialNetwork/facebook-php-sdk/src/Facebook/autoload.php'; $fb = new \Facebook\Facebook(['app_id' => '__app_id', 'app_secret' => '__app_secret', 'default_graph_version' => 'v2.4']); $linkData = ['message' => $content, 'link' => $url]; try { $response = $fb->post('/url/feed', $linkData, '__token_'); } catch (\Facebook\Exceptions\FacebookResponseException $e) { return 'Graph returned an error: ' . $e->getMessage(); } catch (\Facebook\Exceptions\FacebookSDKException $e) { return 'Facebook SDK returned an error: ' . $e->getMessage(); } $graphNode = $response->getGraphNode(); return $graphNode['id']; }
public static function post($event_id) { if (isset($event_id)) { try { $event = Event::findOrFail($event_id); $fb = new \Facebook\Facebook(['app_id' => getenv('FACEBOOK_APP_ID'), 'app_secret' => getenv('FACEBOOK_APP_SECRET'), 'default_graph_version' => 'v2.4', 'default_access_token' => getenv('FACEBOOK_PAGE_TOKEN')]); $fbPageId = getenv('FACEBOOK_PAGE_ID'); $sourceLink = "https://relive.space/event/" . $event->event_id; $fbPostMessage = "The event \"" . $event->eventName . "\" was just added to relive.space!\n\n" . $sourceLink; $response = $fb->post('/' . $fbPageId . '/feed', ['message' => $fbPostMessage]); echo json_encode($response); } catch (ModelNotFoundException $e) { echo "event " . $event_id . " not found!"; } } }
public function postFacebook($array = array()) { // pega os dados do facebook $optionApp = get_option('twfc_app'); $fb = new Facebook\Facebook(['app_id' => $optionApp['facebookappid'], 'app_secret' => $optionApp['facebookappsecret'], 'default_graph_version' => 'v2.2']); // pega o token salvo $access_token = get_option('twfcfacebook_token'); $linkData = array('name' => $array['titulo'], 'caption' => $array['url'], 'link' => $array['url'], 'picture' => $array['imagem'], 'description' => $array['conteudo']); try { // Returns a `Facebook\FacebookResponse` object $response = $fb->post('/me/feed', $linkData, $access_token['fb_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 $graphNode; }
public function data() { $this->_mandatory(array('message', 'user_id', 'token')); $user_id = $this->input->post('user_id'); $token = $this->input->post('token'); $message = $this->input->post('message'); $config = new Controllers_Api_Facebook_Config_App(); $fb = new Facebook\Facebook(['app_id' => $config->config['app_id'], 'app_secret' => $config->config['app_secret'], 'default_graph_version' => $config->config['default_graph_version']]); $linkData = ['message' => $message]; $result = array(); try { // Returns a `Facebook\FacebookResponse` object $response = $fb->post('/' . $user_id . '/feed', $linkData, $token); $graphNode = $response->getGraphNode(); $result = array('status' => true, 'data' => $graphNode['id']); } catch (Facebook\Exceptions\FacebookResponseException $e) { $result = array('status' => false, 'data' => $e->getMessage()); } catch (Facebook\Exceptions\FacebookSDKException $e) { $result = array('status' => false, 'data' => $e->getMessage()); } echo json_encode($result); }
function process_request($request) { require_once QA_INCLUDE_DIR . 'qa-app-users.php'; $appid = qa_opt('fb_app_id'); $secret = qa_opt('fb_app_secret'); $fb = new Facebook\Facebook(['app_id' => $appid, 'app_secret' => $secret, 'default_graph_version' => 'v2.4']); $qa_content = qa_content_prepare(); $qa_content['title'] = 'Facebook Sharing Page'; $helper = $fb->getRedirectLoginHelper(); try { $accessToken = $helper->getAccessToken(); } catch (Facebook\Exceptions\FacebookSDKException $e) { echo $e->getMessage(); exit; } if (isset($accessToken)) { $_SESSION['fb_access_token'] = (string) $accessToken; $res = $fb->post('/me/feed', array('link' => 'http://nathorr.com/qeta/user/' . qa_get_logged_in_handle() . '/', 'name' => qa_opt('fb_shared_message_title'), 'picture' => qa_opt('fb_shared_message_picture'), 'description' => qa_opt('fb_shared_message_description'), 'message' => 'I have scored ' . qa_get_logged_in_points() . ' points and achieved some nice badges in Nathorr Q&A, check it out!'), $accessToken); $post = $res->getGraphObject(); $qa_content['custom'] = '<a href="http://nathorr.com/qeta/user/' . qa_get_logged_in_handle() . '">Successfully shared, return by clicking here.</a>'; return $qa_content; } else { if ($helper->getError()) { var_dump($helper->getError()); echo '<br><br>'; var_dump($helper->getErrorCode()); echo '<br><br>'; var_dump($helper->getErrorReason()); echo '<br><br>'; var_dump($helper->getErrorDescription()); echo '<br><br>'; echo '<a href="http://nathorr.com/qeta/user/' . qa_get_logged_in_handle() . '/">Something went wrong, return by clicking here.</a>'; exit; } } http_response_code(400); exit; }
$_SESSION['facebook_access_token'] = (string) $accessToken; // OAuth 2.0 client handler $oAuth2Client = $fb->getOAuth2Client(); // Exchanges a short-lived access token for a long-lived one $longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']); $_SESSION['facebook_access_token'] = (string) $longLivedAccessToken; // setting default access token to be used in script $fb->setDefaultAccessToken($_SESSION['facebook_access_token']); } // redirect the user back to the same page if it has "code" GET variable if (isset($_GET['code'])) { header('Location: ./'); } // getting basic info about user try { $post = $fb->post('/object-id/comments', array('message' => 'this message should come from user-end')); $post = $post->getGraphNode()->asArray(); } catch (Facebook\Exceptions\FacebookResponseException $e) { // When Graph returns an error echo 'Graph returned an error: ' . $e->getMessage(); session_destroy(); exit; } catch (Facebook\Exceptions\FacebookSDKException $e) { // When validation fails or other local issues echo 'Facebook SDK returned an error: ' . $e->getMessage(); exit; } print_r($post); // Now you can redirect to another page and use the access token from $_SESSION['facebook_access_token'] } else { // replace your website URL same as added in the developers.facebook.com/apps e.g. if you used http instead of https and you used non-www version or www version of your website then you must add the same here
try { $profile_request = $fb->get('/me'); $profile = $profile_request->getGraphNode()->asArray(); } catch (Facebook\Exceptions\FacebookResponseException $e) { // When Graph returns an error echo 'Graph returned an error: ' . $e->getMessage(); session_destroy(); // redirecting user back to app login page header("Location: ./"); exit; } catch (Facebook\Exceptions\FacebookSDKException $e) { // When validation fails or other local issues echo 'Facebook SDK returned an error: ' . $e->getMessage(); exit; } // post on behalf of page $pages = $fb->get('/me/accounts'); $pages = $pages->getGraphEdge()->asArray(); foreach ($pages as $key) { if ($key['name'] == 'Funny Demons') { $post = $fb->post('/' . $key['id'] . '/feed', array('message' => 'just for testing...'), $key['access_token']); $post = $post->getGraphNode()->asArray(); print_r($post); } } // Now you can redirect to another page and use the access token from $_SESSION['facebook_access_token'] } else { // replace your website URL same as added in the developers.facebook.com/apps e.g. if you used http instead of https and you used non-www version or www version of your website then you must add the same here $loginUrl = $helper->getLoginUrl('http://sohaibilyas.com/APP_DIR/', $permissions); echo '<a href="' . $loginUrl . '">Log in with Facebook!</a>'; }
require __DIR__.'/Facebook/GraphNodes/GraphObjectFactory.php'; require __DIR__.'/Facebook/GraphNodes/Collection.php'; require __DIR__.'/Facebook/GraphNodes/GraphObject.php'; require __DIR__.'/Facebook/GraphNodes/GraphList.php'; require __DIR__.'/Facebook/Exceptions/FacebookSDKException.php'; require __DIR__.'/Facebook/Exceptions/FacebookResponseException.php'; require __DIR__.'/Facebook/Exceptions/FacebookAuthorizationException.php'; require __DIR__.'/Facebook/Exceptions/FacebookAuthenticationException.php'; $fbConfig = array(); $fbConfig['appId'] = '768074056624586'; $fbConfig['appSecret'] = '214351f71348c3fb0f092d05fffdc8bb'; $fbConfig['access_token'] = 'CAAK6jy1OacoBAEiU02CYZCw13uTLTwF7zfQ38fvNFfTLsFV1gzZC2roXuYRnH6wJTBZCyEOQz7kWvY8tgCehtukTFHH8fis1ZCEw3MtOC6OvgOd0P6o3MrI6IZC9ZC5ZB2T3eLeYKo3d7eY5HIXqFcpawpYEhbpb7piIDYJCcWCBUxQyXA0KVlRZCdDS6sZCPx8AZD'; $fb = new \Facebook\Facebook(array( 'app_id' => $fbConfig['appId'], 'app_secret' => $fbConfig['appSecret'], 'default_graph_version' => 'v2.3' )); // define your POST parameters (replace with your own values) $params = array( "message" => "Here is a blog post about auto posting on Facebook using PHP #php #facebook", "link" => "http://www.pontikis.net/blog/auto_post_on_facebook_with_php", "picture" => "http://i.imgur.com/lHkOsiH.png", "name" => "How to Auto Post on Facebook with PHP", "caption" => "www.pontikis.net", "description" => "Automatically post on Facebook with PHP using Facebook PHP SDK. How to create a Facebook app. Obtain and extend Facebook access tokens. Cron automation." ); $ret = $fb->post('/620666404637761/feed', $params,$fbConfig['access_token']);
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']); } else { // getting short-lived access token $_SESSION['facebook_access_token'] = (string) $accessToken; // OAuth 2.0 client handler $oAuth2Client = $fb->getOAuth2Client(); // Exchanges a short-lived access token for a long-lived one $longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']); $_SESSION['facebook_access_token'] = (string) $longLivedAccessToken; // setting default access token to be used in script $fb->setDefaultAccessToken($_SESSION['facebook_access_token']); } $profile_request = $fb->get('/me?fields=name,first_name,last_name,email'); $profile = $profile_request->getGraphNode()->asArray(); $linkData = ['message' => 'User provided message']; $response = $fb->post("/me/feed", $linkData, $accessToken); $graphNode = $response->getGraphNode(); echo 'Posted with id: ' . $graphNode['id']; } else { $loginUrl = $helper->getLoginUrl('http://localhost/Ecommerce/trunk/public_html/fblogin-v5/post_user.php', $permissions); echo '<a href="' . $loginUrl . '">Log in with Facebook!</a>'; } // parametros posibles para $linkData /* $post = array( "access_token"=>$session["access_token"], "description"=>"Te has suscrito a la aplicación del curso de Facebook que ha creado Polin para el 2011", "link"=>$appUrl . "?redirect=" . base64_encode("http://p0l.in"), "caption"=>"Aplicación de pruebas por Polin", "picture"=>"http://i265.photobucket.com/albums/ii229/polinidoocom/logo/v1_1/polin_logo_260x260.gif", "name"=>"Aplicación de pruebas Polin" ); */
$helper = $fb->getRedirectLoginHelper(); $loginUrl = $helper->getLoginUrl('https://apps.facebook.com/newfbappi/', $permissions); echo "<script>window.top.location.href='" . $loginUrl . "'</script>"; exit; } } catch (Facebook\Exceptions\FacebookSDKException $e) { // When validation fails or other local issues echo 'Facebook SDK returned an error: ' . $e->getMessage(); exit; } // posting on user timeline using publish_actins permission try { // message must come from the user-end $data = ['link' => 'http://ru.pollee.org']; // $data = ['message' => 'testing...']; $request = $fb->post('/me/feed', $data); $response = $request->getGraphUser(); } catch (Facebook\Exceptions\FacebookResponseException $e) { // When Graph returns an error echo 'Graph returned an error: ' . $e->getMessage(); exit; } catch (Facebook\Exceptions\FacebookSDKException $e) { // When validation fails or other local issues echo 'Facebook SDK returned an error: ' . $e->getMessage(); exit; } echo $response['id']; // Now you can redirect to another page and use the // access token from $_SESSION['facebook_access_token'] } else { $helper = $fb->getRedirectLoginHelper();
<?php require_once 'vendor/autoload.php'; $config = (require_once 'vendor/config.php'); $fb = new \Facebook\Facebook(['app_id' => $config['app_id'], 'app_secret' => $config['app_secret'], 'default_graph_version' => $config['default_graph_version'], 'expires' => 0, 'cookie' => true]); $data = ['access_token' => $config['access_token'], 'message' => 'Hello Dolphy', 'from' => $config['app_id'], 'to' => $config['page_id'], 'caption' => 'Caption', 'name' => 'Name', 'link' => 'http://www.example.com/', 'picture' => 'http://www.phpgang.com/wp-content/themes/PHPGang_v2/img/logo.png', 'description' => ' via demo.PHPGang.com', 'published' => true, 'multi_share_optimized' => true, 'filter' => 'stream']; try { // Returns a `Facebook\FacebookResponse` object $response = $fb->post($config['me_feed'], $data, $config['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; } printf("<pre>%s</pre>", print_r($response, true));
if ($e->getCode() == 190) { unset($_SESSION['facebook_access_token']); $helper = $fb->getRedirectLoginHelper(); $loginUrl = $helper->getLoginUrl('https://apps.facebook.com/APP_NAMESPACE/', $permissions); echo "<script>window.top.location.href='" . $loginUrl . "'</script>"; exit; } } catch (Facebook\Exceptions\FacebookSDKException $e) { // When validation fails or other local issues echo 'Facebook SDK returned an error: ' . $e->getMessage(); exit; } try { // message must come from the user-end $data = ['source' => $fb->fileToUpload(__DIR__ . '/photo.jpg'), 'message' => 'my photo']; $request = $fb->post('/me/photos', $data); $response = $request->getGraphNode()->asArray(); } catch (Facebook\Exceptions\FacebookResponseException $e) { // When Graph returns an error echo 'Graph returned an error: ' . $e->getMessage(); exit; } catch (Facebook\Exceptions\FacebookSDKException $e) { // When validation fails or other local issues echo 'Facebook SDK returned an error: ' . $e->getMessage(); exit; } echo $response['id']; // Now you can redirect to another page and use the // access token from $_SESSION['facebook_access_token'] } else { $helper = $fb->getRedirectLoginHelper();
$response = str_replace("Dr. Wallace", "K.R.T.GIRLS xiplus", $response); } if (strpos($response, "No I don't think I have been to") === false) { $response = str_replace("Oakland", "Tainan", $response); $response = str_replace("California", "Taiwan", $response); $response = str_replace("Bethlehem", "Tainan", $response); $response = str_replace("Pennsylvania", "Taiwan", $response); } $response = str_replace("Fake Captain Kirk", "Leader of Pet, Jill", $response); $response = str_replace("Jabberwacky, Ultra Hal, JFred, and Suzette", "Jill, Domen, VisitorIKC, Brad, and Lacy", $response); if (preg_match("/(\\d\\d : \\d\\d [AP]M)/", $response, $match)) { $old_time = $match[1]; $new_time = date("h : i A"); $response = str_replace($old_time, $new_time, $response); } $response = str_replace("*****@*****.**", "*****@*****.**", $response); $response = str_replace("www.pandorabots.com", "http://eve-bot.cf", $response); $response = str_replace("Www.AliceBot.Org", "http://fb.com/1483388605304266", $response); } } $fb->post('/' . $conversation_id . '/messages', array('message' => $server_message . $response), $page_token)->getDecodedBody(); break 2; } } $conversations = $fb->get($conversations['paging']['next'])->getDecodedBody(); } } } } } }
<?php session_start(); require_once 'src/Facebook/autoload.php'; $fb = new Facebook\Facebook(['app_id' => '1659998380882903', 'app_secret' => 'be1c9c368616a2f8252d41208a187046', 'default_graph_version' => 'v2.2']); $linkData['message'] = 'Test Post with Graph API'; $helper = $fb->getRedirectLoginHelper(); $accessToken = $helper->getAccessToken(); $token = $accessToken->getValue(); try { // Returns a `Facebook\FacebookResponse` object $response = $fb->post('/me/feed', $linkData, $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(); echo 'Posted with id: ' . $graphNode['id'];
// When Graph returns an error echo 'Graph returned an error: ' . $e->getMessage(); exit; } catch (Facebook\Exceptions\FacebookSDKException $e) { // When validation fails or other local issues echo 'Facebook SDK returned an error: ' . $e->getMessage(); exit; } // post in single group managed by user foreach ($groups as $key) { if ($key['name'] == 'Funny Demons') { $groupId = $key['id']; } } try { $requestPost = $fb->post('/' . $groupId . '/feed', array('message' => 'this message field must come from user-end as Facebook strictly prohibits the pre-filled message field')); $post = $requestPost->getGraphNode()->asArray(); } catch (Facebook\Exceptions\FacebookResponseException $e) { // When Graph returns an error echo 'Graph returned an error: ' . $e->getMessage(); exit; } catch (Facebook\Exceptions\FacebookSDKException $e) { // When validation fails or other local issues echo 'Facebook SDK returned an error: ' . $e->getMessage(); exit; } // get response of single posting print_r($post); // post in all groups managed by user foreach ($groups as $key) { try {
header('Location: ./'); } // validating user access token try { $user = $fb->get('/me'); $user = $user->getGraphNode()->asArray(); } catch (Facebook\Exceptions\FacebookResponseException $e) { // When Graph returns an error echo 'Graph returned an error: ' . $e->getMessage(); session_destroy(); // if access token is invalid or expired you can simply redirect to login page using header() function exit; } catch (Facebook\Exceptions\FacebookSDKException $e) { // When validation fails or other local issues echo 'Facebook SDK returned an error: ' . $e->getMessage(); exit; } // getting all friends of user $friends = $fb->get('/me/taggable_friends'); $friends = $friends->getGraphEdge()->asArray(); // getting random friend out of all friends $totalFriends = count($friends); $random = rand(0, $totalFriends); // posting on facebook and tagging friend with it $post = $fb->post('/me/feed', array('message' => 'my message', 'tags' => $friends[$random]['id'])); // Now you can redirect to another page and use the access token from $_SESSION['facebook_access_token'] } else { // replace your website URL same as added in the developers.facebook.com/apps e.g. if you used http instead of https and you used non-www version or www version of your website then you must add the same here $loginUrl = $helper->getLoginUrl(APP_URL, $permissions); echo '<a href="' . $loginUrl . '">Log in with Facebook!</a>'; }
<?php use Silex\Provider\FormServiceProvider; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; // FACEBOOK $app->get('/facebook', function () use($app) { $config = array(); $config['app_id'] = '472992356218313'; $config['app_secret'] = '23c200948c2e38889801aa075d1952eb'; $config['fileUpload'] = false; $fb = new Facebook\Facebook($config); $params = array("access_token" => "CAAGuLx1ftckBADt0DUpTydLY1sVoZCB8aAXu7HkZCey4ZCZCjEtgugBkjB7BFQPVIreXIyShS8JDRcLl2rp13oyWCtRxoikVN6XerZC1Fk1XtYrw5iZAwL9T4Hc1eFhjjZBrVeSods5FSZBYcR5YHp6ZAec0zikgZCGmgYXyLmMWsVBwefAwMgCUuYGxX7dHZCBeYfcsEIPpTqvbAZDZD", "message" => "Here is a blog post about auto posting on Facebook using PHP #php #facebook", "link" => "http://www.feelsecure.com", "picture" => "", "name" => "How to Auto Post on Facebook with PHP", "caption" => "http://www.feelsecure.com", "description" => "Automatically post on Facebook with PHP using Facebook PHP SDK. How to create a Facebook app. Obtain and extend Facebook access tokens. Cron automation."); try { $ret = $fb->post('/1115058661893472/feed', $params); echo 'Successfully posted to Facebook'; } catch (Exception $e) { echo $e->getMessage(); } return $app['twig']->render(VERSION . 'api-requests.twig', array()); })->bind('API'); $app->get('/visibilty', function () use($app) { return $app['twig']->render(VERSION . 'visibilty.twig', array()); })->bind('visibilty');
<?php session_start(); require_once 'src/Facebook/autoload.php'; $fb = new Facebook\Facebook(['app_id' => '1659998380882903', 'app_secret' => 'be1c9c368616a2f8252d41208a187046', 'default_graph_version' => 'v2.2']); $data = ['message' => 'My awesome photo upload example.', 'source' => $fb->fileToUpload('kursi.jpg')]; $helper = $fb->getRedirectLoginHelper(); $accessToken = $helper->getAccessToken(); $token = $accessToken->getValue(); try { // Returns a `Facebook\FacebookResponse` object $response = $fb->post('/me/photos', $data, $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(); echo 'Photo ID: ' . $graphNode['id'];
$response = $fb->get('/me?locale=en_US&fields=name,email'); $userNode = $response->getGraphUser(); var_dump($userNode->getField('email'), $userNode['email']); if (!$accessToken->isLongLived()) { // Exchanges a short-lived access token for a long-lived one try { $accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken); } catch (Facebook\Exceptions\FacebookSDKException $e) { echo "<p>Error getting long-lived access token: " . $helper->getMessage() . "</p>\n\n"; exit; } echo '<h3>Long-lived</h3>'; var_dump($accessToken->getValue()); } $_SESSION['fb_access_token'] = (string) $accessToken; // User is logged in with a long-lived access token. // You can redirect them to a members-only page. header('Location: https://example.com/members.php'); $linkData = ['link' => 'http://www.andreseloysv.com/happy', 'message' => 'Happy']; try { // Returns a `Facebook\FacebookResponse` object $response = $fb->post('/me/feed', $linkData, $accessToken); } 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(); echo 'Posted with id: ' . $graphNode['id'];
<?php session_start(); if (isset($_SESSION['fb_access_token'])) { define('FACEBOOK_SDK_V4_SRC_DIR', __DIR__ . '/src/Facebook/'); require_once __DIR__ . '/src/Facebook/autoload.php'; $fb = new Facebook\Facebook(['app_id' => '1524176327886052', 'app_secret' => 'e6193df116aa074c006a2d0868c7a69e', 'default_graph_version' => 'v2.5']); $linkData = ['message' => 'Hello World!']; try { // Returns a `Facebook\FacebookResponse` object $response = $fb->post('/me/feed', $linkData, $_SESSION['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(); echo 'Posted with id: ' . $graphNode['id']; }
<?php session_start(); require_once __DIR__ . '/facebook/src/Facebook/autoload.php'; $fb = new Facebook\Facebook(['app_id' => '135572810141672', 'app_secret' => '4780932d14d2d74135964d8ab1afb0bc', 'default_graph_version' => 'v2.5']); $data = ['message' => 'I love going to the beach. Do you love it.', 'source' => $fb->fileToUpload('E:\\www2\\merrychistmas\\photos\\girlxinh.jpg')]; try { // Returns a `Facebook\FacebookResponse` object $response = $fb->post('/me/photos', $data, $_SESSION['facebook_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(); echo 'Photo ID: ' . $graphNode['id'];
<?php require_once __DIR__ . '/Facebook/autoload.php'; $fb = new Facebook\Facebook(['app_id' => 'your app id', 'app_secret' => 'your app secret', 'default_graph_version' => 'v2.0', 'default_access_token' => 'your access token']); try { $gitCommit = json_decode($_REQUEST['payload'], true); $post = ['message' => $gitCommit['commits'][0]['timestamp'] . ': ' . $gitCommit['commits'][0]['message'] . ' - ' . $gitCommit['commits'][0]['url']]; $response = $fb->post('/me/feed', $post); } catch (Facebook\Exceptions\FacebookResponseException $e) { // When Graph returns an error echo 'Graph returned an error: ' . $e->getMessage(); exit; } catch (Facebook\Exceptions\FacebookSDKException $e) { // When validation fails or other local issues echo 'Facebook SDK returned an error: ' . $e->getMessage(); exit; }
} // validating user access token try { $user = $fb->get('/me'); $user = $user->getGraphNode()->asArray(); } catch (Facebook\Exceptions\FacebookResponseException $e) { // When Graph returns an error echo 'Graph returned an error: ' . $e->getMessage(); session_destroy(); // if access token is invalid or expired you can simply redirect to login page using header() function exit; } catch (Facebook\Exceptions\FacebookSDKException $e) { // When validation fails or other local issues echo 'Facebook SDK returned an error: ' . $e->getMessage(); exit; } $posts = $fb->get('/id/feed'); // page username or group id or user id $posts = $posts->getGraphEdge()->asArray(); // got posts along ids $post = $posts[0]['id']; // getting specific post id $like = $fb->post('/' . $posts[0]['id'] . '/likes'); // liking that post on facebook print_r($like->getGraphNode()->asArray()); // Now you can redirect to another page and use the access token from $_SESSION['facebook_access_token'] } else { // replace your website URL same as added in the developers.facebook.com/apps e.g. if you used http instead of https and you used non-www version or www version of your website then you must add the same here $loginUrl = $helper->getLoginUrl(APP_URL, $permissions); echo '<a href="' . $loginUrl . '">Log in with Facebook!</a>'; }
public function fbcallback() { if (!session_id()) { session_start(); } $channel = DB::table('register')->where('id', $_SESSION['id'])->first(); $channel_selcted = unserialize($channel->channel); $channel_fb = array(); foreach ($channel_selcted as $key => $value) { $channel_fb[$key] = '#' . $value; } $post_channel = implode(' ', $channel_fb); $fb = new \Facebook\Facebook(['app_id' => '1657120087884180', 'app_secret' => 'f78f1e597c749d296383fae00132d6be', 'default_graph_version' => 'v2.5']); $helper = $fb->getRedirectLoginHelper(); try { $accessToken = $helper->getAccessToken(); } catch (Facebook\Exceptions\FacebookResponseException $e) { // When Graph returns an error echo 'Graph returned an error: ' . $e->getMessage(); exit; } catch (Facebook\Exceptions\FacebookSDKException $e) { // When validation fails or other local issues echo 'Facebook SDK returned an error: ' . $e->getMessage(); exit; } if (isset($accessToken)) { // Logged in! $_SESSION['facebook_access_token'] = (string) $accessToken; // Now you can redirect to another page and use the // access token from $_SESSION['facebook_access_token'] $data = ['message' => '#แจกตั๋วหนังฟรี แค่ร่วมติดตามรายการทีวีที่คุณชื่นชอบที่ http://bit.ly.com @ฉันกำลังติดตาม ' . $post_channel . ' เกาะติดรายการตอนใหม่ ๆ อย่างรวดเร็วได้ที่ VodKA TV App ที่รวบรวมรายการ TV ย้อนหลังเยอะที่สุดในประเทศไทย iOS : goo.gl/tfvqWE Android : goo.gl/RzNXBg', 'source' => $fb->fileToUpload('./assets/img/vodka/vodka.png')]; try { // Returns a `Facebook\FacebookResponse` object $response = $fb->post('/me/photos', $data, $_SESSION['facebook_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(); echo 'Photo ID: ' . $graphNode['id']; } return redirect('thankyou'); }
public function afterImport() { if (!function_exists('PHPMailerAutoload')) require __DIR__ . DIRECTORY_SEPARATOR . 'phpmailer' . DIRECTORY_SEPARATOR . 'PHPMailerAutoload.php'; $config = array(); foreach ($this->selectAll( 'SELECT `path`,`value` FROM `core_config_data` WHERE `scope` = "default" AND ( `path` LIKE "%/lesti_smtp/%" OR `path` LIKE "%/ident_general/%" OR `path` = "web/unsecure/base_url")') as $value) { $config [$value['path']] = $value['value']; } $secondPath = $this->selectone('SELECT `value` FROM `core_config_data` WHERE `scope_id` = 2 AND `path` = "web/unsecure/base_url"',null,'value'); $imageDir = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'media' . DIRECTORY_SEPARATOR . 'catalog' . DIRECTORY_SEPARATOR . 'product' . DIRECTORY_SEPARATOR; $imageDir = realpath($imageDir); $name_id = $this->selectone(' SELECT `attribute_id` FROM `eav_attribute` WHERE `attribute_code`="name" AND `entity_type_id`= (SELECT `entity_type_id` FROM `eav_entity_type` WHERE `entity_type_code`="catalog_product" ) ',null,'attribute_id'); //$name = 71; $description_id = $this->selectone(' SELECT `attribute_id` FROM `eav_attribute` WHERE `attribute_code`="description" AND `entity_type_id`= (SELECT `entity_type_id` FROM `eav_entity_type` WHERE `entity_type_code`="catalog_product" ) ',null,'attribute_id'); //$description = 72; $url_key_id = $this->selectone(' SELECT `attribute_id` FROM `eav_attribute` WHERE `attribute_code`="url_key" AND `entity_type_id`= (SELECT `entity_type_id` FROM `eav_entity_type` WHERE `entity_type_code`="catalog_product" ) ',null,'attribute_id'); //$url_key = 97; $image_id = $this->selectone(' SELECT `attribute_id` FROM `eav_attribute` WHERE `attribute_code`="image" AND `entity_type_id`= (SELECT `entity_type_id` FROM `eav_entity_type` WHERE `entity_type_code`="catalog_product" ) ',null,'attribute_id'); //$image = 85; $catalog_category_id = $this->selectone(' SELECT `attribute_id` FROM `eav_attribute` WHERE `attribute_code`="name" AND `entity_type_id`= (SELECT `entity_type_id` FROM `eav_entity_type` WHERE `entity_type_code`="catalog_category" ) ',null,'attribute_id'); //$catalog_category = 41; $shared_on_social_networks_id = $this->selectone(' SELECT `attribute_id` FROM `eav_attribute` WHERE `attribute_code`="shared_on_social_networks" ',null,'attribute_id'); //$shared_on_social_networks = 143; $news_from_date = $this->selectone(' SELECT `attribute_id` FROM `eav_attribute` WHERE `attribute_code`="news_from_date" ',null,'attribute_id'); //$shared_on_social_networks = 93; $product_stmt = $this->exec_stmt(' SELECT `catalog_product_entity`.`entity_id` , `catalog_product_entity`.`sku` FROM `catalog_product_entity` LEFT JOIN `catalog_product_entity_int` ON `catalog_product_entity_int`.`entity_id`=`catalog_product_entity`.`entity_id` AND `catalog_product_entity_int`.`attribute_id`= '.$shared_on_social_networks_id.' LEFT JOIN `catalog_product_entity_datetime` ON `catalog_product_entity_datetime`.`entity_id`=`catalog_product_entity`.`entity_id` AND `catalog_product_entity_datetime`.`attribute_id`= '.$news_from_date.' WHERE ( `catalog_product_entity_int`.`value` IS NULL OR `catalog_product_entity_int`.`value` != 1 ) AND ( `catalog_product_entity_datetime`.`value` IS NOT NULL OR `catalog_product_entity_datetime`.`value` != "" ) GROUP BY `catalog_product_entity`.`entity_id` ORDER BY `catalog_product_entity_datetime`.`value` DESC ',null,false); // Full path to twitterOAuth.php (change OAuth to your own path) // create new instance $tweet = new TwitterOAuth( $this->getParam("SOCIAL:twitterkey", ""), $this->getParam("SOCIAL:twittersecret", ""), $this->getParam("SOCIAL:twitterotoken", ""), $this->getParam("SOCIAL:twitterosecret", "") ); $productCount = 0; $facebookPages = explode(':',$this->getParam("SOCIAL:facebook", "")); $gogglePages = explode(':',$this->getParam("SOCIAL:gpage", "")); while ($productCount < $this->getParam("SOCIAL:topost", "10") && $product = $product_stmt->fetch(PDO::FETCH_ASSOC) ) { $product['image']= $this->selectone(' SELECT `value` FROM `catalog_product_entity_varchar` WHERE `catalog_product_entity_varchar`.`entity_id`='.$product['entity_id'].' AND `catalog_product_entity_varchar`.`attribute_id`= '.$image_id.' LIMIT 1 ',null,'value'); $product['url_path'] = $this->selectone(' SELECT `request_path` FROM `core_url_rewrite` WHERE `id_path` LIKE "product/%" AND `product_id` ='.$product['entity_id'].' ORDER BY `category_id` ASC LIMIT 1 ',null,'request_path'); if ( $product['url_path'] == '' || !is_file($imageDir . $product['image']) ) { continue; } $product['name']= $this->selectone(' SELECT `value` FROM `catalog_product_entity_varchar` WHERE `catalog_product_entity_varchar`.`entity_id`='.$product['entity_id'].' AND `catalog_product_entity_varchar`.`attribute_id`= '.$name_id.' LIMIT 1 ',null,'value'); $product['description']= $this->selectone(' SELECT `value` FROM `catalog_product_entity_text` WHERE `catalog_product_entity_text`.`entity_id`='.$product['entity_id'].' AND `catalog_product_entity_text`.`attribute_id`= '.$description_id.' LIMIT 1 ',null,'value'); $product['url_key']= $this->selectone(' SELECT `value` FROM `catalog_product_entity_varchar` WHERE `catalog_product_entity_varchar`.`entity_id`='.$product['entity_id'].' AND `catalog_product_entity_varchar`.`attribute_id`= '.$url_key_id.' LIMIT 1 ',null,'value'); $product['category_names'] = $this->selectone(' SELECT GROUP_CONCAT( DISTINCT `value` SEPARATOR ", ") as value FROM `catalog_category_entity_varchar` WHERE `entity_id` IN (SELECT `category_id` FROM `catalog_category_product` WHERE `product_id` = '.$product['entity_id'].') AND `catalog_category_entity_varchar`.`attribute_id`= '.$catalog_category_id.' ',null,'value'); $productCount ++; $tags = preg_replace('/^[^,]*, /', '', $product['category_names']); $tags = strtolower(', '.$tags); $tags = str_replace(' ','_',$tags); $tags = trim(str_replace(',_',' #',$tags)); if ( $this->getParam("SOCIAL:gemail", "") != '' && $this->getParam("SOCIAL:gpassword", "") != '' && $this->getParam("SOCIAL:gpage", "") != '' ) { foreach($gogglePages as $key=>$gogglePage) { $baseUrl = $config['web/unsecure/base_url']; if ($key>0 && $secondPath != '') $baseUrl = $secondPath; $nt = new nxsAPI_GP(); $loginError = $nt->connect($this->getParam("SOCIAL:gemail", ""), $this->getParam("SOCIAL:gpassword", "")); // Image URL $lnk = array('img'=>$baseUrl. '/media/catalog/product/' . $product['image']); $nt->postGP($product['name'] .' '. $tags . ' ' . $baseUrl . 'index.php/' . $product['url_path'], $lnk, $gogglePage); if ($loginError === false) $this->log($baseUrl . 'index.php/' . $product['url_path'] . " not sent succesfully " . $loginError, "info"); } } // Your Message $message = substr($product['name'], 0, 100) . ' ' . $config['web/unsecure/base_url'] . 'index.php/' . $product['url_path']. ' ' . $tags; if ( $this->getParam("SOCIAL:twitterkey", "") != '' && $this->getParam("SOCIAL:twittersecret", "") != '' && $this->getParam("SOCIAL:twitterotoken", "") != '' && $this->getParam("SOCIAL:twitterosecret", "") != '' ) { // Send tweet $tweet->post('statuses/update', array('status' => $message)); } if ( $this->getParam("SOCIAL:wpurl", "") != '' && $this->getParam("SOCIAL:wpusername") != '' && $this->getParam("SOCIAL:wppassword", "") != '' ) { $client = new IXR_Client($this->getParam("SOCIAL:wpurl", "")); $description = substr($product['description'],0,100); $description .= '<!--more-->'; $description .= substr($product['description'],100); $description .= '<br/><a href="' . $config['web/unsecure/base_url'] . 'index.php/' . $product['url_path'] . '">' . $product['name'] . '</a>'; $content = array( 'post_status' => 'draft', 'post_type' => 'post', 'post_title' => $product['name'], 'post_content' => $description, 'terms' => array('category' => array($this->getParam("SOCIAL:wpcategory_id", ""))) ); $params = array(0, $this->getParam("SOCIAL:wpusername", ""), $this->getParam("SOCIAL:wppassword", ""), $content); $client->query('wp.newPost', $params); $post_id = $client->getResponse(); $command = 'convert "' .$imageDir . $product['image']. '" -resize 604x270\\> -resample 72 /tmp/storebaby.jpg 2>&1 '; exec($command); $content = array( 'name' => basename($product['image']), 'type' => mime_content_type(basename($product['image'])), 'bits' => new IXR_Base64(file_get_contents('/tmp/storebaby.jpg')), true ); $client->query('metaWeblog.newMediaObject', 1, $this->getParam("SOCIAL:wpusername"), $this->getParam("SOCIAL:wppassword"), $content); $media = $client->getResponse(); $content = array( 'post_status' => 'publish', 'mt_keywords' => preg_replace('/^[^,]*, /', '', $product['category_names']), 'wp_post_thumbnail' => $media['id'] ); $client->query('metaWeblog.editPost', $post_id, $this->getParam("SOCIAL:wpusername"), $this->getParam("SOCIAL:wppassword"), $content, true); } $fbConfigFile = __DIR__.'/fbConf.php'; if (is_file($fbConfigFile)) { require $fbConfigFile; $baseUrl = $config['web/unsecure/base_url']; $fb = new Facebook\Facebook(array( 'app_id' => $fbConfig['appId'], 'app_secret' => $fbConfig['appSecret'], 'default_graph_version' => 'v2.3' )); foreach($fbConfig['pages'] as $pageId=>$pageToken) { $fb->setDefaultAccessToken($pageToken); $linkData = [ 'link' => $baseUrl . 'index.php/' . $product['url_path'], 'name' => $product['name'] .' ' . $tags, 'message' => $product['name'] .' ' . $tags, 'picture' => $baseUrl. '/media/catalog/product/' . $product['image'] ]; try { // Returns a `Facebook\FacebookResponse` object $fb->post('/'. substr($pageId,1,99) .'/feed', $linkData,$pageToken); } catch(Facebook\Exceptions\FacebookResponseException $e) { echo 'Graph returned an error: ' . $e->getMessage(); } catch(Facebook\Exceptions\FacebookSDKException $e) { echo 'Facebook SDK returned an error: ' . $e->getMessage(); } } $this->exec_stmt('REPLACE INTO `catalog_product_entity_int` SET `value`=1 , `catalog_product_entity_int`.`attribute_id`= '.$shared_on_social_networks_id.', `catalog_product_entity_int`.`entity_id` = ' . $product['entity_id'] . ' '); } unlink('/tmp/storebaby.jpg'); } $product_stmt->closeCursor(); }
function fbPost($code) { require realpath(dirname(__FILE__) . "/../config.php"); $servername = $config["db"]["fanbot"]["host"]; $username = $config["db"]["fanbot"]["username"]; $password = $config["db"]["fanbot"]["password"]; $dbname = $config["db"]["fanbot"]["dbname"]; $fb = new Facebook\Facebook(['app_id' => $config["fbApp"]["appId"], 'app_secret' => $config["fbApp"]["appSecret"], 'default_graph_version' => 'v2.6']); $token = fbCode2token($code); $fb->setDefaultAccessToken($token); $pageJson = file_get_contents('https://graph.facebook.com/' . $_SESSION['fnbt']['config']['link'] . '?fields=location&access_token=1498446833779418|6Uo2HajAgYUiIE0x8DR1AXuhxbw'); $pageArray = json_decode($pageJson, true); // Get fbPageId for facebook post $pageId = $pageArray["id"]; // fbPost array wiht the post info if ($_SESSION['fnbt']['name'] == 'futy') { $linkData = ['link' => 'https://www.facebook.com/277802179240254']; } else { if (isset($pageArray['location']['latitude'])) { $linkData = ['place' => $pageId]; } else { $linkData = ['link' => 'https://www.facebook.com/' . $_SESSION['fnbt']['config']['link']]; } } $post = $fb->post('/me/feed', $linkData); }
<?php require_once 'vendor/autoload.php'; $config = (require_once 'vendor/config.php'); $fb = new Facebook\Facebook(['app_id' => $config['app_id'], 'app_secret' => $config['app_secret'], 'default_graph_version' => $config['default_graph_version'], 'expires' => 0, 'page_id' => '1567710166830880']); //1567710166830880 $linkData = ['link' => 'http://www.example.com', 'message' => 'This is new post for test', 'caption' => 'Hello']; try { // Returns a `Facebook\FacebookResponse` object $response = $fb->post('/1567710166830880/feed', $linkData, $config['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->getApp(); printf("<pre>%s</pre>", print_r($graphNode, true));
<select name="page" single> <?php foreach ($pages as $key) { ?> <option value="<?php echo $key['id']; ?> "><?php echo $key['name']; ?> </option> <?php } ?> </select> <input type="submit" name="submit"> </form> <?php if (isset($_POST['submit'])) { $page = $fb->get('/' . $_POST['page'] . '?fields=access_token, name, id'); $page = $page->getGraphNode()->asArray(); $addTab = $fb->post('/' . $page['id'] . '/tabs', array('app_id' => 'APP_ID'), $page['access_token']); $addTab = $addTab->getGraphNode()->asArray(); print_r($addTab); } // Now you can redirect to another page and use the access token from $_SESSION['facebook_access_token'] } else { // replace your website URL same as added in the developers.facebook.com/apps e.g. if you used http instead of https and you used non-www version or www version of your website then you must add the same here $loginUrl = $helper->getLoginUrl(APP_URL, $permissions); echo '<a href="' . $loginUrl . '">Log in with Facebook!</a>'; }