This Post show eKomi Fronted Integration with zend application.
In this example i have create ItemreviewServise file for connect eKomi API get reviews and store those reviews on local database. For performance reasons eKomi recommend to store the reviews in local database.
class Itemreview_Service_ItemreviewService extends Base_Model_Service {
public function ekomiItemreview($language)
{
$ekomicertificate = '';
$errorlang = "";
if($language == "english"){
$interfaceid = $configItem->ekomiInterfaceId;
$ekomipassword = $configItem->ekomiPassword;
$errorlang = "English";
}
try
{
$Url = $config->ekomiProFeedbakUrl;
$Service = new Itemreview_Service_ClassAPIwebService($Url, "GET", "ISO-8859-1");
$Params["interface_id"] = $interfaceid;
$Params["interface_pw"] = $ekomipassword;
$Params["type"] = "csv";
$Params["range"] = $config->ekomiRange;
flush();
$Response = $Service->SendRequest($Params);
$CSVBODY = $Response["Body"];
$ItemreviewServicearray = new Itemreview_Service_ItemreviewService();
$retunval = $ItemreviewServicearray->csv_to_array($CSVBODY);
foreach ($retunval as $key => $arrParam) {
try
{
$ItemreviewService = new Itemreview_Service_ItemreviewService();
$Itemreviwentity = new Itemreview_Entity_Itemreview();
$Itemreviwentity->setItm_id($arrParam['product_id']);
$Itemreviwentity->setLang_id(66);
$Itemreviwentity->set_auther("eKomi");
$Itemreviwentity->setRe_text($arrParam['feedback']);
$Itemreviwentity->setRe_text($replasequote);
$Itemreviwentity->set_rating($arrParam['star_rating']);
$Itemreviwentity->set_reviewlink("$ekomicertificate");
$Itemreviwentity->setRe_isenable(1);
$Itemreviwentity->set_unixtime($arrParam['UNIXTIME']);
$Itemreviwentity->set_orderid($arrParam['order_id']);
$ItemreviewDAO = new Itemreview_Dao_ItemreviewDao();
$ItemreviewDAO->addEkomireview($Itemreviwentity);
}
catch ( Exception $e)
{
print_r("error ...!!");
}
}
}
catch ( Exception $e)
{
print_r("record fail-$errorlang ---!!!");
}
}
public static function csv_to_array($csv, $delimiter = ',', $enclosure = '"', $escape = '\\', $terminator = "\n") {
$r = array();
$filecsv = APPLICATION_PATH."/xml/eKomiresponce.csv";
if(file_exists($filecsv)){
unlink($filecsv);
}
$fp = fopen($filecsv, 'w');
fwrite($fp, $csv);
fclose($fp);
$name_columns = array("UNIXTIME", "order_id", "product_id", "star_rating", "feedback");
$handle = fopen ($filecsv, 'r');
while (($data = fgetcsv($handle, 1000, ',', '"')) !== FALSE)
{
$r[] = array_combine($name_columns,$data);
}
return $r;
}
Following Class , i use to connect eKomi web service and get reviews
class Itemreview_Service_ClassApiwebService
{
const TIME_OUT = 10;
private $UserAgent;
private $Method;
private $CharSet;
private $Url;
public function __construct($ServerUrl, $Method, $CharSet, $UserAgent="")
{
error_reporting(E_ERROR | E_WARNING | E_PARSE);
if (function_exists(domxml_open_mem))
throw new Exception("Please comment out the line 'extension=php_domxml.dll' in Apache/bin/php.ini and restart the server!");
if (empty($UserAgent)) $UserAgent = "PHP WebService Client";
$this->Url = parse_url($ServerUrl);
$this->UserAgent = $UserAgent;
$this->Method = strtoupper($Method);
$this->CharSet = $CharSet;
if (empty($this->Url["path"])) $this->Url["path"] = "/";
}
public function SendRequest($Data, $SoapAction="")
{
if (is_array($Data))
{
$Params = array();
foreach ($Data as $Key => $Value)
{
$Encode = str_replace('%7E', '~', rawurlencode($Value));
$Params[] = "$Key=$Encode";
}
$Query = implode('&', $Params);
}
else $DispParams = $Data;
switch ($this->Method)
{
case "GET":
$SendData = "GET ".$this->Url["path"]."?$Query HTTP/1.0\r\n";
$SendData .= "Host: " . $this->Url['host'] . "\r\n";
$SendData .= "User-Agent: " . $this->UserAgent . "\r\n";
$SendData .= "\r\n";
break;
case "POST":
$SendData = "POST ".$this->Url["path"]." HTTP/1.0\r\n";
$SendData .= "Host: " . $this->Url['host'] . "\r\n";
$SendData .= "Content-Type: application/x-www-form-urlencoded; charset=".$this->CharSet."\r\n";
$SendData .= "Content-Length: " . strlen($Query) . "\r\n";
$SendData .= "User-Agent: " . $this->UserAgent . "\r\n";
$SendData .= "\r\n";
$SendData .= $Query;
break;
default:
throw new Exception("Invalid Method: ".$this->Method);
}
$Port = array_key_exists('port',$this->Url) ? $this->Url['port'] : null;
switch ($this->Url['scheme'])
{
case 'https':
if (!function_exists(openssl_verify))
throw new Exception("Please remove the comment in the line ';extension=php_openssl.dll' in Apache/bin/php.ini and restart the server!");
$Scheme = 'ssl://';
$Port = ($Port === null) ? 443 : $Port;
break;
case 'http':
$Scheme = '';
$Port = ($Port === null) ? 80 : $Port;
break;
default:
throw new Exception("Invalid protocol in: ".$this->ServerUrl);
}
$Socket = @fsockopen($Scheme . $this->Url['host'], $Port, $ErrNo, $ErrStr, 10);
if (!$Socket)
throw new Exception ("Unable to establish connection to host " . $this->Url['host'] . " $ErrStr");
fwrite($Socket, $SendData);
$Response = "";
while (!feof($Socket))
{
$Response .= fgets($Socket, 1000);
}
fclose($Socket);
// Between Header and ResponseBody there are two empty lines
list($Header, $ResponseBody) = explode("\r\n\r\n", $Response, 2);
$Split = preg_split("/\r\n|\n|\r/", $Header);
list($Protocol, $StatusCode, $StatusText) = explode(' ', trim(array_shift($Split)), 3);
$RetArray["Status"] = $StatusCode;
$RetArray["Body"] = $ResponseBody;
return $RetArray;
}
}
Hope this will help :)