Natively Connect to Linear Data in PHP
Drop the CData ODBC Driver for Linear into your LAMP or WAMP stack to build Linear-connected Web applications. This article shows how to use PHP's ODBC built-in functions to connect to Linear 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.
You can authenticate to Linear with a personal API key or with OAuth 2.0. The API key is the simplest option for connecting with your own Linear account.
Authenticating with an API Key
Set the following connection properties:
- AuthScheme: Set this to APIKey.
- APIKey: A Linear personal API key.
To create a personal API key, log in to Linear, open Settings > Security & access > Personal API keys, select New API key, and create it. Copy the key immediately, because Linear shows it only once.
Authenticating with OAuth
OAuth requires a custom OAuth application registered in Linear (Settings > API > OAuth applications), which provides the OAuthClientId and OAuthClientSecret. Two flows are supported:
- Authorization code: Set AuthScheme to OAuth, InitiateOAuth to GETANDREFRESH, and provide OAuthClientId, OAuthClientSecret, and the CallbackURL defined in your application (e.g., http://localhost:33333). The driver opens Linear in your browser so you can grant access.
- Client credentials: Set AuthScheme to OAuthClient and provide OAuthClientId and OAuthClientSecret. This authenticates the application itself, with no browser interaction, and suits machine-to-machine integrations.
By default, the driver requests the read,write scopes. The driver refreshes the access token automatically when it expires.
Establish a Connection
Open the connection to Linear by calling the odbc_connect or odbc_pconnect methods. To close connections, use odbc_close or odbc_close_all.
$conn = odbc_connect("CData ODBC Linear 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 Linear 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 Team WHERE key = ?");
Execute Queries
Execute prepared statements with odbc_execute.
$conn = odbc_connect("CData ODBC Linear Source","user","password");
$query = odbc_prepare($conn, "SELECT * FROM Team WHERE key = ?");
$success = odbc_execute($query, array('ENG'));
Execute nonparameterized queries with odbc_exec.
$conn = odbc_connect("CData ODBC Linear Source","user","password");
$query = odbc_exec($conn, "SELECT id, name FROM Team WHERE key = 'ENG'");
Process Results
Access a row in the result set as an array with the odbc_fetch_array function.
$conn = odbc_connect("CData ODBC Linear data Source","user","password");
$query = odbc_exec($conn, "SELECT id, name FROM Team WHERE key = 'ENG'");
while($row = odbc_fetch_array($query)){
echo $row["id"] . "\n";
}
Display the result set in an HTML table with the odbc_result_all function.
$conn = odbc_connect("CData ODBC Linear data Source","user","password");
$query = odbc_prepare($conn, "SELECT * FROM Team WHERE key = ?");
$success = odbc_execute($query, array('ENG'));
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 Linear-specific adaptations of the PHP community documentation for all ODBC functions.