Get ZOHO Ticket using PHP (CURL)

Get ZOHO Ticket using PHP (CURL)

PHP GET ZOHO TICKETS
I had to search high and low to get this information.  I was not easy and finally I managed to do it.  Without a ZOHO cookie capability there has to be some way to track users who are logged on through ZOHO's own user management system.

The following PHP program can be used on your own server.  It requires the CURL module of PHP to be activated.  CURL allows you to send POST and URL inquiries to other applications.  In this case we request ZOHO to give us an API Ticket number that we will use in conjunction with our API identification number.

This is how you do it.  The following is a PHP program.  You can make it a PHP function to return you that value of the PHP that you need to use the ZOHO API.

  1. <?php
  2. $url = "https://accounts.zoho.com/login";   
  3. $datais=array(
  4.   "LOGIN_ID"=>"[your ZOHO user name]",
  5.   "PASSWORD"=>"[your zoho user password]",
  6.   "FROM_AGENT"=>"true",
  7.   "servicename"=>"ZohoCreator");
  8.   
  9. $ch = curl_init($url);
  10. curl_setopt($ch, CURLOPT_POST, true);
  11. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($datais));
  12. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  13. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  14. $result=curl_exec($ch);
  15. curl_close($ch);
  16. // echo print_r($result); un comment this is you want to see the results
  17. ?>
  18. This is what ZOHO returns... so we have to 
  19. Now we have to parse the results to get our ticket.

  20. The following PHP code does the trick!

  21. $start=strpos($result,"TICKET=")+7;
  22. $length=strpos($result,"RESULT")-1;
  23. $apiticket=substr($result,$start,$length-$start);

  24. Now we put it in an web application on our website to get the results and use them to let's say get a ticket to use in our session monitoring program.

  25. <?php
  26. // this function allows you to use PHP to send API requests to ZOHO for any type of request supported by zoho REST
  27. // it is important to read carefully the ZOHO requirements
  28. // Where we have [something] you have to change it to meet your own requirements otherwise the program will not work
  29. function curl_post($url, array $post = NULL, array $options = array())
  30. {
  31.     $defaults = array(
  32.     CURLOPT_POST => 1,
  33.     CURLOPT_HEADER => 0,
  34.     CURLOPT_URL => $url, // this is the url that we built
  35.     CURLOPT_FRESH_CONNECT => 1,
  36.     CURLOPT_RETURNTRANSFER => 1,
  37.     CURLOPT_SSL_VERIFYPEER=> FALSE,            
  38.     CURLOPT_FORBID_REUSE => 1,
  39.     CURLOPT_TIMEOUT => 4,
  40.     CURLOPT_POSTFIELDS => http_build_query($post)  // this PHP 5.0 command ensures that our post variables are formatted properly.  It helps you to save a lot of coding which you would otherwise have to do.
  41.         );
  42.     
  43.     $ch = curl_init();
  44.     curl_setopt_array($ch, ($options + $defaults));
  45.     if( ! $result = curl_exec($ch))
  46.     {
  47.         trigger_error(curl_error($ch));
  48.     }
  49.     curl_close($ch);
  50.     return $result;
  51.     } 
  52. // Using this function we retrieve a ZOHO ticket to be used along with our API code
  53. function getzohoticket(){
  54.     // get zoho ticket
  55.     $urlis="https://accounts.zoho.com/login";
  56.     $datais=array(
  57.     "LOGIN_ID"=>"[Your User Log ON ID]",
  58.     "PASSWORD"=>"[Your Password]",
  59.     "FROM_AGENT"=>"true",
  60.     "servicename"=>"ZohoCreator");
  61.     $result=curl_post($urlis, $datais);
  62. //    echo "Api Ticket <br />";  Un comment these if you want to see what the results are
  63. //    echo $result;
  64. //    echo "<br />";    
  65.     $start=strpos($result,"TICKET=")+7;
  66.     $length=strpos($result,"RESULT")-1;
  67.     $apiticket=substr($result,$start,$length-$start);
  68. //    echo $apiticket."<br /><br />";    
  69.     return $apiticket;
  70. }
  71. // This is just an example of how to add a record in ZOHO.. I use it to log a session for my users
  72. function logsession($ticketvalue){
  73.     // get zoho ticket
  74.     $baseurl ="http://creator.zoho.com/api";
  75.     $format = "json";
  76.     $application = "sessions";
  77.     $formname ="Session";
  78.     $username="[name of your use]";
  79.     $mode="add";
  80.     $apikey="[your key number]";
  81.     $ticket=$ticketvalue;
  82.     $datais = array(
  83.     "apikey"=>$apikey,
  84.     "ticket"=>$ticket,
  85.     "zc_ownername"=>"[your zoho user name]",
  86.     "Code" => $username,
  87.     "Session_ID" => session_id()); // I use this to create a unique key that I can use to track on my user with
  88.     
  89.     //$datais=http_build_query($datais);
  90.     $urlis=$baseurl."/".$format."/".$application."/".$formname."/".$mode."/";
  91.     $result=curl_post($urlis, $datais);    
  92.     echo $result;
  93.     return $result;
  94. }
  95. // This is important because we want to use something on the clien't server to register a cookie that we will use
  96. // later
  97. session_start();
  98. // here we request from zoho and set a cookie that has the zoho ticket number if it doesn't exist otherwise we load the cookie that has the zoho ticket number
  99. if (!isset($_COOKIE['[add your prefix]zoho_ticket'])){
  100.     $ticket = getzohoticket(); // let us get the zoho ticket here
  101.     setcookie('[add your prefix]zoho_ticket',$ticket,time()+60*60*24*7);     // set the 7 days default expiration
  102. }
  103. else{
  104.     $ticket=$_COOKIE['[add your prefix]zoho_ticket']; // or we load the ticket that already exists
  105. }
  106. // here we take the ticket and do something with it... in this case we register a zoho session
  107. logsession($ticket);
  108. ?>