Updating Facebook Pages/Places using Facebook APIs

If someone likes to update the information like address, working hours, etc of multiple Facebook places from their application (without actually going to Facebook), Facebook has provided the apis to do so. Below we have listed simple steps how to do it.

Difference between Page and Place

Facebook Page: Like a friend's profile, Facebook Pages enable public figures, businesses, organizations and other entities to create an authentic and public presence on Facebook. Unlike your profile, Facebook Pages are visible to everyone on the internet by default. You, and every person on Facebook, can connect with these Pages by becoming a fan and then receive their updates in your News Feed and interact with them.

Facebook Place: Facebook places are also the pages but it has address where its located, basically its a location.

  1. Connection to the Facebook Account: 

    First of all our web application needs to be connected the main Page where all the Places are connected. We need to have the admin access of the page.

    Requirements:
    1. Facebook App:  Facebook Application can be created from here https://developers.facebook.com/apps. After creating an app you will get App ID/API Key just store it somewhere.
    2. Access Token:  When someone connects with an app using Facebook Login, the app will be able to obtain an access token which provides temporary, secure access to Facebook APIs.

    How to get Access Token:
    // App Id of you application (client_id)
    $appId = 'XXXXXX';
    //redirect_uri – this should be the URL to your website.
    $redirectUrl = 'http://yourhost.com/action';
    //scope – this is what you want the access code to allow access to.
    $scope = 'user_about_me,create_event,manage_pages,publish_stream,user_photos,friends_photos';
    $url = 'https://graph.facebook.com/oauth/authorize?type=user_agent&client_id=';
    $url .= $appId;
    $url .= '&redirect_uri=' . $redirectUrl;
    $url .= '&scope=' . $scope;
    header("Location:$url");

    You will get the access-token in the redirected action, save it some where and use it whenever required. Now you are connected to the Facebook account.
    Above access token is temporary, will be expired in 2 hours. It can be extended by below curl call:

    $params = array(
    'grant_type' => 'fb_exchange_token',
    'client_id' => AppId,
    'client_secret' => AppSecretId, //You will get this in app details
    'fb_exchange_token' => AccessToken //Access token obtained from previous api
    );
    $curlUrl = https://graph.facebook.com/oauth/access_token?'.http_build_query($params);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $curlUrl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);
    $accessToken = explode('=', $output);

  2. Connection to the Facebook Place:

    In order to update the Place, again we need to connect it with our website.
    Below is the code snippet:


    $mainPageID = Id of the main page where all the places are connected.
    $url = 'https://graph.facebook.com/' . $mainPageID.'?fields=access_token&method=get&access_token=' . $accessToken;
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($ch);
    curl_close($ch);
    $token = json_decode($result);
    $pageAccessToken = $token->access_token;

    Now you are connected to the main page, where all the places are connected. You can update all the places/locations connected to this page with step 3.

  3. Update place/location with given information:

    Update the place/location with given information with following curl call.

    $url =  'https://graph.facebook.com/' .$ mainPageID . '/locations';
    $location = array(
    'street' => 'street',
    'city' => 'city',
    'state' => 'state',
    'country' => 'country',
    'zip' => 'zip',
    'latitude' => 'latitude',
    'longitude' => 'longitude'
    );

    $params = array(
    'access_token' => $pageAccessToken,
    'main_page_id' => $mainPageID,
    'location_page_id' => $pageId, // id of the place, you want to update.
    'store_number' => 'storeId', // You will get this id in the place details.
    'location' => json_encode($location),
    'phone' => 'phone',
    'hours' => 'workingHours'
    );

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);
    curl_close($ch);

    For more information about the parameters please see here: https://developers.facebook.com/docs/graph-api/reference/v2.1/page/locations

    For more information on errors codes place see here:  https://developers.facebook.com/docs/reference/ads-api/error-reference

Categories: Flow Framework, APIs

Copyright © 2019 Lelesys Informatik GmbH, Deutschland. All Rights Reserved.