Tags

, , ,

zohoWhile working in a project earlier this month, I needed to insert certain data in the zoho form dynamically. I looked for it over the web but everything I got was in bits and pieces, so I thought of writing this down so that people looking for something like this would get everything at one place.

Before making the api call to add records with the rest apis we need to know some prerequisites

Prerequisites:–
1. Auth Token
We can get the auth token by two means, either by using the api mode or by browser mode

To generate the Auth Token, you need to send an authentication request to Zoho Accounts using the URL format below.

API Mode

https://accounts.zoho.com/apiauthtoken/nb/create?SCOPE=ZohoCreator/creatorapi&EMAIL_ID=Zoho CREATOR Username or Email ID&PASSWORD=Zoho CREATOR Password

Browser Mode

https://accounts.zoho.com/apiauthtoken/create?SCOPE=ZohoCreator/creatorapi

You can use the browser mode by logging into your account.

2. Request URL
The format for the request URL

 http://creator.zoho.com/api/<response format>/<applicaton name>/<operation>/apikey=[API Key]&ticket=[Ticket]

3.REST Response Format
The type of response being generated will depend on the ‘format’ specified in the request URL. Currently, the following response formats are supported

  • XML
  • CSV
  • JSON

I will here be using the XML format to demonstrate the example. Now lets go to the code snippets

First we need to define the auth token in a variable.

$auth_token = 'your auth token';

Looking at the xml format, here I have created a static form for this example which can be dynamic as well. Only concern is to check the mandatory fields for the module you are working with and making sure those fields are included in the xml

$xml_data = '<Leads>'; 
$xml_data .= '<row no="1">';
$xml_data .= '<FL val="Drujoopress">Paypal</FL>';
$xml_data .= '<FL val="Company">Mindfire Solutions</FL>';
$xml_data .= '<FL val="First Name">Maruti</FL>';
$xml_data .= '<FL val="Last Name">Mohanty</FL>';
$xml_data .= '<FL val="Email">mm@mm.com</FL>';
$xml_data .= '<FL val="Phone">1234567899</FL>';
$xml_data .= '<FL val="City">Bhubaneswar</FL>';
$xml_data .= '<FL val="State">Odisha</FL>';
$xml_data .= '<FL val="Country">India</FL>';
$xml_data .= '<FL val="Zip Code">751007</FL>';
$xml_data .= '</row>';
$xml_data .= '</Leads>';

Now looking on the request url, we have followed the request url format and filled with the required data

http://creator.zoho.com/api/xml/Leads/insertRecords?authtoken=' . $auth_token . '&scope=crmapi&xmlData=' . urlencode( $xml_data );

Now below is the complete code which dynamically saves the data in the zoho crm using the zoho crm REST API

<?php

$auth_token = 'your auth token';
$xml_data = '<Leads>'; 
$xml_data .= '<row no="1">';
$xml_data .= '<FL val="Lead Source">Drujoopress</FL>';
$xml_data .= '<FL val="Company">Mindfire Solutions</FL>';
$xml_data .= '<FL val="First Name">Maruti</FL>';
$xml_data .= '<FL val="Last Name">Mohanty</FL>';
$xml_data .= '<FL val="Email">mm@mm.com</FL>';
$xml_data .= '<FL val="Phone">1234567899</FL>';
$xml_data .= '<FL val="City">Bhubaneswar</FL>';
$xml_data .= '<FL val="State">Odisha</FL>';
$xml_data .= '<FL val="Country">India</FL>';
$xml_data .= '<FL val="Zip Code">751007</FL>';
$xml_data .= '</row>';
$xml_data .= '</Leads>';

$request_url = 'http://creator.zoho.com/api/xml/Leads/insertRecords?authtoken=' . $auth_token . '&scope=crmapi&xmlData=' . urlencode( $xml_data );$curl = curl_init( $request_url );

curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $curl, CURLOPT_CONNECTTIMEOUT, 5 );  
$curl_response = curl_exec( $curl );
curl_close( $curl );

Happy Coding πŸ™‚

References:
Writer Zoho
Insert Records

Advertisements