Discover how a bimodal integration strategy can address the major data management challenges facing your organization today.
Get the Report →Natively Connect to IBM Cloud Object Storage Data in PHP
The CData ODBC driver for IBM Cloud Object Storage enables you to create PHP applications with connectivity to IBM Cloud Object Storage data. Leverage the native support for ODBC in PHP.
Drop the CData ODBC Driver for IBM Cloud Object Storage into your LAMP or WAMP stack to build IBM Cloud Object Storage-connected Web applications. This article shows how to use PHP's ODBC built-in functions to connect to IBM Cloud Object Storage data, execute queries, and output the results.
Configure a DSN
If you have not already, first specify connection properties in an ODBC DSN (data source name). This is the last step of the driver installation. You can use the Microsoft ODBC Data Source Administrator to create and configure ODBC DSNs.
Register a New Instance of Cloud Object Storage
If you do not already have Cloud Object Storage in your IBM Cloud account, follow the procedure below to install an instance of SQL Query in your account:
- Log in to your IBM Cloud account.
- Navigate to the page, choose a name for your instance and click Create. You will be redirected to the instance of Cloud Object Storage you just created.
Connecting using OAuth Authentication
There are certain connection properties you need to set before you can connect. You can obtain these as follows:
API Key
To connect with IBM Cloud Object Storage, you need an API Key. You can obtain this as follows:
- Log in to your IBM Cloud account.
- Navigate to the Platform API Keys page.
- On the middle-right corner click "Create an IBM Cloud API Key" to create a new API Key.
- In the pop-up window, specify the API Key name and click "Create". Note the API Key as you can never access it again from the dashboard.
Cloud Object Storage CRN
If you have multiple accounts, you will need to specify the CloudObjectStorageCRN explicitly. To find the appropriate value, you can:
- Query the Services view. This will list your IBM Cloud Object Storage instances along with the CRN for each.
- Locate the CRN directly in IBM Cloud. To do so, navigate to your IBM Cloud Dashboard. In the Resource List, Under Storage, select your Cloud Object Storage resource to get its CRN.
Connecting to Data
You can now set the following to connect to data:
- InitiateOAuth: Set this to GETANDREFRESH. You can use InitiateOAuth to avoid repeating the OAuth exchange and manually setting the OAuthAccessToken.
- ApiKey: Set this to your API key which was noted during setup.
- CloudObjectStorageCRN (Optional): Set this to the cloud object storage CRN you want to work with. While the connector attempts to retrieve this automatically, specifying this explicitly is recommended if you have more than Cloud Object Storage account.
When you connect, the connector completes the OAuth process.
- Extracts the access token and authenticates requests.
- Saves OAuth values in OAuthSettingsLocation to be persisted across connections.
Establish a Connection
Open the connection to IBM Cloud Object Storage by calling the odbc_connect or odbc_pconnect methods. To close connections, use odbc_close or odbc_close_all.
$conn = odbc_connect("CData ODBC IBMCloudObjectStorage Source","user","password");
Connections opened with odbc_connect are closed when the script ends. Connections opened with the odbc_pconnect method are still open after the script ends. This enables other scripts to share that connection when they connect with the same credentials. By sharing connections among your scripts, you can save system resources, and queries execute faster.
$conn = odbc_pconnect("CData ODBC IBMCloudObjectStorage Source","user","password");
...
odbc_close($conn); //persistent connection must be closed explicitly
Create Prepared Statements
Create prepared statements and parameterized queries with the odbc_prepare function.
$query = odbc_prepare($conn, "SELECT * FROM Objects WHERE Bucket = ?");
Execute Queries
Execute prepared statements with odbc_execute.
$conn = odbc_connect("CData ODBC IBMCloudObjectStorage Source","user","password");
$query = odbc_prepare($conn, "SELECT * FROM Objects WHERE Bucket = ?");
$success = odbc_execute($query, array('someBucket'));
Execute nonparameterized queries with odbc_exec.
$conn = odbc_connect("CData ODBC IBMCloudObjectStorage Source","user","password");
$query = odbc_exec($conn, "SELECT Key, Etag FROM Objects WHERE Bucket = 'someBucket'");
Process Results
Access a row in the result set as an array with the odbc_fetch_array function.
$conn = odbc_connect("CData ODBC IBM Cloud Object Storage data Source","user","password");
$query = odbc_exec($conn, "SELECT Key, Etag FROM Objects WHERE Bucket = 'someBucket'");
while($row = odbc_fetch_array($query)){
echo $row["Key"] . "\n";
}
Display the result set in an HTML table with the odbc_result_all function.
$conn = odbc_connect("CData ODBC IBM Cloud Object Storage data Source","user","password");
$query = odbc_prepare($conn, "SELECT * FROM Objects WHERE Bucket = ?");
$success = odbc_execute($query, array('someBucket'));
if($success)
odbc_result_all($query);
More Example Queries
You will find complete information on the driver's supported SQL in the help documentation. The code examples above are IBM Cloud Object Storage-specific adaptations of the PHP community documentation for all ODBC functions.