Natively Connect to Sage X3 Cloud Data in PHP
Drop the CData ODBC Driver for Sage X3 Cloud into your LAMP or WAMP stack to build Sage X3 Cloud-connected Web applications. This article shows how to use PHP's ODBC built-in functions to connect to Sage X3 Cloud 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.
Sage X3 Cloud uses the OAuth 2.0 Client Credentials flow, and an X-API-Key is also required for API access. Set AuthScheme to OAuth and specify the following connection properties:
- URL: The base URL of your Sage X3 Cloud instance.
- OAuthAccessTokenUrl: The OAuth token endpoint (e.g., https://your-auth-domain/oauth/token).
- OAuthClientId: Your OAuth application client ID.
- OAuthClientSecret: Your OAuth application client secret.
- Audience: The API audience value for the token request.
- XAPIKey: The X-API-Key provided by your Sage X3 Cloud administrator.
- Folder: The Sage X3 folder name (e.g., SEED). This folder is used as the default schema.
- Folders (optional): A comma-separated list of Sage X3 folders (e.g., SEED,PERF). Each folder is exposed as a separate schema, so you can query across folders with the Schema.Table syntax.
The driver obtains an access token with the Client Credentials flow and sends it with the X-API-Key on every API request. With InitiateOAuth set to GETANDREFRESH (the default), the driver acquires and refreshes the token automatically.
Establish a Connection
Open the connection to Sage X3 Cloud by calling the odbc_connect or odbc_pconnect methods. To close connections, use odbc_close or odbc_close_all.
$conn = odbc_connect("CData ODBC SageX3Cloud 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 SageX3Cloud 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 BPCUSTOMER WHERE BPCNUM = ?");
Execute Queries
Execute prepared statements with odbc_execute.
$conn = odbc_connect("CData ODBC SageX3Cloud Source","user","password");
$query = odbc_prepare($conn, "SELECT * FROM BPCUSTOMER WHERE BPCNUM = ?");
$success = odbc_execute($query, array('MARTIN'));
Execute nonparameterized queries with odbc_exec.
$conn = odbc_connect("CData ODBC SageX3Cloud Source","user","password");
$query = odbc_exec($conn, "SELECT BPCNUM, BPCNAM FROM BPCUSTOMER WHERE BPCNUM = 'MARTIN'");
Process Results
Access a row in the result set as an array with the odbc_fetch_array function.
$conn = odbc_connect("CData ODBC Sage X3 Cloud data Source","user","password");
$query = odbc_exec($conn, "SELECT BPCNUM, BPCNAM FROM BPCUSTOMER WHERE BPCNUM = 'MARTIN'");
while($row = odbc_fetch_array($query)){
echo $row["BPCNUM"] . "\n";
}
Display the result set in an HTML table with the odbc_result_all function.
$conn = odbc_connect("CData ODBC Sage X3 Cloud data Source","user","password");
$query = odbc_prepare($conn, "SELECT * FROM BPCUSTOMER WHERE BPCNUM = ?");
$success = odbc_execute($query, array('MARTIN'));
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 Sage X3 Cloud-specific adaptations of the PHP community documentation for all ODBC functions.