Using PHP to insert records to Zoho CRM via the API

spike5792 picture spike5792 · Mar 31, 2012 · Viewed 12.9k times · Source

I thought using get_file_contents function would allow me to execute the API as I would with other API's I've used in the past. However this approach doesn't work with the Zoho CRM API - possibly because I'm passing XML data rather than it being a RESTful query?

API doc is at http://zohocrmapi.wiki.zoho.com/insertRecords-Method.html

When passing this through the web browser address bar it works:

https://crm.zoho.com/crm/private/xml/Contacts/insertRecords?authtoken=Auth Token&scope=crmapi
&newFormat=1
&xmlData=
<Contacts>
<row no="1">
<FL val="First Name">Scott</FL>
<FL val="Last Name">James</FL>
<FL val="Email">[email protected]</FL>
<FL val="Department">CG</FL>
<FL val="Phone">999999999</FL>
<FL val="Fax">99999999</FL>
<FL val="Mobile">99989989</FL>
<FL val="Assistant">John</FL>
</row>
</Contacts>

I don't get any errors when running this using file_get_contents. Does anyone know what I need to do to get this to work?

Answer

Bolli picture Bolli · Mar 19, 2013

This is working for me. I wrote 2 methods. One for getting auth key and one for creating a contact.

<?php 
class zoho{

    public function getAuth()
    {
        $username = "[email protected]";
        $password = "yourpassword";
        $param = "SCOPE=ZohoCRM/crmapi&EMAIL_ID=".$username."&PASSWORD=".$password;
        $ch = curl_init("https://accounts.zoho.com/apiauthtoken/nb/create");
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
        $result = curl_exec($ch);
        /*This part of the code below will separate the Authtoken from the result.
        Remove this part if you just need only the result*/
        $anArray = explode("\n",$result);
        $authToken = explode("=",$anArray['2']);
        $cmp = strcmp($authToken['0'],"AUTHTOKEN");
        echo $anArray['2'].""; if ($cmp == 0)
        {
        echo "Created Authtoken is : ".$authToken['1'];
        return $authToken['1'];
        }
        curl_close($ch);
    }   



public function postData($auth, $fornavn,$efternavn, $email,$addr,$by,$postnr,$land,$kommentar)
    {
        $xml = 
        '<?xml version="1.0" encoding="UTF-8"?>
        <Contacts>
        <row no="1">
        <FL val="First Name">'.$fornavn.'</FL>
        <FL val="Last Name">'.$efternavn.'</FL>
        <FL val="Email">'.$email.'</FL>
        <FL val="Department">'.$land.'</FL>
        <FL val="Phone">999999999</FL>
        <FL val="Fax">99999999</FL>
        <FL val="Mobile">99989989</FL>
        <FL val="Assistant">none</FL>
        </row>
        </Contacts>';

    $url ="https://crm.zoho.com/crm/private/xml/Contacts/insertRecords";
    $query="authtoken=".$auth."&scope=crmapi&newFormat=1&xmlData=".$xml;
    $ch = curl_init();
    /* set url to send post request */
    curl_setopt($ch, CURLOPT_URL, $url);
    /* allow redirects */
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    /* return a response into a variable */
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    /* times out after 30s */
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    /* set POST method */
    curl_setopt($ch, CURLOPT_POST, 1);
    /* add POST fields parameters */
    curl_setopt($ch, CURLOPT_POSTFIELDS, $query);// Set the request as a POST FIELD for curl.

    //Execute cUrl session
    $response = curl_exec($ch);
    curl_close($ch);
    echo $response;

    }
}

?>

Use like this:

<?php 
    include('zoho.php');
    $zoho = new zoho();
    echo "testing....<br />";
    $auth = $zoho->getAuth();
    echo " <pre>";
    echo $auth;
    $result = $zoho->postData($auth, 'Bob','test', '[email protected]','adresse','by','postr','Danmark','Some comment');
    print_r($result);
 ?>

Im still working on what kind of XML you need to insert address info on the contact.