Shri
Shri

Reputation: 752

How to use Zoho CRM API V2 from PHP page?

I was trying to store and retrieve some values on Zoho CRM and ended up using V1 of the API.

My Current Code is:

<html>
<head>  
          <meta content="width=320px, initial-scale=1, user-scalable=yes" name="viewport" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
</head>

<body>
<div id="logform">
      <form method="POST" onClick="exec()">
<b>Login To Continue:</b><br/><br/>
 <b>Name:</b><input type="text" id="name" name='name' value=""><br/>
 <b>Email:</b><input type="email" id="email" name='email' value="" ><br/>
 <input type="submit" name="submit"value="Submit" id="submit" >Submit</button>
 <!--<font color="white"><input type="button" ></a>-->
</form></div>

<script type="text/javascript">
        <script type="text/javascript">
        function exec() { 
            var mail=document.getElementById("email").innerHTML;
            var namen=document.getElementById("name");
            document.cookie = "name="+namen;
            var phpadd= <?php echo sendData();?> ;
        }
        </script>

        <script type="text/javascript">document.getElementById("submit").addEventListener("click", var hide = document.getElementById("logform").style.display="none";);
        </script>
</script>

      <?php 
function sendData()
{
        $email=$_POST["email"]; 
        $name=$_POST["name"]; 
        $cookie_name = "Event";
        $cookie_value = "Login";

setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
        $fname="Usr";
        $lname="1";
        $auth="#AuthKey";
        $xml = 
        '<?xml version="1.0" encoding="UTF-8"?>
        <Contacts>
        <row no="1">
        <FL val="First Name">'.$name.'</FL>
        <FL val="Last Name">'.$name.'</FL>
        <FL val="Email">'.$email.'</FL>
        <FL val="Department">Medical</FL>
        <FL val="Phone">0000000000</FL>
        <FL val="Fax">0000</FL>
        <FL val="Mobile">0000000000</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;

}
?>

</body>

</html>

Now I came to know that API v1 is getting deprecated. I need to migrate this piece of code to CRM API V2. I also need to read these data uploaded to CRM. Someone please help me! The online documentations are very confusing.

Upvotes: 1

Views: 2724

Answers (1)

Igor Bykov
Igor Bykov

Reputation: 2812

You are right, V1 will be depreciated in 30 days.

The easiest way to switch is probably using Zoho PHP SDK. It might be installed as composer package.

SDK has quite a complex, mostly not-well-documented API, but, following examples in documentation, you should be able to figure out how you can use it.

You most probably want to use "Records API".

If you have never worked with oAuth2 yet, the basic idea of this authentication system is that instead of having something like "secret key" (which is essentially a password) you interchange tokens with service provider (Zoho).

Tokens are like passwords that expire quite quickly but you can renew them anytime you want.

Normally, you will want to completely automate & abstract out all token management so, it will run kind of separately from the main business your code does.

Luckily, Zoho PHP SDK already do it for you & manage tokens automatically.

Upvotes: 1

Related Questions