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








    Access your files securely from anywhere







                            Zoho Developer Community




                                                  • Desk Community Learning Series


                                                  • Digest


                                                  • Functions


                                                  • Meetups


                                                  • Kbase


                                                  • Resources


                                                  • Glossary


                                                  • Desk Marketplace


                                                  • MVP Corner


                                                  • Word of the Day


                                                  • Ask the Experts



                                                            • Sticky Posts

                                                            • Kaizen #222 - Client Script Support for Notes Related List

                                                              Hello everyone! Welcome to another week of Kaizen. The final Kaizen post of the year 2025 is here! With the new Client Script support for the Notes Related List, you can validate, enrich, and manage notes across modules. In this post, we’ll explore how
                                                            • Kaizen #217 - Actions APIs : Tasks

                                                              Welcome to another week of Kaizen! In last week's post we discussed Email Notifications APIs which act as the link between your Workflow automations and you. We have discussed how Zylker Cloud Services uses Email Notifications API in their custom dashboard.
                                                            • Kaizen #216 - Actions APIs : Email Notifications

                                                              Welcome to another week of Kaizen! For the last three weeks, we have been discussing Zylker's workflows. We successfully updated a dormant workflow, built a new one from the ground up and more. But our work is not finished—these automated processes are
                                                            • Kaizen #152 - Client Script Support for the new Canvas Record Forms

                                                              Hello everyone! Have you ever wanted to trigger actions on click of a canvas button, icon, or text mandatory forms in Create/Edit and Clone Pages? Have you ever wanted to control how elements behave on the new Canvas Record Forms? This can be achieved
                                                            • Kaizen #142: How to Navigate to Another Page in Zoho CRM using Client Script

                                                              Hello everyone! Welcome back to another exciting Kaizen post. In this post, let us see how you can you navigate to different Pages using Client Script. In this Kaizen post, Need to Navigate to different Pages Client Script ZDKs related to navigation A.


                                                            Manage your brands on social media



                                                                  Zoho TeamInbox Resources



                                                                      Zoho CRM Plus Resources

                                                                        Zoho Books Resources


                                                                          Zoho Subscriptions Resources

                                                                            Zoho Projects Resources


                                                                              Zoho Sprints Resources


                                                                                Qntrl Resources


                                                                                  Zoho Creator Resources



                                                                                      Zoho CRM Resources

                                                                                      • CRM Community Learning Series

                                                                                        CRM Community Learning Series


                                                                                      • Kaizen

                                                                                        Kaizen

                                                                                      • Functions

                                                                                        Functions

                                                                                      • Meetups

                                                                                        Meetups

                                                                                      • Kbase

                                                                                        Kbase

                                                                                      • Resources

                                                                                        Resources

                                                                                      • Digest

                                                                                        Digest

                                                                                      • CRM Marketplace

                                                                                        CRM Marketplace

                                                                                      • MVP Corner

                                                                                        MVP Corner









                                                                                          Design. Discuss. Deliver.

                                                                                          Create visually engaging stories with Zoho Show.

                                                                                          Get Started Now


                                                                                            Zoho Show Resources

                                                                                              Zoho Writer

                                                                                              Get Started. Write Away!

                                                                                              Writer is a powerful online word processor, designed for collaborative work.

                                                                                                Zoho CRM コンテンツ



                                                                                                  Nederlandse Hulpbronnen


                                                                                                      ご検討中の方