Natively Connect to Salesforce Data 360 Data in PHP
Drop the CData ODBC Driver for Salesforce Data 360 into your LAMP or WAMP stack to build Salesforce Data 360-connected Web applications. This article shows how to use PHP's ODBC built-in functions to connect to Salesforce Data 360 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.
Salesforce Data 360 supports authentication via the OAuth standard.
OAuth
Set AuthScheme to OAuth.
Desktop Applications
CData provides an embedded OAuth application that simplifies authentication at the desktop.
You can also authenticate from the desktop via a custom OAuth application, which you configure and register at the Salesforce Data 360 console. For further information, see Creating a Custom OAuth App in the Help documentation.
Before you connect, set these properties:
- InitiateOAuth: GETANDREFRESH. You can use InitiateOAuth to avoid repeating the OAuth exchange and manually setting the OAuthAccessToken.
- OAuthClientId (custom applications only): The Client ID assigned when you registered your custom OAuth application.
- OAuthClientSecret (custom applications only): The Client Secret assigned when you registered your custom OAuth application.
When you connect, the driver opens Salesforce Data 360's OAuth endpoint in your default browser. Log in and grant permissions to the application.
The driver then completes the OAuth process as follows:
- Extracts the access token from the callback URL.
- Obtains a new access token when the old one expires.
- Saves OAuth values in OAuthSettingsLocation so that they persist across connections.
For other OAuth methods, including Web Applications and Headless Machines, refer to the Help documentation.
Establish a Connection
Open the connection to Salesforce Data 360 by calling the odbc_connect or odbc_pconnect methods. To close connections, use odbc_close or odbc_close_all.
$conn = odbc_connect("CData ODBC SalesforceDataCloud 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 SalesforceDataCloud 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 Account WHERE EmployeeCount = ?");
Execute Queries
Execute prepared statements with odbc_execute.
$conn = odbc_connect("CData ODBC SalesforceDataCloud Source","user","password");
$query = odbc_prepare($conn, "SELECT * FROM Account WHERE EmployeeCount = ?");
$success = odbc_execute($query, array('250'));
Execute nonparameterized queries with odbc_exec.
$conn = odbc_connect("CData ODBC SalesforceDataCloud Source","user","password");
$query = odbc_exec($conn, "SELECT [Account ID], [Account Name] FROM Account WHERE EmployeeCount > 250");
Process Results
Access a row in the result set as an array with the odbc_fetch_array function.
$conn = odbc_connect("CData ODBC Salesforce Data 360 data Source","user","password");
$query = odbc_exec($conn, "SELECT [Account ID], [Account Name] FROM Account WHERE EmployeeCount > 250");
while($row = odbc_fetch_array($query)){
echo $row["[Account ID]"] . "\n";
}
Display the result set in an HTML table with the odbc_result_all function.
$conn = odbc_connect("CData ODBC Salesforce Data 360 data Source","user","password");
$query = odbc_prepare($conn, "SELECT * FROM Account WHERE EmployeeCount = ?");
$success = odbc_execute($query, array('250'));
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 Salesforce Data 360-specific adaptations of the PHP community documentation for all ODBC functions.