Koneksi Ke ArangoDB menggunakan PHP

Ditulis oleh Muhammad Arslan, dipublikasi pada 18 Oct 2017 dalam kategori Tutorial
Koneksi Ke ArangoDB menggunakan PHP - CodePolitan.com

ArangoDB adalah salah satu database NoSQL yang dikembangkan oleh suatu perusahaan di Jerman. ArangoDB memiliki dua versi yaitu enterprise dan komunitas. ArangoDB mengklaim dirinya sebagai database yang bersifat multimodel dimana kamu dapat menggunakan paradigma document-based, key-value, dan graph database.

Seri Belajar PHP - Cara Install PHP, MariaDB, Apache di Linux dengan XAMPP

Di PHP, kamu dapat menggunakan library triagens/arangodb untuk menggunakan ArangoDB di dalam kode program PHP. Berikut adalah cara install-nya:

$ cd /path/to/your/project/folder
$ composer require triagens/arangodb

Setelah berhasil mendapatkan modul triagens/arangodb, kamu dapat mulai menggunakan library tersebut di dalam kode PHP atau web framework PHP kamu. Berikut adalah contoh script PHP untuk memeriksa koneksi ke ArangoDB:

<?php

require dirname(__FILE__) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';

use ArangoDBClient\Connection as ArangoConnection;
use ArangoDBClient\ConnectionOptions as ArangoConnectionOptions;
use ArangoDBClient\UpdatePolicy as ArangoUpdatePolicy;

$connectionOptions =array(
    // server endpoint to connect to
    ArangoConnectionOptions::OPTION_ENDPOINT => 'tcp://127.0.0.1:8529',
    // authorization type to use (currently supported: 'Basic')
    ArangoConnectionOptions::OPTION_AUTH_TYPE => 'Basic',
    // user for basic authorization
    ArangoConnectionOptions::OPTION_AUTH_USER => 'root',
    // password for basic authorization
    ArangoConnectionOptions::OPTION_AUTH_PASSWD => 'root',
    // connection persistence on server. can use either 'Close' (one-time connections) or 'Keep-Alive' (re-used connections)
    ArangoConnectionOptions::OPTION_CONNECTION => 'Close',
    // connect timeout in seconds
    ArangoConnectionOptions::OPTION_TIMEOUT => 3,
    // whether or not to reconnect when a keep-alive connection has timed out on server
    ArangoConnectionOptions::OPTION_RECONNECT => true,
    // optionally create new collections when inserting documents
    ArangoConnectionOptions::OPTION_CREATE => true,
    // optionally create new collections when inserting documents
    ArangoConnectionOptions::OPTION_UPDATE_POLICY => ArangoUpdatePolicy::LAST,
);

// open connection
$connection = new ArangoConnection($connectionOptions);

var_dump($connection);

Bila kita jalankan kode diatas, maka akan muncul output berikut:

$ php test-connection.php
object(ArangoDBClient\Connection)#3 (10) {
  ["_options":"ArangoDBClient\Connection":private]=>
  object(ArangoDBClient\ConnectionOptions)#2 (2) {
    ["_values":"ArangoDBClient\ConnectionOptions":private]=>
    array(26) {
      ["endpoint"]=>
      string(20) "tcp://127.0.0.1:8529"
      ["host"]=>
      NULL
      ["port"]=>
      int(8529)
      ["timeout"]=>
      int(3)
      ["createCollection"]=>
      bool(true)
      ["policy"]=>
      string(4) "last"
      ["rev"]=>
      NULL
      ["waitForSync"]=>
      bool(false)
      ["batchSize"]=>
      NULL
      ["journalSize"]=>
      int(33554432)
      ["isSystem"]=>
      bool(false)
      ["isVolatile"]=>
      bool(false)
      ["Connection"]=>
      string(5) "Close"
      ["trace"]=>
      NULL
      ["enhancedTrace"]=>
      bool(false)
      ["verifyCert"]=>
      bool(false)
      ["allowSelfSigned"]=>
      bool(true)
      ["ciphers"]=>
      NULL
      ["AuthUser"]=>
      string(4) "root"
      ["AuthPasswd"]=>
      string(4) "root"
      ["AuthType"]=>
      string(5) "Basic"
      ["Reconnect"]=>
      bool(true)
      ["Batch"]=>
      bool(false)
      ["BatchPart"]=>
      bool(false)
      ["database"]=>
      string(7) "_system"
      ["CheckUtf8Conform"]=>
      bool(false)
    }
    ["_endpoint":"ArangoDBClient\ConnectionOptions":private]=>
    object(ArangoDBClient\Endpoint)#4 (1) {
      ["_value":"ArangoDBClient\Endpoint":private]=>
      string(20) "tcp://127.0.0.1:8529"
    }
  }
  ["_httpHeader":"ArangoDBClient\Connection":private]=>
  string(73) "
Host: 127.0.0.1
Authorization: Basic cm9vdDpyb290
Connection: Close
"
  ["_baseUrl":"ArangoDBClient\Connection":private]=>
  string(12) "/_db/_system"
  ["_handle":"ArangoDBClient\Connection":private]=>
  NULL
  ["_useKeepAlive":"ArangoDBClient\Connection":private]=>
  bool(false)
  ["_batches":"ArangoDBClient\Connection":private]=>
  array(0) {
  }
  ["_activeBatch":"ArangoDBClient\Connection":private]=>
  NULL
  ["_captureBatch":"ArangoDBClient\Connection":private]=>
  bool(false)
  ["_batchRequest":"ArangoDBClient\Connection":private]=>
  bool(false)
  ["_database":"ArangoDBClient\Connection":private]=>
  string(7) "_system"
}


Berlangganan Informasi CodePolitan Melalui Email

Jangan sampai kamu melewatkan informasi penting tentang pemrograman dan teknologi! Kamu bisa berlangganan Newsletter CodePolitan dengan cara mengisi formulir di samping.