Natively Connect to USPS Data in PHP

The CData ODBC driver for USPS enables you to create PHP applications with connectivity to USPS data. Leverage the native support for ODBC in PHP.

Drop the CData ODBC Driver for USPS into your LAMP or WAMP stack to build USPS-connected Web applications. This article shows how to use PHP's ODBC built-in functions to connect to USPS 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.

To authenticate with USPS, set the following connection properties.

  • PostageProvider: The postage provider to use to process requests. Available options are ENDICIA and STAMPS. If unspecified, this property will default to ENDICIA.
  • UseSandbox: This controls whether live or test requests are sent to the production or sandbox servers. If set to true, the Password, AccountNumber, and StampsUserId properties are ignored.
  • StampsUserId: This value is used for logging into authentication to the Stamps servers. This value is not applicable for Endicia and is optional if UseSandbox is true.
  • Password: This value is used for logging into Endicia and Stamps servers. If the postage provider is Endicia, this will be the pass phrase associated with your postage account. It is optional if UseSandbox is true.
  • AccountNumber: The shipper's account number. It is optional if UseSandbox is true.
  • PrintLabelLocation: This property is required to use the GenerateLabels or GenerateReturnLabels stored procedures. This should be set to the folder location where generated labels should be stored.

The Cache Database

Many of the useful task available from USPS require a lot of data. To ensure this data is easy to input and recall later, utilize a cache database to make requests. Set the cache connection properties in order to use the cache:

  • CacheLocation: The path to the cache location, for which a connection will be configured with the default cache provider. For example, C:\users\username\documents\uspscache

As an alternative to CacheLocation, set the combination of CacheConnection and CacheProvider to configure a cache connection using a provider separate from the default.

Establish a Connection

Open the connection to USPS by calling the odbc_connect or odbc_pconnect methods. To close connections, use odbc_close or odbc_close_all.

$conn = odbc_connect("CData ODBC USPS 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 USPS 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 Senders WHERE SenderID = ?");

Execute Queries

Execute prepared statements with odbc_execute.

$conn = odbc_connect("CData ODBC USPS Source","user","password"); $query = odbc_prepare($conn, "SELECT * FROM Senders WHERE SenderID = ?"); $success = odbc_execute($query, array('25'));

Execute nonparameterized queries with odbc_exec.

$conn = odbc_connect("CData ODBC USPS Source","user","password"); $query = odbc_exec($conn, "SELECT FirstName, Phone FROM Senders WHERE SenderID = '25'");

Process Results

Access a row in the result set as an array with the odbc_fetch_array function.

$conn = odbc_connect("CData ODBC USPS data Source","user","password"); $query = odbc_exec($conn, "SELECT FirstName, Phone FROM Senders WHERE SenderID = '25'"); while($row = odbc_fetch_array($query)){ echo $row["FirstName"] . "\n"; }

Display the result set in an HTML table with the odbc_result_all function.

$conn = odbc_connect("CData ODBC USPS data Source","user","password"); $query = odbc_prepare($conn, "SELECT * FROM Senders WHERE SenderID = ?"); $success = odbc_execute($query, array('25')); 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 USPS-specific adaptations of the PHP community documentation for all ODBC functions.