2

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">test@test.com</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?

spike5792
  • 75
  • 2
  • 2
  • 5

4 Answers4

4

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 = "youremail@mail.com";
        $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', 'lol@lol.dk','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.

Bolli
  • 4,974
  • 6
  • 31
  • 47
  • 1
    And now I know this is a bad idea. Don't request authtoken on every request. Instead hardcode the key in there or even better, create a method that requests a new key, if your current key is not working. – Bolli Mar 28 '13 at 14:30
  • There's now a [newer way](https://stackoverflow.com/q/61961302/105539) to handle this in 2020. – Volomike Jun 03 '20 at 13:19
3
   <?php
$xml = 
        '<?xml version="1.0" encoding="UTF-8"?>
        <Contacts>
        <row no="1">
        <FL val="First Name">Digant</FL>
        <FL val="Last Name">Shah1</FL>
        <FL val="Email">digant.shah91@gmail.com</FL>
        <FL val="Department">php</FL>
        <FL val="Phone">999999999</FL>
        <FL val="Fax">99999999</FL>
        <FL val="Mobile">99989989</FL>
        <FL val="Assistant">none</FL>
        </row>
        </Contacts>';
$auth="*******************";
    $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;




?>
Digant Shah
  • 163
  • 1
  • 1
  • 8
1

If you have troubles with SSL connection try to add next curl option:

curl_setopt($ch, CURLOPT_SSL_CIPHER_LIST, 'rsa_rc4_128_sha');
Alex Koloskov
  • 231
  • 3
  • 5
0
`$ch = curl_init('https://crm.zoho.com/crm/private/xml/Contacts/insertRecords?');
curl_setopt($ch, CURLOPT_VERBOSE, 1);//standard i/o streams 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//Set to return data to string ($response) 
curl_setopt($ch, CURLOPT_POST, 1);//Regular post 
Stephan
  • 41,764
  • 65
  • 238
  • 329
  • $authtoken = "7d3e4882977bb4ec00b457cff3292196"; $xmlData=' Scott James test@test.com CG 999999999 99999999 99989989 John ';$query = "newFormat=1&authtoken={$authtoken}&scope=crmapi&xmlData={$xmlData}"; curl_setopt($ch, CURLOPT_POSTFIELDS, $query); $response = curl_exec($ch); echo $response; curl_close($ch); – Padma1525978 Fiori Jul 18 '12 at 05:31