Easysoft ODBC-Salesforce Driver

How do I connect PHP to Salesforce.com on Linux or UNIX?

Article:
01064
Last Reviewed:
28th January 2014
Revision:
1

Use the Salesforce.com ODBC Driver to connect PHP to Salesforce.com.

The following steps show how to access Salesforce.com from a PHP script and insert a new product into the Salesforce.com Product2 object.

  1. If you have not already done so, install PHP, PHP-ODBC and PHP-CLI (Command Line Interface) on your Linux / UNIX machine
  2. Install and license the Salesforce.com ODBC driver on the machine where PHP is installed.

    For installation instructions, see the Salesforce.com ODBC driver documentation. Refer to the documentation to see which environment variables you need to set (LD_LIBRARY_PATH, LIBPATH, LD_RUN_PATH or SHLIB_PATH depending on the platform and linker).

  3. Create an ODBC data source in /etc/odbc.ini that connects to your Salesforce.com organisation. For example:
    [SALESFORCE-PHP]
    Description=Easysoft ODBC-SalesForce Driver
    Driver=Easysoft ODBC-SalesForce
    uri=https://login.salesforce.com/services/Soap/u/27
    user=myuser@mydomain.com
    password=p455w0rd
    token=aBCdEfG1hij2kLmNoPq3R4STu
  4. Use isql.sh to test the new data source. For example:
    $ cd /usr/local/easysoft/unixODBC/bin
    $ ./isql.sh -v SALESFORCE-PHP
  5. If you connection succeeds, try retrieving some Salesforce.com data. For example:
    select count(*) from Product2
    

    If you are unable to connect, refer to this article for assistance.

The following example PHP script inserts a product into the Salesforce.com Product2 object.

<?php

    /* ODBC connection details. Replace these values with your DSN and Salesforce.com username and password */
    $ODBC_Connection='SALESFORCE-PHP';
    $ODBC_User='myuser@mydomain.com';
    $ODBC_Pass='p455w0rd';

    /* Product details */
    $Name='Easysoft Test Product';
    $ProductCode='ETP001';
    $Description='12345-67890';

    /* Connect to the ODBC data source */
    $conn=odbc_connect($ODBC_Connection,$ODBC_User,$ODBC_Pass);
    if (!$conn) exit("Connection Failed:" . odbc_errormsg() );

    /* Insert the product details */
    $sql="insert into Product2 ( Name, ProductCode, Description ) "
        . "values ('".$Name."', '".$ProductCode."', '".$Description."')";

    $rs=odbc_exec($conn,$sql);
    if (!$rs) exit("Insert Failed:" . odbc_errormsg() );

    /* Get the Product2 Id from Salesforce.com to show that the insert has worked */
    $sql="select Id from Product2 where Name='".$Name."'"
    . "and ProductCode='".$ProductCode."' and Description "
    . "like '".$Description."'";
    $rs=odbc_exec($conn,$sql);
    if (!$rs) exit("Select Failed:" . odbc_errormsg() );
    odbc_fetch_row($rs);
    $col1=odbc_result($rs, "Id");
    echo $Name." stored in Salesforce with an Id of ".$col1."\n";

?>

To insert the new product into the Product2 object, save the PHP program to a file and then run it. For example:

$ php salesforce-product2.php
Applies To

Knowledge Base Feedback

* Did this content help you?
* Please select one option based on your first choice:

(* Required Fields)