Connecting to Magento via SOAP

In my earlier post I have shown how you can connect to Magento with REST api. In this post let us connect via SOAP.

The below class acts like a proxy to call the magento soap api.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
/**
 * 
 * @author Hari K T
 * 
 */
class My_Soap_Magento
{
    /**
     * 
     * Host name to connect
     * 
     * @var string
     * 
     */
    protected $hostname;

    /**
     * 
     * User name
     * 
     * @var string
     * 
     */
    protected $username;

    /**
     * 
     * API Key
     * 
     * @var string
     * 
     */
    protected $apikey;

    /**
     * 
     * Zend_Soap_Client
     * 
     * @var Zend_Soap_Client
     * 
     */
    protected $client;


    protected $session;

    /**
     * 
     * Constructor
     * 
     * @param string $hostname The host name
     * 
     * @param string $username The user name of the host
     * 
     * @param string $apikey The apikey of the host
     * 
     */
    public function __construct($hostname, $username, $apikey)
    {
        $this->hostname = $hostname;
        $this->username = $username;
        $this->apikey   = $apikey;
    }

    /**
     * 
     * Magic method, the methods are named on the basis of Magento SOAP api
     * You don't need to pass the session as the first argument, for convience.
     * 
     * @link http://www.magentocommerce.com/api/soap/introduction.html
     * 
     * @param string $function
     * 
     * @param mixed $args
     * 
     * @return string | null
     * 
     */
    public function __call($function, $args)
    {
        $session = $this->getSession();
        $params = array_merge(array($this->getSession()), $args);
        $result = call_user_func_array(array($this->getClient(), $function), $params);
        if ($result) {
            // I need to get as json
            return Zend_Json_Encoder::encode($result);
        }
        return null;
    }

    /**
     * 
     * Get the session from logging in to the Magento server
     * 
     * @return string
     * 
     */
    public function getSession()
    {
        if (! $this->session) {
            try {
                $this->session = $this->getClient()->login($this->username, $this->apikey);
            } catch (Exception $e) {

            }
        }
        return $this->session;
    }

    /**
     * 
     * If we already have a session, we can set the session so it don't 
     * need to login again and get the session. This helps to reduce the 
     * call for login . 
     * 
     * @see getSession()
     * 
     * @param string $session
     * 
     * @return IM_Soap_Magento
     * 
     */
    public function setSession($session)
    {
        $this->session = $session;
        return $this;
    }

    /**
     * 
     * Get the Zend_Soap_Client object
     * 
     * @return Zend_Soap_Client
     * 
     */
    public function getClient()
    {
        if (! $this->client) {
            $endpoint  = trim($this->hostname, '/') . '/api/v2_soap/?wsdl';
            $this->client = new Zend_Soap_Client(
                $endpoint
            );
        }
        return $this->client;
    }
}

In order to connect you want to create an object of the above class My_Soap_Magento. You need to get the username and api key from the magento host.

1
2
3
4
5
$object = new My_Soap_Magento(
    'the-magento-host',
    'username',
    'apikey'
);

Now you are done! You can connect to the soap api version 2 of Magento server.

Sample how to get the product information, category tree, category information, products list etc are shown below.

See we are not passing the session parameter.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
echo "Product Info";
$result = $proxy->catalogProductInfo(16);
echo $result;

echo "Category Tree";
$result = $proxy->catalogCategoryTree();
echo $result;

echo "Category Info";
$result = $proxy->catalogCategoryInfo(10);
echo $result;

echo 'Products List';
$result = $proxy->catalogProductList();
echo $result;

magento, php, soap

Comments