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.
- <?php
- $url = "https://accounts.zoho.com/login";
- $datais=array(
- "LOGIN_ID"=>"[your ZOHO user name]",
- "PASSWORD"=>"[your zoho user password]",
- "FROM_AGENT"=>"true",
- "servicename"=>"ZohoCreator");
-
- $ch = curl_init($url);
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($datais));
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- $result=curl_exec($ch);
- curl_close($ch);
- // echo print_r($result); un comment this is you want to see the results
- ?>
- This is what ZOHO returns... so we have to

- Now we have to parse the results to get our ticket.
- The following PHP code does the trick!
- $start=strpos($result,"TICKET=")+7;
- $length=strpos($result,"RESULT")-1;
- $apiticket=substr($result,$start,$length-$start);
- 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.
- <?php
- // this function allows you to use PHP to send API requests to ZOHO for any type of request supported by zoho REST
- // it is important to read carefully the ZOHO requirements
- // Where we have [something] you have to change it to meet your own requirements otherwise the program will not work
- function curl_post($url, array $post = NULL, array $options = array())
- {
- $defaults = array(
- CURLOPT_POST => 1,
- CURLOPT_HEADER => 0,
- CURLOPT_URL => $url, // this is the url that we built
- CURLOPT_FRESH_CONNECT => 1,
- CURLOPT_RETURNTRANSFER => 1,
- CURLOPT_SSL_VERIFYPEER=> FALSE,
- CURLOPT_FORBID_REUSE => 1,
- CURLOPT_TIMEOUT => 4,
- 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.
- );
-
- $ch = curl_init();
- curl_setopt_array($ch, ($options + $defaults));
- if( ! $result = curl_exec($ch))
- {
- trigger_error(curl_error($ch));
- }
- curl_close($ch);
- return $result;
- }
- // Using this function we retrieve a ZOHO ticket to be used along with our API code
- function getzohoticket(){
- // get zoho ticket
- $urlis="https://accounts.zoho.com/login";
- $datais=array(
- "LOGIN_ID"=>"[Your User Log ON ID]",
- "PASSWORD"=>"[Your Password]",
- "FROM_AGENT"=>"true",
- "servicename"=>"ZohoCreator");
- $result=curl_post($urlis, $datais);
- // echo "Api Ticket <br />"; Un comment these if you want to see what the results are
- // echo $result;
- // echo "<br />";
- $start=strpos($result,"TICKET=")+7;
- $length=strpos($result,"RESULT")-1;
- $apiticket=substr($result,$start,$length-$start);
- // echo $apiticket."<br /><br />";
- return $apiticket;
- }
- // This is just an example of how to add a record in ZOHO.. I use it to log a session for my users
- function logsession($ticketvalue){
- // get zoho ticket
- $baseurl ="http://creator.zoho.com/api";
- $format = "json";
- $application = "sessions";
- $formname ="Session";
- $username="[name of your use]";
- $mode="add";
- $apikey="[your key number]";
- $ticket=$ticketvalue;
- $datais = array(
- "apikey"=>$apikey,
- "ticket"=>$ticket,
- "zc_ownername"=>"[your zoho user name]",
- "Code" => $username,
- "Session_ID" => session_id()); // I use this to create a unique key that I can use to track on my user with
-
- //$datais=http_build_query($datais);
- $urlis=$baseurl."/".$format."/".$application."/".$formname."/".$mode."/";
- $result=curl_post($urlis, $datais);
- echo $result;
- return $result;
- }
- // This is important because we want to use something on the clien't server to register a cookie that we will use
- // later
- session_start();
- // 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
- if (!isset($_COOKIE['[add your prefix]zoho_ticket'])){
- $ticket = getzohoticket(); // let us get the zoho ticket here
- setcookie('[add your prefix]zoho_ticket',$ticket,time()+60*60*24*7); // set the 7 days default expiration
- }
- else{
- $ticket=$_COOKIE['[add your prefix]zoho_ticket']; // or we load the ticket that already exists
- }
- // here we take the ticket and do something with it... in this case we register a zoho session
- logsession($ticket);
- ?>