Firebase & Drupal

Firebase & Drupal

In this post, I will describe how to synchronise content from Drupal into a database Firestore.

Authentication

First, we will need a Firebase project, a service account to communicate with the Firebase service, and a configuration file with your service account's credentials.

To generate a private key file for your service account, in the Firebase console, open Settings > Service Accounts.

Then, for Drupal, we will use JWT PHP library to provide a token of authentication.

composer require firebase/php-jwt
use Firebase\JWT\JWT;

function getToken(){
$str = file_get_contents('path/service-account.json');
$account = json_decode($str, true);
$now = time();
$token = array(
    "iss" => $account['client_email'],
    "scope" => "https://www.googleapis.com/auth/firebase",
    "aud" => "https://www.googleapis.com/oauth2/v4/token",
    'exp' => $now + 65, // arbitrary
    'iat' => $now,
);

$jwt = JWT::encode($token, $account['private_key'], 'RS256');
$headers = [
    'Content-Type: application/x-www-form-urlencoded',
];
$data = 'grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion='.$jwt;

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://www.googleapis.com/oauth2/v4/token' );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt( $ch,CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch );
curl_close( $ch );
$token = json_decode($result, true);
$access_token = isset($token['access_token']) ? $token['access_token'] : "";
}

Send Data from Drupal

Now, we will send data from Drupal to Firebase Firestore, we can use curl with patch method.

For more information https://firebase.google.com/docs/firestore/reference/rest/v1beta1/projects.databases.documents/patch

PATCH https://firestore.googleapis.com/v1beta1/{document.name=projects/*/databases/*/documents/*/**}

$data = [
    "fields" => [
        'field1' => ['booleanValue' => true],
        "field2" => ['stringValue' => "valeur"],
        "title" => ['stringValue' => "title"],
        "body" => ['stringValue' => "text"],
    ]
];
    $token = $this->getToken();
    $headers = [
        'Authorization: Bearer '.$token,
        'Content-Type: application/json',
    ];
    #Send Reponse To FireBase Server
    $ch = curl_init();
    curl_setopt( $ch,CURLOPT_URL, 'https://firestore.googleapis.com/v1/projects/project_id/databases/(default)/documents/document/key');
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH");
    curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
    curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode($data));
    $result = curl_exec($ch );
    $response = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close( $ch );

firebase drupal

Conclusion

Finally, we can pull data from Drupal to Firestore with JWT authentication, we can use that for others firebase tools like Remote config.

Comments