Creator API call via PHP not working.
I am trying to write a php application on our website that retrieves our list of members from the creator database we have.
Unfortunately the info and descriptions I've found so far are rather poor or outdated, so I am working somewhat blind in making this work even in the most recent examples I've emulated.
I do successfully retrieve a ticket, and my login, password, apikey are obscured for obvious reasons.
My goal is to read records from a view "PHCP_Members_Know_Location" I've already created and put them into an array using PHP on our site. My example dies when I go to use Curl to do the first read. The Error is below, with a result of null.
["ch"]=> resource(2) of type (curl) ["result"]=> string(0) ""
Notice: in zohologin2.php on line 25
If I try manually sending the post method via my browser I get some odd internal error from Zoho.
Any help would be appreciated in where I am missing something
- Code Example
- <?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))
{
echo "<pre>";
echo var_dump(get_defined_vars());
echo "</pre>";
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['zohophcp'.$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('zohophcp'.$cookie_suffix,$apiticket,time()+60*60);//set for one hour
}
else{
// here we retrieve the old cookie
$apiticket=$_COOKIE['zohophcp'.$cookie_suffix];
// now we extend the time by one hour 60 seconds * 60 minutes * X to make it hours
setcookie('zohophcp'.$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,"member");
// check to see if there is already a session open
if (isset($_COOKIE['zohophcpmember']))
{
$baseurl ="http://creator.zoho.com/api/";
$format = "csv/";
$mode="read/";
$datais = array(
"apikey"=>"xxxxxxxxxxx10754986bafa621",
"ticket"=>$apiticket, // this is the API ticket that get_zoho_ticket created or found
"zc_ownername"=>"phcpdb",
"CSVString"=>$csvString);
$urlis=$baseurl.$format.$mode;
$result=curl_post($urlis,$datais);
}
return $result;
}
$csvString='phcp,PHCP_Members_Know_Location,View\nStatus,Equals,Mentor,.';
$varis=zoho_read("xxxxxxx@email.com","somePassword",$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>";
?>