05 July 2014

Sample Code to Perform Insert, Update and Read Operation on Lead Module of Zoho through Zoho API

Before using, sign up for Zoho CRM Zoho Sign UP and set your zoho credentials
<?php

 $username = "";
 $password = "";
 $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_SSL_VERIFYPEER, false);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
 $result = curl_exec($ch);
 
 $anArray = explode("\n",$result);
 $authToken = explode("=",$anArray['2']);

 curl_close($ch);
 
 $param = "authtoken=".$authToken['1']."&scope=crmapi&newFormat=1&xmlData=";
 
 $param .= '<?xml version="1.0" encoding="UTF-8"?><Leads><row no="1"><FL val="Lead Source">Web Download</FL><FL val="Company">Your Company</FL><FL val="First Name">Hannah</FL><FL val="Last Name">Smith</FL><FL val="Email">testing@testing.com</FL><FL val="Title">Manager</FL><FL val="Phone">1234567890</FL><FL val="Home Phone">0987654321</FL><FL val="Other Phone">1212211212</FL><FL val="Fax">02927272626</FL><FL val="Mobile">292827622</FL></row></Leads>';
 
 $ch = curl_init("https://crm.zoho.com/crm/private/json/Leads/insertRecords");
 curl_setopt($ch, CURLOPT_POST, true);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
 $result = curl_exec($ch);
 
 $result = json_decode($result, true);
 
 $recordId = $result['response']['result']['recorddetail']['FL'][0]['content'];
 
 $param = "authtoken=".$authToken['1']."&scope=crmapi&id=$recordId&xmlData=";
 
 $param .= '<?xml version="1.0" encoding="UTF-8"?><Leads><row no="1"><FL val="Lead Source">Web Download</FL><FL val="Company">Your Company</FL><FL val="First Name">Hannah</FL><FL val="Last Name">Smith</FL><FL val="Email">manish@devitechnosolutions.com</FL><FL val="Title">Manager</FL><FL val="Phone">1234567890</FL><FL val="Home Phone">0987654321</FL><FL val="Other Phone">1212211212</FL><FL val="Fax">02927272626</FL><FL val="Mobile">292827622</FL></row></Leads>';
 
 $ch = curl_init("https://crm.zoho.com/crm/private/json/Leads/updateRecords");
 curl_setopt($ch, CURLOPT_POST, true);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
 $result = curl_exec($ch);
 
 $param = "newFormat=1&authtoken=".$authToken['1']."&scope=crmapi";
 
 $ch = curl_init("https://crm.zoho.com/crm/private/json/Leads/getRecords");
 curl_setopt($ch, CURLOPT_POST, true);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
 $result = curl_exec($ch);
 
 print_r($result);

 
?>


No comments:

Post a Comment