PHP cURL insertRecords example
After trying to insert a Lead using the API documentation example (and subsequently banging my head against the wall for some time), then searching in the forums for examples to no avail, I decided to learn cURL, which is far simpler than I assumed. Someone else in the forum asked for an example with cURL a while back, so I figured I'd post what worked (fairly simply) for me.
- //using cURL to get new ticket each time instead of setting up script to renew every 7 days
- $tick = curl_init('https://accounts.zoho.com/login');
- curl_setopt($tick, CURLOPT_VERBOSE, 1);
- curl_setopt($tick, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($tick, CURLOPT_POST, 1);
- $query = "LOGIN_ID=[[YOUR ID]]&PASSWORD=[[YOUR PASSWORD]]&FROM_AGENT=true&servicename=ZohoCRM&submit=Generate Ticket";
- curl_setopt($tick, CURLOPT_POSTFIELDS, $query);
- $getTick = curl_exec($tick);
- curl_close($tick);
-
- //generated a string, need to dig ticket out
- $tickInfo = (explode("\n",$getTick));
- foreach($tickInfo as $t) {
- if (strstr($t,'TICKET=')) {
- $ticket = end(explode('=',$t));
- }
- }
-
- $api = 'YOUR API KEY';
- /*$xml is string of XML, formatted exactly like https://zohocrmapi.wiki.zoho.com/insertRecords-Method.html#Sample_Lead_XMLDATA*/
-
- //Initialize connection
- $ch = curl_init('https://crm.zoho.com/crm/private/xml/Leads/insertRecords');
- curl_setopt($ch, CURLOPT_VERBOSE, 1);//standard i/o streams
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);// Turn off the server and peer verification
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//Set to return data to string ($response)
- curl_setopt($ch, CURLOPT_POST, 1);//Regular post
- //Set post fields
- $query = "newFormat=1&apikey={$api}&ticket={$ticket}&xmlData={$xml}";
- curl_setopt($ch, CURLOPT_POSTFIELDS, $query);// Set the request as a POST FIELD for curl.
- //Execute cUrl session
- $response = curl_exec($ch);
- curl_close($ch);
Let me know if it doesn't work from straight copy/paste - I removed some code that shouldn't be essential to the example that.
The documentation would definitely benefit from more extensive examples...
-David
www.lookdadcreative.com