コード例 #1
0
ファイル: Client.php プロジェクト: EvanDotPro/cosmos
 /**
  * Takes the API configuration array from the INI and
  * starts a new instance of the API client
  * 
  * @param array $config
  * @return void
  * @throws Cosmos_Api_Client_Exception|Cosmos_Api_Exception
  */
 public function __construct($config)
 {
     if (!isset($config['consumerKey']) || !$config['consumerKey']) {
         throw new Cosmos_Api_Client_Exception("Cannot start API client without a consumer key.");
     }
     if (!isset($config['requestFormat']) || !$config['requestFormat']) {
         throw new Cosmos_Api_Client_Exception("Cannot start API client without specifying a request format.");
     }
     $this->_request = new Cosmos_Api_Request();
     $this->_request->setRequestFormat($config['requestFormat']);
     $this->_request->setOauthParam('oauth_consumer_key', $config['consumerKey']);
     $this->_request->setOauthParam('oauth_signature_method', 'HMAC-SHA1');
     $this->_request->setOauthParam('oauth_version', '1.0');
     $format = $this->_request->getRequestFormat();
     if ($format == 'local') {
         if (!isset($config['version']) || !$config['version']) {
             throw new Cosmos_Api_Client_Exception("Using local mode requires an API version number to be specified.");
         }
         if (isset($config['responseFormat'])) {
             Zend_Registry::get('log')->info('Notice: The API responseFormat configuration parameter is ignored when you are using local request mode.');
         }
         if (isset($config['consumerSecret'])) {
             Zend_Registry::get('log')->info('Notice: There is no need to specify the API consumerSecret when using local mode.');
         }
         if (isset($config['url'])) {
             Zend_Registry::get('log')->info('Notice: There is no need to specify the API URL when using local mode.');
         }
         $this->_request->setVersion($config['version']);
         $this->_request->setResponseFormat($format);
     } else {
         if (!isset($config['url']) || !$config['url']) {
             throw new Cosmos_Api_Client_Exception("Using {$format} mode requires an API URL to be specified.");
         }
         if (!isset($config['consumerSecret']) || !$config['consumerSecret']) {
             throw new Cosmos_Api_Client_Exception("Using {$format} mode requires the consumer secret to be specified.");
         }
         if (isset($config['version'])) {
             Zend_Registry::get('log')->info('Notice: The API version configuration parameter is ignored unless you are using local request mode.');
         }
         $this->_request->setUrl($config['url']);
         $this->_request->setOauthConsumerSecret($config['consumerSecret']);
     }
     switch ($format) {
         case 'local':
             $this->_internalClient = new Cosmos_Api_Client_Local($config['version']);
             break;
         case 'xmlrpc':
             $this->_internalClient = new Cosmos_Api_Client_XmlRpc($config['url']);
             break;
         case 'jsonrpc':
             $this->_internalClient = new Cosmos_Api_Client_JsonRpc($config['url']);
             break;
     }
 }