Integration of ZOHO CRM to MySQL using PHP
Hi Developers,
I have one scenario....
When user create Leads in ZOHO CRM account at that same time the same record with same values also get created in My local database(MySQL) using PHP Coding style.
Now i am using following code for this functionality as per the ZOHO API specification and example
Code:-
<?php
error_reporting(0);
/* 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", "AUTH Key");
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);
echo "Extension:=".$parameter."<br>";
$fullurl = "https://crm.zoho.com/crm/private/xml/Leads/getMyRecords?".$parameter;
echo "Full URL:=".$fullurl."<br>";
/* Call API */
$response = $utilObj->sendCurlRequest(TARGETURL, $parameter);
$utilObj->parseXMLandInsertInDB($response);
class Utilities
{
public function setParameter($key, $value, $parameter)
{
echo "We are in setParameter function.....<br/>";
if ($parameter === "" || strlen($parameter) == 0)
{
$parameter = $key . '=' . $value;
}
else
{
$parameter .= '&' . $key . '=' . $value;
}
return $parameter;
}
public function parseXMLandInsertInDB($xmldata) {
$xmlString = <<<XML
$xmlData
XML;
echo "We are in parseXMLandInsertInDB function.....<br/>";
$xml = simplexml_load_string($xmlString);
echo "We are in parseXMLandInsertInDB If function.....---<br/>";
$modeuleDependantObj = new moduleDependant();
$output = $modeuleDependantObj->insertInDB($xml);
echo "We are in parseXMLandInsertInDB If function.....<br/>";
}
public function sendCurlRequest($url, $parameter)
{
echo "We are in sendCurlRequest function.....<br/>";
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);
echo "We are in sendCurlRequesThe value of $ch=".$ch."and URL = ".$url."<br/>";
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";
private $host = "localhost";
private $username = "root";
private $password = "";
public function insertInDB($xml)
{
echo "We are in insertInDB function.....<br/>";
$numberOfRecords = count($xml->result->Leads->row);
echo "Number of records in XML file:-".$numberOfRecords."<br/>";
/* $records[row value][field value] */
$records[][] = array();
for ($i = 0; $i < $numberOfRecords; $i++)
{
echo "We are in $i for loop.....<br/>";
$numberOfValues = count($xml->result->Leads->row[$i]->FL);
for ($j = 0; $j < $numberOfValues; $j++)
{
echo "We are in $j for loop.....<br/>";
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];
echo "LeadID:-".$records[$i]['LEADID']."</br>";
break;
case 'First Name':
$records[$i]['First Name'] = (string) $xml->result->Leads->row[$i]->FL[$j];
echo "First Name:-".$records[$i]['First Name']."</br>";
break;
case 'Last Name':
$records[$i]['Last Name'] = (string) $xml->result->Leads->row[$i]->FL[$j];
echo "Last Name:-".$records[$i]['Last Name']."</br>";
break;
case 'Company':
$records[$i]['Company'] = (string) $xml->result->Leads->row[$i]->FL[$j];
echo "Company:-".$records[$i]['Company']."</br>";
break;
}
}
}
/* Inserting in database */
//$connection = mysql_connect($this->host, $this->username, $this->password) or die(mysql_error());
$connection=mysqli_connect("Localhost","root","","test");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//$query = "Insert into crm.leads (id, firstname, lastname, company) value ";
$query = "Insert into basic_information (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'] . "')";
echo $query;
}
if ($k > 0)
{
$query .= ", ('" . $records[$k]['LEADID'] . "','" . $records[$k]['First Name'] . "','" . $records[$k]['Last Name'] . "','" . $records[$k]['Company'] . "')";
echo $query;
}
}
$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';
}
}
}
?>
After run this script i cant get the any response from XML file?
If any one have any idea or code about this functionality then please share it with me .
Thanks & Regards,
Rupam Raut