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

Use the Salesforce ODBC driver to connect PHP to Salesforce.

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

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

    For installation instructions, refer to the Salesforce ODBC driver documentation. Refer to the documentation to find out 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 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 data. For example:
    select count(*) from Product2
    

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

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

<?php

    /* ODBC connection details. Replace these values with your DSN and Salesforce 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 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