Read ZOHO Database using PHP (CURL)

Read ZOHO Database using PHP (CURL)

Here is the working version of PHP CURL to read data from your ZOHO database.
Using the CSV POST method to avoid using GET"s that show your queries in the address bar
This is the code for posting the data using PHP CURL

<?php
// This is the function for posting with PHP using CURL
function curl_post($url, array $post = NULL, array $options = array())
{
    $defaults = array(
    CURLOPT_POST => TRUE,
    CURLOPT_HEADER => 0,
    CURLOPT_URL => $url,
    CURLOPT_FRESH_CONNECT => 1,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_SSL_VERIFYPEER=> FALSE,           
    CURLOPT_FORBID_REUSE => 1,
    CURLOPT_VERBOSE=> 1,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_POSTFIELDS => http_build_query($post)
     );
   
    $ch = curl_init();
    curl_setopt_array($ch, ($options + $defaults));
    if( ! $result = curl_exec($ch))
    {
        trigger_error(curl_error($ch));
    }
    curl_close($ch);
    return $result;
    }

// This is where we pick up our ticket from zoho and save it as a cookie
function get_zoho_ticket($ownerlogin,$ownerpassword,$cookie_suffix){
    // will check to see if a session has been started and if not will start one
    if (!isset($_SESSION))
    {
        session_start();
    }
    // will check to see if a session is going otherwise we will create a session
    // for [your standard cookie name] replace with 'my_own_name' or something unique to you
    // when you add the suffix you can use the same code for different applications
    // just change the suffix to whatever like '_employees'
    if (!isset($_COOKIE[[your standard cookie name].$cookie_suffix]))
    {
        $url = "https://accounts.zoho.com/login";  
        $datais=array(
        "LOGIN_ID"=>$ownerlogin,
        "PASSWORD"=>$ownerpassword,
        "FROM_AGENT"=>"true",
        "servicename"=>"ZohoCreator");
        $result = curl_post($url,$datais); 
        // this is where we extract the ticket value
        $start=strpos($result,"TICKET=")+7;
        $length=strpos($result,"RESULT")-1;
        $apiticket=substr($result,$start,$length-$start);   
        setcookie([your standard cookie name].$cookie_suffix,$apiticket,time()+60*60);//set for one hour
    }
    else{
        // here we retrieve the old cookie
        $apiticket=$_COOKIE[[your standard cookie name].$cookie_suffix];
        // now we extend the time by one hour 60 seconds * 60 minutes * X to make it hours
        setcookie([your standard cookie name].$cookie_suffix,$apiticket,time()+60*60);//reset for one hour
    }
return $apiticket;
}

// Here we read our zoho database
function zoho_read($ownername,$ownerpassword,$csvString)
{
    // will check for ticket and generate one if one has not been generated
    $apiticket = get_zoho_ticket($ownername,$ownerpassword,"[add to  make unique cookie for your app]");   

    // check to see if there is already a session open
    if (isset($_COOKIE['your unique cookie name']))
    {
        $baseurl ="http://creator.zoho.com/api/";
        $format = "csv/";
        $mode="read/";
        $datais = array(
        "apikey"=>"[your API key]",
        "ticket"=>$apiticket, // this is the API ticket that get_zoho_ticket created or found
        "zc_ownername"=>"[owner name]",
        "CSVString"=>$csvString);
        $urlis=$baseurl.$format.$mode;
        $result=curl_post($urlis,$datais);
    }
    return $result;   
}   
// you must be very careful about the capitalization.  You will loose hours if you dont get it right.
// when logged in execute in  your browser http://creator.zoho.com/api/reference
// this will show you just how your applications and forms are named FOLLOW CAPITALIZATION EXACTLY
// use the \n at the end of View to indicate new line
// use the list of operators Equals, Notequal etc.
// if you use AND or OR then remember to put \n and not a comma
// at the very end your last item will be comma then period [,.]
// for example $csvSTRING = 'employees,Staff,View\nName,Equals,John,AND\nAge,Equals,35,.
// the http_build_query() function will convert the string to a legal post
// just the same avoid white spaces
$csvString='[application name in small caps],[Form Name],View\n[column],[operator],[value],.';
$varis=zoho_read("[owner name]","[your password]",$csvString);
// process your the return information  here like take the $varis which is a CSV string and use PHP string
// functions to extract the data you need

//Sample Output of $varis

echo "<pre>";
echo var_dump($varis);
echo "</pre>";
?>
string(573) "Form Name , Criteria , Status
Staff ,ID_CODE equals 502 ,Success
Modified_User_IP_Address ,Added_User_IP_Address ,Added_User ,Added_Time ,Modified_User ,Modified_Time ,ID ,Full_Name ,ID_CODE ,EXTENSION ,Department ,Location ,TITLE ,Job_Title ,NATIONALITY ,Subject_field ,Message ,Password ,Dont_Know
XXX.XXX.16.217 , XX.XXX.158.XXX , xxxxxxx , 26-May-2010 09:13:50 , Public , 24-Jan-2011 11:40:26 , 420594000000086079 , CHRISTOPHER RICHARDSON , 502 , 441 , Admin , Rayyan Admin , DEPUTY MANAGER , Admin Manager , AMERICAN , , , , false




"