import java.io.*;
import java.util.*;
import java.net.*;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.PartSource;
import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource;
public class TestAPI
{
public static void main(String a[])
{
try
{
String authtoken = "YOUR AUTH TOKEN";
String targetURL = "https://crm.zoho.com/crm/private/xml/Leads/getRecords";
String paramname = "content";
PostMethod post = new PostMethod(targetURL);
post.setParameter("authtoken",authtoken);
post.setParameter("scope","crmapi");
HttpClient httpclient = new HttpClient();
PrintWriter myout = null;
/*-------------------------------------- Execute the http request--------------------------------*/
try
{
long t1 = System.currentTimeMillis();
int result = httpclient.executeMethod(post);
System.out.println("HTTP Response status code: " + result);
System.out.println(">> Time taken " + (System.currentTimeMillis() - t1));
/*-------------------------------------- Execute the http request--------------------------------*/
/* ---------------------------writing the response to a file--------------------*/
myout = new PrintWriter(new File("response.xml"));
myout.print(post.getResponseBodyAsString());
/* ---------------------------writing the response to a file--------------------*/
/*-----------------------Get response as a string ----------------*/
String postResp = post.getResponseBodyAsString();
System.out.println("postResp=======>"+postResp);
/* ---------------------Get response as a string ----------------------------*/
if(postResp.equals("Invalid Ticket Id"))
{
// generate new auth token and call the API
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
myout.close();
post.releaseConnection();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
<?php
/* NOTE: Define your mysql database parameters in moduleDependant class */
/* Constant Declarations */
define("TARGETURL", "https://crm.zoho.com/crm/private/xml/Leads/getMyRecords");
/* user related parameter */
define("AUTHTOKEN", "YOUR AUTH TOKEN");
define("SCOPE", "crmapi");
/* create a object */
$utilObj = new Utilities();
/* set parameters */
$parameter = "";
$parameter = $utilObj->setParameter("scope", SCOPE, $parameter);
$parameter = $utilObj->setParameter("authtoken", AUTHTOKEN, $parameter);
$parameter = $utilObj->setParameter("selectColumns", "Leads(LEADID,First Name,Last Name,Company)", $parameter);
/* Call API */
$response = $utilObj->sendCurlRequest(TARGETURL, $parameter);
$utilObj->parseXMLandInsertInDB($response);
class Utilities {
public function setParameter($key, $value, $parameter) {
if ($parameter === "" || strlen($parameter) == 0) {
$parameter = $key . '=' . $value;
} else {
$parameter .= '&' . $key . '=' . $value;
}
return $parameter;
}
public function parseXMLandInsertInDB($xmldata) {
$xmlString = <<<XML
$xmldata
XML;
$xml = simplexml_load_string($xmlString);
if (isset($xml->result)) {
$modeuleDependantObj = new moduleDependant();
$output = $modeuleDependantObj->insertInDB($xml);
} else if (isset($xml->error)) {
echo "Error code: " . $xml->error->code . "<br/>";
echo "Error message: " . $xml->error->message;
}
}
public function sendCurlRequest($url, $parameter) {
try {
/* initialize curl handle */
$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, $parameter);
/* execute the cURL */
$result = curl_exec($ch);
curl_close($ch);
return $result;
} catch (Exception $exception) {
echo 'Exception Message: ' . $exception->getMessage() . '<br/>';
echo 'Exception Trace: ' . $exception->getTraceAsString();
}
}
}
class moduleDependant {
/* Define your mysql database parameters */
private $host = "hostname";
private $username = "mysql_username";
private $password = "mysql_password";
public function insertInDB($xml) {
$numberOfRecords = count($xml->result->Leads->row);
/* $records[row value][field value] */
$records[][] = array();
for ($i = 0; $i < $numberOfRecords; $i++) {
$numberOfValues = count($xml->result->Leads->row[$i]->FL);
for ($j = 0; $j < $numberOfValues; $j++) {
switch ((string) $xml->result->Leads->row[$i]->FL[$j]['val']) {
/* Get attributes as element indices */
case 'LEADID':
$records[$i]['LEADID'] = (string) $xml->result->Leads->row[$i]->FL[$j];
break;
case 'First Name':
$records[$i]['First Name'] = (string) $xml->result->Leads->row[$i]->FL[$j];
break;
case 'Last Name':
$records[$i]['Last Name'] = (string) $xml->result->Leads->row[$i]->FL[$j];
break;
case 'Company':
$records[$i]['Company'] = (string) $xml->result->Leads->row[$i]->FL[$j];
break;
}
}
}
/* Inserting in database */
$connection = mysql_connect($this->host, $this->username, $this->password) or die(mysql_error());
$query = "Insert into crm.leads (id, firstname, lastname, company) value ";
for ($k = 0; $k < count($records); $k++) {
if ($k == 0) {
$query .= "('" . $records[$k]['LEADID'] . "','" . $records[$k]['First Name'] . "','" . $records[$k]['Last Name'] . "','" . $records[$k]['Company'] . "')";
}
if ($k > 0) {
$query .= ", ('" . $records[$k]['LEADID'] . "','" . $records[$k]['First Name'] . "','" . $records[$k]['Last Name'] . "','" . $records[$k]['Company'] . "')";
}
}
$result = mysql_query($query, $connection) or die(mysql_error());
if (isset($result) && mysql_affected_rows($connection) == count($records)) {
echo "Data's are inserted in database successfully.<br/><br/> ";
/* Table structure of inserted data, you can check inserted data by uncommenting a below section */
/*
$insertedTable = "<table cellspacing=0 cellpadding='4px' border=1>";
$insertedTable .= "<tr><td>Lead ID</td><td>First Name</td><td>Last Name</td><td>Company</td></tr>";
for ($k = 0; $k < count($records); $k++) {
$insertedTable .= "<tr><td>" . $records[$k]['LEADID'] . "</td><td>" . $records[$k]['First Name'] . "</td><td>" . $records[$k]['Last Name'] . "</td><td>" . $records[$k]['Company'] . "</td></tr>";
}
$insertedTable .= "</table>";
echo $insertedTable;
*/
} else {
echo 'Some error while inserting in database';
}
}
}
import urllib
import urllib2
module_name = 'Leads'
authtoken = 'Your authtoken'
params = {'authtoken':authtoken,'scope':'crmapi'}
final_URL = "https://crm.zoho.com/crm/private/xml/"+module_name+"/getRecords"
data = urllib.urlencode(params)
request = urllib2.Request(final_URL,data)
response = urllib2.urlopen(request)
xml_response = response.read()
print xml_response
using System;
using System.Net;
using System.IO;
using System.Web;
using System.Text;
using System.Net.Security;
public class ZohoCRMAPI
{
public static string zohocrmurl = "https://crm.zoho.com/crm/private/xml/";
public static void Main (string[] args)
{
string result = APIMethod("Leads","getRecords","508020000000332001");//Change the id,method name, and module name here
Console.Write(result);
}
public static String APIMethod(string modulename,string methodname,string recordId)
{
string uri = zohocrmurl + modulename + "/"+methodname+"?";
/* Append your parameters here */
string postContent = "scope=crmapi";
postContent = postContent + "&authtoken=0ac32dc177c4918eca902fd290a92f4a";//Give your authtoken
if (methodname.Equals("insertRecords") || methodname.Equals("updateRecords"))
{
postContent = postContent + "&xmlData="+ HttpUtility.UrlEncode("Your CompanyHannahSmithtesting@testing.com");
}
if (methodname.Equals("updateRecords") || methodname.Equals("deleteRecords") || methodname.Equals("getRecordById"))
{
postContent = postContent + "&id="+recordId;
}
string result = AccessCRM(uri, postContent);
return result;
}
public static string AccessCRM(string url, string postcontent)
{
WebRequest request = WebRequest.Create(url);
request.Method = "POST";
byte[] byteArray = Encoding.UTF8.GetBytes(postcontent);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
return responseFromServer;
}
}
API Method: insertRecords
XML Format:
https://crm.zoho.com/crm/private/xml/Invoices/insertRecords?authtoken=Auth Token&scope=crmapi
XML Data:
<Invoices>
<row no="1">
...all other invoice attributes...
<FL val="Product Details">
<product no="1">
<FL val="Product Id">___your_zoho_productId___</FL>
<FL val="Product Name">___your_zoho_product_name___</FL>
<FL val="Quantity">1</FL>
<FL val="List Price">1.00</FL>
<FL val="Discount">0</FL>
<FL val="Total">1.00</FL>
<FL val="Total After Discount">1.00</FL>
<FL val="Tax">0</FL>
<FL val="Net Total">1.00</FL>
</product>
</FL>
...any other invoice attributes...
</row>
</Invoices>
API Method: insertRecords
XML Format:
https://crm.zoho.com/crm/private/xml/Leads/insertRecords?authtoken=Auth Token&scope=crmapi
XML Data:
<Potentials>
<row no="1">
<FL val="Potential Name">First Potential</FL>
<FL val="Description">description of the potential</FL>
<FL val="Closing Date"> 01/04/2009 </FL>
<FL val=" ACCOUNTID" >Your__Account__ID</FL>
<FL val="Email">test@test.test</FL>
<FL val="Stage">"-data-"</FL>
<FL val="boolean flag">TRUE</FL>
<FL val="product">FREE</FL>
<FL val="Date of Birth"> 01/01/1970</FL>
<FL val="Mailing City">Germany</FL>
</row>
</Potentials>
API Method: insertRecords
XML Format:
https://crm.zoho.com/crm/private/xml/Accounts/insertRecords?authtoken=Auth Token&scope=crmapi
XML Data:
<Accounts>
<row no="1">
<FL val="Account Name">TestUser</FL>
<FL val="Email">test@test.test</FL>
<FL val="boolean flag">TRUE</FL>
<FL val="First contact">01/01/2009</FL>
<FL val="Last Login">05/10/2009</FL>
<FL val="Created Time">2009-05-10 14:45:56</FL>
</row>
</Accounts>
Note:
import java.io.*;
import java.util.*;
import java.net.*;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.PartSource;
import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource;
public class GetRecords
{
public static void main(String a[])
{
try
{
String module = "CRM module name";
String authtoken=Auth Token;
String targetURL = "
https://crm.zoho.com/crm/private/xml/"+module+"/getRecords";
String paramname = "content";
PostMethod post = new PostMethod(targetURL);
post.setParameter("authtoken",authtoken);
post.setParameter("scope","crmapi");
HttpClient httpclient = new HttpClient();
PrintWriter myout = null;
/*-------------------------------------- Execute the http request--------------------------------*/
try
{
long t1 = System.currentTimeMillis();
int result = httpclient.executeMethod(post);
System.out.println("HTTP Response status code: " + result);
System.out.println(">> Time taken " + (System.currentTimeMillis() - t1));
/*-------------------------------------- Execute the http request--------------------------------*/
/* ---------------------------writing the response to a file--------------------*/
myout = new PrintWriter(new File("response.xml"));
myout.print(post.getResponseBodyAsString());
/* ---------------------------writing the response to a file--------------------*/
/*-----------------------Get response as a string ----------------*/
String postResp = post.getResponseBodyAsString();
System.out.println("postResp=======>"+postResp);
/* ---------------------Get response as a string ----------------------------*/
if(postResp.equals("Invalid Ticket Id"))
{
// generate the ticket id again and call the API
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
myout.close();
post.releaseConnection();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
API Method: insertRecords
XML Format:
https://crm.zoho.com/crm/private/xml/Leads/insertRecords?
authtoken=Auth Token&scope=crmapi
XML Data:
<Invoices>
<row no="1">
...all other invoice attributes...
<FL val="Product Details">
<product no="1">
<FL val="Product Id">___your_zoho_productId___</FL>
<FL val="Product Name">___your_zoho_product_name___</FL>
<FL val="Quantity">1</FL>
<FL val="List Price">1.00</FL>
<FL val="Discount">0</FL>
<FL val="Total">1.00</FL>
<FL val="Total After Discount">1.00</FL>
<FL val="Tax">0</FL>
<FL val="Net Total">1.00</FL>
</product>
</FL> ...any other invoice attributes...
</row>
</Invoices>
<?php
$username = "testUsername";
$password = "testPassword";
$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);
?>
import urllib
import urllib2
#You should have the price book id and product id, for using this API.
authtoken = 'Your authtoken'
pricebook_id = '508020142132343432'
product_id = '508020014316189251'
list_price = '900'
params = {'authtoken':authtoken,'scope':'crmapi','id':pricebook_id,'xmlData':''+product_id+''
+list_price+'','relatedModule':'Products'}
final_URL = "https://crm.zoho.com/crm/private/xml/PriceBooks/updateRelatedRecords"
data = urllib.urlencode(params)
request = urllib2.Request(final_URL,data)
response = urllib2.urlopen(request)
xml_response = response.read()
print xml_response