Created
May 16, 2013 19:35
-
-
Save poly-glot/5594418 to your computer and use it in GitHub Desktop.
Magento XMLRPC Api Client using ZendFramework
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $api = MagentoApi::get_instance(); | |
| $api->setHost('your_magento_url/api/xmlrpc/') | |
| ->setCredentials('your_magento_api_user','your_magento_api_key'); | |
| /*** | |
| * For list of Supported Method Please visit | |
| * http://www.magentocommerce.com/api/soap/catalog/catalog.html | |
| * | |
| * You can use API Method as {alias}_{method} e.g. product_info, product_list | |
| **/ | |
| $product = $api->product_info(1900)->getResponse(); | |
| var_dump($product); | |
| $api->logout(); | |
| class MagentoApi { | |
| protected static $instance = null; | |
| public $apiUser = ''; | |
| public $apiKey = ''; | |
| protected $host = null; | |
| protected $session = null; | |
| protected $client = null; | |
| protected $response = null; | |
| public static function get_instance() { | |
| if(is_null(self::$instance)) { | |
| self::$instance = new MagentoApi(); | |
| self::$instance->loadZendFramework(); | |
| } | |
| return self::$instance; | |
| } | |
| public function loadZendFramework() { | |
| if(!class_exists('Zend_XmlRpc_Client')) { | |
| require_once 'Zend/Loader.php'; | |
| require_once 'Zend/Loader/Autoloader.php'; | |
| Zend_Loader_Autoloader::getInstance(); | |
| } | |
| return $this; | |
| } | |
| public function setHost($host) { | |
| $this->host = $host; | |
| $this->client = new Zend_XmlRpc_Client($this->host); | |
| return $this; | |
| } | |
| public function setCredentials($apiUser, $apiKey) { | |
| $this->apiUser = $apiUser; | |
| $this->apiKey = $apiKey; | |
| return $this; | |
| } | |
| public function __call($method, $args) { | |
| if(is_null($this->session)) | |
| $this->session = $this->client->call("login", array('apiUser' => $this->apiUser, 'apiKey' => $this->apiKey)); | |
| if(false !== (strpos($method,'_'))) | |
| $method = str_replace('_', '.', $method); | |
| $this->response = $this->client->call('call', array($this->session, $method, $args)); | |
| return $this; | |
| } | |
| public function getResponse() { | |
| return $this->response; | |
| } | |
| public function logout() { | |
| if(!is_null($this->session)) | |
| $this->client->call('endSession', array($this->session)); | |
| return $this; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment