Overview

Namespaces

  • Datadepo
    • Api
      • DataStores
      • Structures
      • Synchronizers

Classes

  • Datadepo\Api\ApiWrapper
  • Datadepo\Api\Collector
  • Datadepo\Api\DataDepoResponse
  • Datadepo\Api\DataDepoSync
  • Datadepo\Api\DataStores\PdoDataStore
  • Datadepo\Api\IniConfiguration
  • Datadepo\Api\RunningFiles
  • Datadepo\Api\Structures\AbstractStructure
  • Datadepo\Api\Structures\BusinessLine
  • Datadepo\Api\Structures\BusinessSupplierLine
  • Datadepo\Api\Structures\CategoryLine
  • Datadepo\Api\Structures\DataLine
  • Datadepo\Api\Structures\ImageLine
  • Datadepo\Api\Structures\ParameterLine
  • Datadepo\Api\Structures\PriceLine
  • Datadepo\Api\Structures\RelatedLine
  • Datadepo\Api\Structures\StoreLine
  • Datadepo\Api\Structures\SupplierBankAccountLine
  • Datadepo\Api\Structures\SupplierLine
  • Datadepo\Api\Structures\SupplierPersonLine
  • Datadepo\Api\Structures\VariantLine
  • Datadepo\Api\Synchronizers\AbstractSynchronizer
  • Datadepo\Api\Synchronizers\BusinessSynchronizer
  • Datadepo\Api\Synchronizers\CategorySynchronizer
  • Datadepo\Api\Synchronizers\DataSynchronizer
  • Datadepo\Api\Synchronizers\SuppliersSynchronizer

Interfaces

  • Datadepo\Api\DataStores\IDataStore

Exceptions

  • Datadepo\Api\ApiException
  • Datadepo\Api\ConfigurationException
  • Datadepo\Api\DataDepoRunningException
  • Datadepo\Api\DataDepoSuspendedException
  • Overview
  • Namespace
  • Class
  1: <?php
  2: namespace Datadepo\Api;
  3: 
  4: class ApiWrapper
  5: {
  6:   
  7:   /** @var array */
  8:   protected $settingsDatadepo;
  9:   
 10:   /** @var array */
 11:   protected $settingsAccount;
 12: 
 13:   /**
 14:    * @param array $settingsDatadepo
 15:    * @param array $settingsAccount
 16:    */
 17:   public function __construct($settingsDatadepo, $settingsAccount)
 18:   {
 19:     $this->settingsDatadepo = $settingsDatadepo;
 20:     $this->settingsAccount = $settingsAccount;
 21:   }
 22:  
 23:   /**
 24:    * @param string $type
 25:    * @param string $actionType
 26:    * @param array $params
 27:    * @return \stdClass
 28:    */
 29:   public function request($type, $actionType, array $params = array())
 30:   {
 31:     $url = $this->settingsDatadepo['apiUrl'] . '/' . $this->settingsDatadepo['version']. '/' . $type . '/' . $actionType . '/';
 32:     list($socket) = $this->createFsock($url, $params);
 33:     $contents = "";
 34:     while (!feof($socket)) {
 35:       $contents .= fgets($socket, 4096);
 36:     }
 37:     fclose($socket);
 38:     
 39:     $json = json_decode($contents);
 40:     if (!$json instanceof \stdClass) {
 41:       throw new ApiException('Unrecognized response from datadepo');
 42:     }
 43:     return $json;
 44:   }
 45:   
 46:   /**
 47:    * @param \stdClass $json
 48:    * @return string|null
 49:    */
 50:   public function analyzeResponse($json)
 51:   {
 52:     if (isset($json->url)) {
 53:       return 'url';
 54:     }
 55:     elseif (isset($json->suspend)) {
 56:       return 'suspend';
 57:     }
 58:     return NULL;
 59:   }
 60:   
 61:   /**
 62:    * @param string $url
 63:    * @param string $filePath
 64:    */
 65:   public function download($url, $filePath)
 66:   {
 67:     list($socket) = $this->createFsock($url);
 68: 
 69:     //write data to file
 70:     umask(0000);
 71:     $fp = fopen($filePath, 'w');
 72:     while (!feof($socket)) {
 73:       fwrite($fp, fread($socket, 4096));
 74:     }
 75:     fclose($fp);
 76:     fclose($socket);
 77:     chmod($filePath, 0777);
 78:   }
 79:   
 80:   
 81:   
 82:   
 83:   
 84:   /**
 85:    * @param string $url
 86:    * @param array $params
 87:    * @return resource
 88:    * @throws Exception
 89:    */
 90:   protected function createFsock($url, $params = FALSE)
 91:   {
 92:     $url = parse_url($url);
 93:     if (!isset($url['host']) || !isset($url['path'])) {
 94:       throw new ApiException('Invalid URL');
 95:     }
 96:     
 97:     $host = $url['host'];
 98:     $path = $url['path'] . (isset($url['query']) ? '?' .  $url['query'] : '');
 99:     $socket = fsockopen($host, 80, $errno, $errstr, $this->settingsDatadepo['connectTimeout']);
100:     if (!$socket) {
101:       throw new ApiException($errstr);
102:     }
103:     stream_set_timeout($socket, $this->settingsDatadepo['connectTimeout']); 
104:     
105:     //add authentication informations to post
106:     $postData = FALSE;
107:     if ($params !== FALSE) {
108:       foreach (array('user', 'pwd') as $name) {
109:         if (isset($this->settingsAccount[$name])) {
110:           $params[$name] = $this->settingsAccount[$name];
111:         }
112:       }
113:       $postData = http_build_query($params);
114:     }
115:     
116:     $http  = "POST ".$path." HTTP/1.0\r\n";
117:     $http .= "Host: ".$host."\r\n";
118:     if (isset($_SERVER['HTTP_USER_AGENT'])) {
119:       $http .= "User-Agent: " . $_SERVER['HTTP_USER_AGENT'] . "\r\n";
120:     }
121:     $http .= "Content-Type: application/x-www-form-urlencoded\r\n";
122:     if ($postData) {
123:       $http .= "Content-length: " . strlen($postData) . "\r\n";
124:     }
125:     $http .= "Connection: close\r\n\r\n";
126:     if ($postData) {
127:       $http .= $postData . "\r\n\r\n";
128:     }
129:     fwrite($socket, $http); 
130:     
131:     //remove header
132:     $header = '';
133:     
134:     do {
135:       $c = fgets ( $socket, 4096 );
136:       if ($c === FALSE) {
137:         throw new ApiException('Connection timeout');
138:       }
139:             $header .= $c;
140:         } 
141:     while (strpos($header, "\r\n\r\n") === FALSE);
142:     return array($socket, $header);
143:   }
144:   
145: }
API documentation generated by ApiGen