Search code examples
phpxml-parsingkey-valuesurveyzoho

convert xml response (from ZohoCRM) to simple "key=value" pairs (for SurveyGizmo) using php or Zoho Creator


This deals with exchanging data between two web services.

I'm stumped as to where to even start. Any assistance, pointers, or even complete solutions :) much appreciated.

I'll describe in plain language...

SurveyGizmo can post (insert) data to ZohoCRM.

After insert, Zoho responds with either an xml response that includes an ID of the inserted record.

Gizmo needs that ID for subsequent updates.

The problem is, Gizmo will only accept a response in a simple "key1=value1,key2=value2" format.

I'd like to have a php script on my server (or ZohoCreator deluge script) receive Gizmo's post, pass it through to ZohoCRM, receive the xml response from Zoho, convert it to key=value, and return it to Gizmo.

This is what Gizmo posts to ZohoCRM:

https://crm.zoho.com/crm/private/xml/Cases/insertRecords?newFormat=2&apikey=KEYGOESHERE&ticket=TICKETGOESHERE&duplicateCheck=2&xmlData=
<Cases>
<row no="1">
<FL val="WHOID">contactidrelatedtocasebeinginserted</FL>
<FL val="Subject">mysubject</FL>
<FL val="Project">myproject</FL>
<FL val="Case Owner">myemail</FL>
</row>
</Cases>

This is what Zoho CRM responds after insert:

(the 00000 from Id is what I need to return to Gizmo as "Id=000000"):

<response uri="/crm/private/xml/Cases/insertRecords">
<result>
<message>Record(s) inserted successfully</message>
<recorddetail>
<FL val="Id">00000</FL>
<FL val="Created Time">2011-11-03 20:14:44</FL>
<FL val="Modified Time">2011-11-03 21:34:39</FL>
<FL val="Created By">
<![CDATA[ Bamboo ]]>
</FL>
<FL val="Modified By">
<![CDATA[ Bamboo ]]>
</FL>
</recorddetail>
</result>
</response>

Thank you so much.

Below is a script I've been using elsewhere. I'm just not sure where to put the xml parser to echo the ID response back to the posting url.

////
/////note: the whole point of the script was to fix the hyphen to underscore in smtp-id so zoho would accept sendgrid
////
 define('POSTURL', 'https://creator.zoho.com/api/xml/USERNAME/test/add/');
 define('POSTVARS', '');  // POST VARIABLES TO BE SENT
 // INITIALIZE ALL VARS
 $event='';
 $email='';
 $response='';
 $attempt='';
 $url='';
 $status='';
 $reason='';
 $type='';
 $category='';
 $zoho_email_id='';
 $smtp_id='';
 $timestamp='';
 $ch='';
 $Rec_Data='';
 $Temp_Output='';

 if($_SERVER['REQUEST_METHOD']==='POST') {  // REQUIRE POST OR DIE
 if(isset($_POST['event'])) $event=$_POST['event'];  // GET event INTO VAR 
 if(isset($_POST['email'])) $email=$_POST['email'];  // GET email INTO VAR 
 if(isset($_POST['response'])) $response=$_POST['response'];  // GET response INTO VAR 
 if(isset($_POST['attempt'])) $attempt=$_POST['attempt'];  // GET attempt INTO VAR 
 if(isset($_POST['url'])) $url=$_POST['url'];  // GET url INTO VAR 
 if(isset($_POST['status'])) $status=$_POST['status'];  // GET status INTO VAR 
 if(isset($_POST['reason'])) $reason=$_POST['reason'];  // GET reason INTO VAR 
 if(isset($_POST['type'])) $type=$_POST['type'];  // GET type INTO VAR 
 if(isset($_POST['category'])) $category=$_POST['category'];  // GET category INTO VAR 
 if(isset($_POST['zoho_email_id'])) $zoho_email_id=$_POST['zoho_email_id'];  // GET zoho_email_id INTO VAR 
 if(isset($_POST['smtp-id'])) $smtp_id=$_POST['smtp-id'];  // GET smtp-id INTO VAR 
 if(isset($_POST['timestamp'])) $timestamp=$_POST['timestamp'];  // GET timestamp INTO VAR 

 $ch = curl_init(POSTURL);
 curl_setopt($ch, CURLOPT_POST      ,1);
 curl_setopt($ch, CURLOPT_POSTFIELDS    ,'apikey=APIKEY&zc_ownername=USERNAME&event='.$event.'&email='.$email.'&response='.$response.'&attempt='.$attempt.'&url='.$url.'&status='.$status.'&reason='.$reason.'&type='.$type.'&category='.$category.'&zoho_email_id='.$zoho_email_id.'&smtp_id='.$smtp_id.'&timestamp='.$timestamp);
 curl_setopt($ch, CURLOPT_FOLLOWLOCATION  ,1); 
 curl_setopt($ch, CURLOPT_HEADER      ,0);  // DO NOT RETURN HTTP HEADERS 
 curl_setopt($ch, CURLOPT_RETURNTRANSFER  ,1);  // RETURN THE CONTENTS OF THE CALL
 $Rec_Data = curl_exec($ch);

 ob_start();
 header("Content-Type: text/html");
 $Temp_Output = ltrim(rtrim(trim(strip_tags(trim(preg_replace ( "/\s\s+/" , " " , html_entity_decode($Rec_Data)))),"\n\t\r\h\v\0 ")), "%20");
 $Temp_Output = ereg_replace (' +', ' ', trim($Temp_Output));
 $Temp_Output = ereg_replace("[\r\t\n]","",$Temp_Output);
 $Temp_Output = substr($Temp_Output,307,200);
 echo $Temp_Output;
 $Final_Out=ob_get_clean();
 echo $Final_Out;  
 curl_close($ch);
} else die('Blah! That didn't work!');

exit;

Solution

  • Use simplexml in php to read and parse the xml. If you just need to retrieve the Id, you can do as simple as:

    $xml = simplexml_load_string($xml_string);          
    $response = 'Id='.$xml->result->recorddetail->FL;
    

    You can traverse though the whole XML structure. I'd suggest you read the documentaion: http://www.php.net/manual/en/book.simplexml.php