PHP legacy driver
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
-
Ensure the Python driver is installed. For more, see DataStax PHP Driver for Apache Cassandra.
-
Collect the required information for Connecting with legacy drivers.
-
Get your Client ID and Client Secret by creating your application token for your username and password.
Procedure
-
Copy the certificate files from the Astra DB secure connect bundle to
/etc/ssl/certs/astra-secure-connect
. This folder should contain theca.cert
,cert
, andkey
files. -
Replace the
<hostname>
with your Astra DB database hostname. -
Replace the
<port>
with your Astra DB database port. -
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>
is602c8f3c-ea96-41b0-a7c1-6c6c5bdd1f34-us-east1.db.astra.datastax.com
-
<port>
is31575
-
<username>
isjohn.smith
-
<password>
isjsP@ssw0rd