PHP cURL insertRecords example

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.

  1. //using cURL to get new ticket each time instead of setting up script to renew every 7 days

  2. $tick = curl_init('https://accounts.zoho.com/login');

  3. curl_setopt($tick, CURLOPT_VERBOSE, 1);

  4. curl_setopt($tick, CURLOPT_RETURNTRANSFER, 1);

  5. curl_setopt($tick, CURLOPT_POST, 1);

  6. $query = "LOGIN_ID=[[YOUR ID]]&PASSWORD=[[YOUR PASSWORD]]&FROM_AGENT=true&servicename=ZohoCRM&submit=Generate Ticket";

  7. curl_setopt($tick, CURLOPT_POSTFIELDS, $query);

  8. $getTick = curl_exec($tick);

  9. curl_close($tick);



  10. //generated a string, need to dig ticket out

  11. $tickInfo = (explode("\n",$getTick));

  12. foreach($tickInfo as $t) {

  13.     if (strstr($t,'TICKET=')) {

  14.         $ticket = end(explode('=',$t));

  15.     }

  16. }



  17. $api = 'YOUR API KEY';

  18. /*$xml is string of XML, formatted exactly like https://zohocrmapi.wiki.zoho.com/insertRecords-Method.html#Sample_Lead_XMLDATA*/



  19. //Initialize connection

  20. $ch = curl_init('https://crm.zoho.com/crm/private/xml/Leads/insertRecords');

  21. curl_setopt($ch, CURLOPT_VERBOSE, 1);//standard i/o streams

  22. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);// Turn off the server and peer verification

  23. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

  24. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//Set to return data to string ($response)

  25. curl_setopt($ch, CURLOPT_POST, 1);//Regular post

  26. //Set post fields

  27. $query = "newFormat=1&apikey={$api}&ticket={$ticket}&xmlData={$xml}";

  28. curl_setopt($ch, CURLOPT_POSTFIELDS, $query);// Set the request as a POST FIELD for curl.

  29. //Execute cUrl session

  30. $response = curl_exec($ch);

  31. 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