-
- jeff.meyer

-
Total Posts: 51
Joined: 2008-11-24
|
I needed a faster way to make API calls, so I created the following little class to allow me to just call like:
$attributeSets = MageApi::call('product_attribute_set.list');
But I’m kind of new to using dynamic method arguments with PHP. I have to guess that the way I solved my call method below is a bit cumbersome with the switch statement (and obviously limiting as-is). Anyone know of a better way to achieve this?
Here’s the class…
class MageApi { protected static $api_user = API_USER; protected static $api_key = API_KEY; protected static $base_url = BASE_URL; protected static $api_path = '/api/soap/?wsdl'; protected static $proxy = false; protected static $session = false; public static function getProxy() { if (!self::$proxy) { self::$proxy = new SoapClient(self::$base_url.self::$api_path); } return self::$proxy; } public static function getSession() { if (!self::$session) { self::$session = self::getProxy()->login(self::$api_user, self::$api_key); } return self::$session; } public static function call() { if (count(func_get_args()) > 0) { $args = array(); foreach (func_get_args() as $i => $value) { $args[$i] = $value; } } else { return false; } switch (func_num_args()) { case "1": return self::getProxy()->call(self::getSession(), $args[0]); break; case "2": return self::getProxy()->call(self::getSession(), $args[0], $args[1]); break; case "3": return self::getProxy()->call(self::getSession(), $args[0], $args[1], $args[2]); break; case "4": return self::getProxy()->call(self::getSession(), $args[0], $args[1], $args[2], $args[3]); break; case "5": return self::getProxy()->call(self::getSession(), $args[0], $args[1], $args[2], $args[3], $args[4]); break; default: return false; break; } } }
|