As social networks gain more and more ground, customers are often confronted with the task of integrating their sites with Facebook, Twitter, Google+, etc. While website development recently we were asked to establish the cross-posting of the customer site with a Facebook public page. The relevant desk-top research having been done (Facebook's API and the existing tools for Drupal integration investigated), we eventually bookmarked a method which we are going to describe in this post.
Secondly, you will have to create an application in Facebook to establish connection between it and the customer site. For this, a package of Drupal for Facebook modules will have to be installed and configured. Though, the latter lacks the public pages posting function, it nevertheless has many useful functions and modules that help fulfill various tasks related to Drupal integration with Facebook.
/** * Auto publish facebook. */ function internetdevels_autopublish_facebook($params) { if ($fb = _fb_api_init()) { try { $page_info = $fb->api('/' . FB_PAGE_ID . '?fields=access_token'); $params['access_token'] = $page_info['access_token']; $fb->api('/' . FB_PAGE_ID . '/feed', 'POST', $params); } catch (FacebookApiException $e) { drupal_set_message(t('Not published to Facebook')); } } }
Here’s an example of a function that will make posting to FB possible. It can be done with the help of hook_node_presave(), hook_node_insert() or with any other functions relevant to the current task.
Sparams — the script array for uploading and publishing the content; Sparams['description'] — description; Sparams['picture'] — picture; Sparams['name'] — title of the post; Sparams['link'] — link.
_fb_api_init() — a fb module function to check whether a user is logged in FB or not, as well as creating the object on FACEBOOK.
FB_PAGE_ID — the public page ID.
Spage_info — with this we get the token so that the author of the post would be a public page, not your Facebook account.
Additional information:
https://developers.facebook.com/docs