Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Created simple API calling class, but need php advice
 
jeff.meyer
Member
 
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_userself::$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;
        
}
    }
}
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top