PHP legacy drivers

This example can be used to attach to Astra DB from PHP using the DataStax Driver. Before proceeding you might also consider using the REST, GraphQL or Document APIs to connect to Astra DB without the need for a custom driver.

Prerequisites

Procedure

  1. Copy the certificate files from the Astra DB secure connect bundle to /etc/ssl/certs/astra-secure-connect. This folder should contain the ca.cert, cert, and key files.

  1. Replace the <hostname> with your Astra DB database hostname.

  1. Replace the <port> with your Astra DB database port.

  1. Replace <username> and <password> with the Astra DB database Client ID and Client Secret.

<?php

$ssl     = Cassandra::ssl()
               \->withTrustedCerts('/etc/ssl/certs/astra-secure-connect/ca.cert')
               \->withClientCert('/etc/ssl/certs/astra-secure-connect/cert')
               \->withPrivateKey('/etc/ssl/certs/astra-secure-connect/key')
               \->build();


$cluster = Cassandra::cluster()
               \->withSSL($ssl)
               \->withCredentials("dbadmin", "my_awesome_password")
               \->withContactPoints('602c8f3c-ea96-41b0-a7c1-6c6c5bdd1f34-us-east1.db.astra.datastax.com')
               \->withPort(31575)
               \->build();

$session = $cluster\->connect();

$result = $session\->execute('SELECT keyspace_name, table_name FROM system_schema.tables');

foreach ($result as $row) {
    printf("The keyspace "%s" has a table "%s".
    ", $row['keyspace_name'], $row['table_name']);
}"

Example

<?php

$ssl     = Cassandra::ssl()
               \->withTrustedCerts('/etc/ssl/certs/astra-secure-connect/ca.cert')
               \->withClientCert('/etc/ssl/certs/astra-secure-connect/cert')
               \->withPrivateKey('/etc/ssl/certs/astra-secure-connect/key')
               \->build();

$cluster = Cassandra::cluster()
               \->withSSL($ssl)
               \->withCredentials("john.smith", "jsP@ssw0rd")
               \->withContactPoints('602c8f3c-ea96-41b0-a7c1-6c6c5bdd1f34-us-east1.db.astra.datastax.com')
               \->withPort(31575)
               \->build();
$session = $cluster\->connect();

$result = $session\->execute('SELECT keyspace_name, table_name FROM system_schema.tables');

foreach ($result as $row) {
    printf("The keyspace "%s" has a table "%s".
    ", $row['keyspace_name'], $row['table_name']);
    }"

In the example above, the following variables are used:

  • <hostname> is 602c8f3c-ea96-41b0-a7c1-6c6c5bdd1f34-us-east1.db.astra.datastax.com

  • <port> is 31575

  • <username> is john.smith

  • <password> is jsP@ssw0rd