Implement Facebook Instant Articles using PHP SDK

Facebook Instant Article is immensely popular these days. With Instant articles, publisher can create fast, interactive articles on Facebook. In addition to this, Instant Article is a great way to monetize article.

Problem with RSS feed is it takes time to load new articles as it depends on FB end solely to fetch new articles from RSS. Sometimes, it may take long time to fetch new article. As a result of this, we may lose our revenue.

In this article, we came up with integrating Facebook Instant Articles using PHP SDK. Using API, we can automatically push instant article to FB through our own CMS.

Required Credentials

1. FB App ID
2. FB App Secret
3. FB Page ID
4. Access Token.

Components of SDK

Before we start integrating Facebook Instant Article API, lets explore the components of SDK in brief.

Element: It is a domain-specific language for creating an Instant Articles structure that strictly follows the specification and can be automatically serialized into the subset of HTML5 markup used in the Instant Articles format. Element allows users to programmatically create Instant Articles that are guaranteed to be in compliance with the format.

Transformer: It is an engine for transforming any markup into an Instant Article structure in the DSL. The engine runs a set of rules on the markup that will specify the selection and transformation of elements output by the CMS into their Instant Articles counterparts.

Client: It is simple wrapper around the Instant Articles API, which can be used for publishing Instant Articles on Facebook. The client provides a CRUD interface for Instant Articles as well as a helper for authentication. The client depends on the main Facebook SDK for PHP as an interface to the Graph API and Facebook Login.

Parser: It is an engine for transforming any Instant Article HTML markup into an InstantArticle Element structure. The Parser component uses the Transformer component under the hood.

Installing Facebook Instant Articles PHP SDK

We can use composer to install Facebook Instant Articles PHP SDK with the following command:

composer require facebook/facebook‐instant‐articles‐sdk‐php

 

Now it’s time to start coding. Create a

<?php

use Facebook\InstantArticles\Elements\InstantArticle;
use Facebook\InstantArticles\Elements\Header;
use Facebook\InstantArticles\Elements\Time;
use Facebook\InstantArticles\Elements\Ad;
use Facebook\InstantArticles\Elements\Analytics;
use Facebook\InstantArticles\Elements\Author;
use Facebook\InstantArticles\Elements\Image;
use Facebook\InstantArticles\Elements\Video;
use Facebook\InstantArticles\Elements\Caption;
use Facebook\InstantArticles\Elements\Footer;
use Facebook\InstantArticles\Transformer\Transformer;

use Facebook\InstantArticles\Client\Client;
use Facebook\InstantArticles\Client\Helper;
use Facebook\InstantArticles\Validators\Type;
use Facebook\Facebook;



include_once('path-to-sdk/vendor/autoload.php');


$charset = 'UTF-8';
$title = 'Title of Article';
$subtitle = 'Subtitle of Article'
$kicker = 'Kicker Text';
$cannonical_link = 'Link to the article in your website';

$APP_ID = 'fb-app-id';
$APP_SECRET = 'fb-app-secret';
$PAGE_ID = 'fb-page-id';
$ACCESS_TOKEN =  'page-access-token';

$is_development = false; // Use development environment?
$is_published = true;


$published_date = 'published_date';
$last_modified_date = 'last_modified_date';
  
$header =
    Header::create()
      ->withPublishTime(
        Time::create( Time::PUBLISHED )->withDatetime(
            \DateTime::createFromFormat(
              'j-M-Y G:i:s',
              date('j-M-Y G:i:s', strtotime($published_date))
            ))
      )
      ->withModifyTime(
        Time::create( Time::MODIFIED )->withDatetime(
            \DateTime::createFromFormat(
              'j-M-Y G:i:s',
              date('j-M-Y G:i:s', strtotime($last_modified_date))
            ))
      );




// Loads the rules configuration file
$rules_file_content = file_get_contents("rules-configuration.json", true); 


// Load html content from a file. 
$content = file_get_contents("sample-html.html", true);

// Create a transformer object and load the rules
$transformer = new Transformer();
$transformer->loadRules($rules_file_content);


$document = new DOMDocument();
libxml_use_internal_errors(true);
$document->loadHTML( '<?xml encoding="' . $charset . '" ?><h1>' . $title . '</h1>' );
libxml_use_internal_errors(false);
$transformer->transform( $header, $document );


if($subtitle) {
  $header->withSubTitle ($subtitle);
}


if ( $kicker ) {
  $header->withKicker( $kicker );
}


$instant_article =
      InstantArticle::create()
        ->withCanonicalUrl( $cannonical_link )
        ->withHeader( $header )
        ->addMetaProperty( 'op:generator:application', 'facebook-instant-articles' )
        ->addMetaProperty( 'op:generator:application:version', IA_PLUGIN_VERSION );


$instant_article->withStyle( 'default' );


$transformer->transformString( $instant_article, $content, $charset );


// Instantiate an API client
$client = Client::create(
  $APP_ID,
  $APP_SECRET,
  $ACCESS_TOKEN,
  $PAGE_ID,
  $is_development 
);


// Import the article
try {
    $client->importArticle($instant_article, $is_published);
} catch (Exception $e) {
    echo 'Could not import the article: '.$e->getMessage();
}

 

Hope you enjoy this article. In the next article, we will write about deleting the instant articles.

7 Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.