Is there a way to utilize an Oracle wallet via PHP on windows?

Viewed 306

I have a PHP codebase that resides on a Windows server that connects to an Oracle database. My database is moving to a Oracle in the cloud setup that requires us to use an Oracle wallet to connect.

I got the setup for the wallet and can connect via the utilities, but all my research thus far has me using oci_connect() with a session_mode of OCI_CRED_EXT. But PHP's oci_connect (https://www.php.net/manual/en/function.oci-connect.php) states "OCI_CRED_EXT is not supported on Windows for security reasons.".

Is there a way to utilize an Oracle wallet via PHP on windows?

1 Answers

The Oracle Cloud wallets provide mutual TLS for security but you still need to pass a DB username and password. Hence you don't use external authentication.

You just need to make sure that Oracle Net can find the wallet, either with the Easy Connect Plus syntax used below (wallet_location is the dir containing the cwallet.sso file), or by editing sqlnet.ora (or putting it in a default location). See How to connect to Oracle Autonomous Cloud Databases.

Code that works for me is:

<?php

error_reporting(E_ALL);
ini_set('display_errors', 'On');

$c = oci_connect('cj', 'topsecret', 'tcps://adb.ap-melbourne-1.oraclecloud.com:1522/xxxxxx.adb.oraclecloud.com?wallet_location=/Users/cjones/Cloud/alwaysfree/CJDBMELB');

if (!$c) {
    $m = oci_error();
    trigger_error('Could not connect to database: '. $m['message'], E_USER_ERROR);
}

$s = oci_parse($c, "select * from dual");
if (!$s) {
    $m = oci_error($c);
    trigger_error('Could not parse statement: '. $m['message'], E_USER_ERROR);
}

$r = oci_execute($s);
if (!$r) {
    $m = oci_error($s);
    trigger_error('Could not execute statement: '. $m['message'], E_USER_ERROR);
}

while (($row = oci_fetch_array($s, OCI_ASSOC+OCI_RETURN_NULLS)) != false) {
    echo "<tr>\n";
    foreach ($row as $item) {
        echo "<td>";
        echo $item!==null?htmlspecialchars($item, ENT_QUOTES|ENT_SUBSTITUTE):"&nbsp;";
        echo "</td>\n";
    }
    echo "</tr>\n";
}
echo "</table>\n";

?>
Related