Showing posts with label web servise. Show all posts
Showing posts with label web servise. Show all posts

Wednesday, May 9, 2012

eKomi Frontend Integration

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 :)




Monday, May 7, 2012

eKomi link genaration

In this post i will show , how connect The SOAP-endpoint of the eKomi APIs http://api.ekomi.de/v2/wsdl and how to get account information using getSnapshot method via PHP.



The following parameters are expected for get account information, 1) auth 2)version

 Auth parameter containt Interface-ID and Interface-Password, separated by pipe (|) Example765|kgdf143shjk468775

Description of version is Script-Version. For self- build precede with ‘cust-’
 Example
cust-1.0.0


The response is a serialized PHP-array with the following elements:


Parameter Type
info array
info[account_name]
info[ekomi_certificate]
info[ekomi_certificate_id]
info[ekomi_certificate_seo]
info[fb_count]
info[fb_avg]
info[fb_avg_detail]
info[fb_avg_room]
info[fb_avg_service]
info[fb_avg_catering]
info[fb_avg_ambience]
feedbacks
feedbacks[0-9]
feedbacks[0-9]
 [transaction_id]

feedbacks[0-9] int Star rating (1-5)
[rating]
feedbacks[0-9]
[rating_room]
feedbacks[0-9]
[rating_service]
feedbacks[0-9]
[rating_catering]
feedbacks[0-9]
[rating_ambience]
 feedbacks[0-9]
    [message] 5)
    feedbacks[0-9]
    [comment] (1-5)
    feedbacks[0-9]
    [delivered] (1-5)

Following is example of php implementation of eKomi link genaration integration.


                        try{
                            $Clientekomi = new soapClient($config->ekomiWsdl);
                            $method = "getSnapshot";
                          
                            $paramss = array( new SoapParam($interfaceid.'|'.$ekomipassword, 'auth'), new SoapParam($config->ekomiCusVertion, 'version'));
                          
                            $retunarray = $Clientekomi->__call($method, $paramss);
                            $arr = unserialize(urldecode($retunarray));
                            $ekomicertificate = $arr['info']['ekomi_certificate_seo'];                       
                        }  catch (Exception $e){
                            //print_r($e);
                            print_r("Error Ocured When try to get Ekomi certificate url - $errorlang..!! \n");
                            //die('die in url');

                        } 


hope this will help you get better idea.... cheeeears... :)