├── README.markdown ├── README.md ├── config ├── autoload.php └── mage_api.php ├── libraries └── Mage_api.php └── spark.info /README.markdown: -------------------------------------------------------------------------------- 1 | # CodeIgniter Magento API Spark 2 | 3 | This spark facilitates the access to all [Magento](http://www.magentocommerce.com/) Core API resources and methods. 4 | One library, one config file and that is it! 5 | 6 | ## Requirements 7 | 8 | 1. PHP 5.1+ 9 | 2. CodeIgniter Reactor 2.0 10 | 3. Magento installation 11 | 4. [PHP-SOAP](http://www.php.net/soap) 12 | 13 | ## Usage 14 | 15 | Open config file and enter: 16 | - Magento WSDL URI, eg: http://demo.magentocommerce.com/api/soap?wsdl 17 | - Magento API username and password 18 | 19 | This spark automagically calls [API methods](http://www.magentocommerce.com/support/magento_core_api) from inside CodeIgniter code. 20 | So you don't need to worry about instantiating SOAP or anything. 21 | 22 | A few examples below. 23 | 24 | Want to update a product? 25 | 26 | $this->load->spark('mage-api/0.0.1'); 27 | $update = array('name'=>'New Name'); 28 | var_dump( $this->mage_api->product_update( 'product_sku', $update ) ); 29 | 30 | How about getting a list (PHP array) of all customer groups? 31 | 32 | var_dump( $this->mage_api->customer_group_list() ); 33 | 34 | The "magic" is that _customer_group_list_ is translated to _customer_group.list_ API call and so on with all the methods on the API. 35 | 36 | ## Change Log 37 | 38 | ### 0.0.1 39 | 40 | * First release -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CodeIgniter Magento API Spark 2 | 3 | This spark facilitates the access to all [Magento](http://www.magentocommerce.com/) Core API resources and methods. 4 | One library, one config file and that is it! 5 | 6 | ## Requirements 7 | 8 | 1. PHP 5.1+ 9 | 2. CodeIgniter Reactor 2.0 10 | 3. Magento installation 11 | 4. [PHP-SOAP](http://www.php.net/soap) 12 | 13 | ## Usage 14 | 15 | Open config file and enter: 16 | - Magento WSDL URI, eg: http://demo.magentocommerce.com/api/soap?wsdl 17 | - Magento API username and password 18 | 19 | This spark automagically calls [API methods](http://www.magentocommerce.com/support/magento_core_api) from inside CodeIgniter code. 20 | So you don't need to worry about instantiating SOAP or anything. 21 | 22 | A few examples below. 23 | 24 | Want to update a product? 25 | 26 | $this->load->spark('mage-api/0.0.1'); 27 | $update = array('name'=>'New Name'); 28 | var_dump( $this->mage_api->product_update( 'product_sku', $update ) ); 29 | 30 | How about getting a list (PHP array) of all customer groups? 31 | 32 | var_dump( $this->mage_api->customer_group_list() ); 33 | 34 | The "magic" is that _customer_group_list_ is translated to _customer_group.list_ API call and so on with all the methods on the API. 35 | 36 | ## Change Log 37 | 38 | ### 0.0.1 39 | 40 | * First release -------------------------------------------------------------------------------- /config/autoload.php: -------------------------------------------------------------------------------- 1 | _ci =& get_instance(); 31 | log_message('debug', 'Mage API Class Initialized'); 32 | 33 | 34 | //Load config 35 | $this->_ci->load->config('mage_api'); 36 | 37 | $this->_api_wsdl_uri = $this->_ci->config->item('magento_wsdl_uri'); 38 | $this->_api_username = $this->_ci->config->item('magento_api_username'); 39 | $this->_api_password = $this->_ci->config->item('magento_api_password'); 40 | 41 | if ( FALSE === $this->can_run() ) 42 | { 43 | $error_message = 'Mage API Class - PHP was not built with SOAP enabled.'; 44 | show_error($error_message); 45 | log_message('error', $error_message); 46 | } 47 | 48 | if( count($options) ){ 49 | $this->_api_options = array_merge($this->_api_options, $options); 50 | } 51 | 52 | $this->initialize(); 53 | } 54 | 55 | /** 56 | * Catches all undefined methods 57 | * @param string command to be run 58 | * @param array arguments to be passed 59 | * @return mixed 60 | */ 61 | public function __call($method, $args = array()) 62 | { 63 | $command = substr_replace($method, '.', strrpos($method, '_'), 1); 64 | 65 | log_message('debug', $command); 66 | log_message('debug', print_r($args, TRUE)); 67 | 68 | return $this->call( $command, $args ); 69 | } 70 | 71 | public function can_run() 72 | { 73 | return extension_loaded('soap'); 74 | } 75 | 76 | public function initialize() 77 | { 78 | $this->_api_instance = new SoapClient($this->_api_wsdl_uri, $this->_api_options); 79 | 80 | try{ 81 | 82 | $this->_api_session_key = $this->_api_instance->login($this->_api_username, $this->_api_password); 83 | 84 | }catch(SoapFault $soap_ex){ 85 | 86 | log_message('error', $soap_ex->getMessage()); 87 | show_error($this->_error_msg($soap_ex->getMessage())); 88 | 89 | }catch(Exception $ex){ 90 | 91 | log_message('error', $ex->getMessage()); 92 | show_error($this->_error_msg($ex->getMessage())); 93 | 94 | } 95 | } 96 | 97 | /** 98 | * Perform API call, also can be used "manually" 99 | * 100 | *@param string $command Command to be performed 101 | *@param optional array $args Call parameters 102 | *@return array Api call result 103 | */ 104 | public function call($command, $args = array()) 105 | { 106 | try{ 107 | return $this->_api_instance->call($this->_api_session_key, $command, $args); 108 | }catch(SoapFault $soap_ex){ 109 | 110 | log_message('error', $soap_ex->getMessage()); 111 | log_message('debug', $this->get_last_request()); 112 | 113 | show_error($this->_error_msg($soap_ex->getMessage())); 114 | 115 | }catch(Exception $ex){ 116 | 117 | log_message('error', $ex->getMessage()); 118 | log_message('debug', $this->get_last_request()); 119 | 120 | show_error($this->_error_msg($soap_ex->getMessage())); 121 | 122 | } 123 | return FALSE; 124 | } 125 | 126 | public function get_last_request() 127 | { 128 | return $this->_api_instance->__getLastRequest(); 129 | } 130 | 131 | protected function _error_msg($text) 132 | { 133 | return sprintf('Magento API ERROR: %s', $text); 134 | } 135 | 136 | } -------------------------------------------------------------------------------- /spark.info: -------------------------------------------------------------------------------- 1 | name: mage_api 2 | 3 | # This is the current version of this spark. All sparks should be in 4 | # x.x.x format. Validation will fail otherwise. 5 | version: 0.0.1 6 | 7 | # This is the version of CodeIgniter this spark is compatible up to. It should 8 | # be in x.x.x format 9 | compatibility: 2.0.0 10 | 11 | tags: ["magento", "api", "soap"] --------------------------------------------------------------------------------