forked from magento/devdocs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_your_own_api.html
605 lines (537 loc) · 22.4 KB
/
create_your_own_api.html
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
---
layout: m1x_soap
title: Create Your Own API
---
<ul>
<li><a href='#CreateYourOwnAPI-CreatingaCustomAPIorExtendingtheCoreAPI'>Creating a Custom API or Extending the Core API</a>
<ul>
<li><a href='#CreateYourOwnAPI-1.CreatinganXMLFilethatWillDefinetheAPIResource'>1. Creating an XML File that Will Define the API Resource</a></li>
<li><a href='#CreateYourOwnAPI-2.AddingaResourceNamedCustomer'>2. Adding a Resource Named Customer</a></li>
<li><a href='#CreateYourOwnAPI-3.AddingFaults'>3. Adding Faults</a></li>
<li><a href='#CreateYourOwnAPI-4.DescribingtheAccessControlList-ACL-fortheResource'>4. Describing the Access Control List (ACL) for the Resource</a></li>
<li><a href='#CreateYourOwnAPI-5.CreatingPHPCode'>5. Creating PHP Code</a></li>
</ul>
<ul>
<li><a href='#CreateYourOwnAPI-CreatingaCustomAdapter'>Creating a Custom Adapter</a>
<ul>
<li><a href='#CreateYourOwnAPI-CommonErrorMessages'>Common Error Messages</a></li>
</ul>
</ul>
</li>
</ul>
</li>
</ul></div>
<h1><a name="CreateYourOwnAPI-CreatingaCustomAPIorExtendingtheCoreAPI"></a>Creating a Custom API or Extending the Core API</h1>
<p> The Core API allows you to manage a set of common resources used in Magento. However, you may choose to have your own set of resources to manage, or you may wish to extend the Core API to handle additional resources. </p>
<p> This tutorial leads you through the process of creating a custom API for a customer module that handles basic customer information. </p>
<p> <b>Note:</b> This tutorial applies to v1 of the API. </p>
<p> To learn more about the Core API, to read Magento Core API calls. </p>
<p> For general information about the Magento API, go to the Introduction.</p>
<h3><a name="CreateYourOwnAPI-1.CreatinganXMLFilethatWillDefinetheAPIResource"></a>1. Creating an XML File that Will Define the API Resource</h3>
<p> Create a file named <b>api.xml</b> in the /etc folder in the customer module. Start with the empty structure, as follows: </p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<div id="root">
<pre class="theme: Default; brush: java; gutter: false"><config>
<api>
<resources>
</resources>
<acl>
<resources>
<all>
</all>
</resources>
</acl>
</api>
</config></pre>
</div>
</div></div>
<h3><a name="CreateYourOwnAPI-2.AddingaResourceNamedCustomer"></a>2. Adding a Resource Named Customer</h3>
<p> Add an element named customer in the <resources> element. Add a <methods> element, with elements for list, create, info, update and remove methods for customer resource. </p>
<p> Note that: </p>
<ul>
<li>list will return all customers</li>
<li>create will create a new customer</li>
<li>info will return data on a specified customer</li>
<li>update will update data on a specified customer</li>
<li>remove will delete data on a specified customer</li>
</ul>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<div id="root">
<pre class="theme: Default; brush: java; gutter: false"><config>
<api>
....
<resources>
<customer translate="title" module="customer">
<title>Customer Resource</title>
<methods>
<list translate="title" module="customer">
<title>Retrive customers</title>
</list>
<create translate="title" module="customer">
<title>Create customer</title>
</create>
<info translate="title" module="customer">
<title>Retrieve customer data</title>
</info>
<update translate="title" module="customer">
<title>Update customer data</title>
</update>
<delete>
<title>Delete customer</title>
</delete>
</methods>
<faults module="customer">
</faults>
</customer>
</resources>
....
</api>
</config></pre>
</div>
</div></div>
<h3><a name="CreateYourOwnAPI-3.AddingFaults"></a>3. Adding Faults</h3>
<p> The resource can return some faults, so add a <faults> element in the customer element, and list the various faults. </p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<div id="root">
<pre class="theme: Default; brush: java; gutter: false"><config>
<api>
....
<resources>
<customer translate="title" module="customer">
....
<faults module="customer"> <!-- module="customer" specifies the module which will be used for translation. -->
<data_invalid> <!-- if we get invalid input data for customers -->
<code>100</code >
<!-- we cannot know all the errors that can appear, their details can be found in error message for call -->
<message>Invalid customer data. Details in error message.</message>
</data_invalid>
<filters_invalid>
<code>101</code >
<message>Invalid filters specified. Details in error message.</message>
</filters_invalid>
<not_exists>
<code>102</code >
<message>Customer doesn't exist.</message>
</not_exists>
<not_deleted>
<code>103</code >
<message>Customer was not deleted. Details in error message.</message>
</not_deleted>
</faults>
</customer>
</resources>
....
</api>
</config></pre>
</div>
</div></div>
<h3><a name="CreateYourOwnAPI-4.DescribingtheAccessControlList-ACL-fortheResource"></a>4. Describing the Access Control List (ACL) for the Resource</h3>
<p> In order to prevent unauthorized access to our custom API, you must first list the resources that are restricted within the <acl> element. </p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<div id="root">
<pre class="theme: Default; brush: java; gutter: false"><config>
<api>
....
<acl>
<resources>
<customer translate="title" module="customer">
<title>Customers</title>
<list translate="title" module="customer">
<title>View All</title>
</list>
<create translate="title" module="customer">
<title>Create</title>
</create>
<info translate="title" module="customer">
<title>Get Info</title>
</info>
<update translate="title" module="customer">
<title>Update</title>
</update>
<delete translate="title" module="customer">
<title>Delete</title>
</delete>
</customer>
</resources>
</acl>
</api>
</config></pre>
</div>
</div></div>
<p> Then, map ACL resources to API resource methods by adding an <acl> element to each part of the resource that needs restricting: </p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<div id="root">
<pre class="theme: Default; brush: java; gutter: false"><config>
<api>
<resources>
<customer translate="title" module="customer">
<title>Customer Resource</title>
<acl>customer</acl>
<methods>
<list translate="title" module="customer">
<title>Retrive customers</title>
<acl>customer/list</acl>
</list>
<create translate="title" module="customer">
<title>Create customer</title>
<acl>customer/create</acl>
</create>
<info translate="title" module="customer">
<title>Retrieve customer data</title>
<acl>customer/info</acl>
</info>
<update translate="title" module="customer">
<title>Update customer data</title>
<acl>customer/update</acl>
</update>
<delete>
<title>Delete customer</title>
<acl>customer/delete</acl>
</delete>
</methods>
....
</customer>
</resources>
....
</api>
</config></pre>
</div>
</div></div>
<h3><a name="CreateYourOwnAPI-5.CreatingPHPCode"></a>5. Creating PHP Code</h3>
<p> Next, write some PHP code to access the resources. Start by creating a class called Mage_Customer_Model_Customer_Api that extends Mage_Api_Model_Resource_Abstract. Save it into a file called <b>api.php</b>. </p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<div id="root">
<pre class="theme: Default; brush: java; gutter: false">class Mage_Customer_Model_Customer_Api extends Mage_Api_Model_Resource_Abstract
{
public function create($customerData)
{
}
public function info($customerId)
{
}
public function items($filters)
{
}
public function update($customerId, $customerData)
{
}
public function delete($customerId)
{
}
}</pre>
</div>
</div></div>
<p> Note that you cannot create method “list” because it’s a PHP keyword, so instead the method is named items. In order to make this work, add a <method> element into the <list> element in <b>api.xml</b>, as shown below.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<div id="root">
<pre class="theme: Default; brush: java; gutter: false"><config>
<api>
<resources>
<customer translate="title" module="customer">
<model>customer/api</model> <!-- our model -->
<title>Customer Resource</title>
<acl>customer</acl>
<methods>
<list translate="title" module="customer">
<title>Retrive customers</title>
<method>items</method> <!-- we have another method name inside our resource -->
<acl>customer/list</acl>
</list>
....
</methods>
....
</resources>
....
</api>
</config></pre>
</div>
</div></div>
<p> Now add some simple functionality to the Mage_Customer_Model_Api methods you created. </p>
<p> Create a customer: </p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<div id="root">
<pre class="theme: Default; brush: php; gutter: false">public function create($customerData)
{
try {
$customer = Mage::getModel('customer/customer')
->setData($customerData)
->save();
} catch (Mage_Core_Exception $e) {
$this->_fault('data_invalid', $e->getMessage());
// We cannot know all the possible exceptions,
// so let's try to catch the ones that extend Mage_Core_Exception
} catch (Exception $e) {
$this->_fault('data_invalid', $e->getMessage());
}
return $customer->getId();
}</pre>
</div>
</div></div>
<p> Retrieve customer info: </p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<div id="root">
<pre class="theme: Default; brush: php; gutter: false">public function info($customerId)
{
$customer = Mage::getModel('customer/customer')->load($customerId);
if (!$customer->getId()) {
$this->_fault('not_exists');
// If customer not found.
}
return $customer->toArray();
// We can use only simple PHP data types in webservices.
}</pre>
</div>
</div></div>
<p> Retrieve list of customers using filtering: </p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<div id="root">
<pre class="theme: Default; brush: php; gutter: false">public function items($filters)
{
$collection = Mage::getModel('customer/customer')->getCollection()
->addAttributeToSelect('*');
if (is_array($filters)) {
try {
foreach ($filters as $field => $value) {
$collection->addFieldToFilter($field, $value);
}
} catch (Mage_Core_Exception $e) {
$this->_fault('filters_invalid', $e->getMessage());
// If we are adding filter on non-existent attribute
}
}
$result = array();
foreach ($collection as $customer) {
$result[] = $customer->toArray();
}
return $result;
}</pre>
</div>
</div></div>
<p> Update a customer: </p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<div id="root">
<pre class="theme: Default; brush: php; gutter: false">public function update($customerId, $customerData)
{
$customer = Mage::getModel('customer/customer')->load($customerId);
if (!$customer->getId()) {
$this->_fault('not_exists');
// No customer found
}
$customer->addData($customerData)->save();
return true;
}</pre>
</div>
</div></div>
<p> Delete a customer: </p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<div id="root">
<pre class="theme: Default; brush: php; gutter: false">public function delete($customerId)
{
$customer = Mage::getModel('customer/customer')->load($customerId);
if (!$customer->getId()) {
$this->_fault('not_exists');
// No customer found
}
try {
$customer->delete();
} catch (Mage_Core_Exception $e) {
$this->_fault('not_deleted', $e->getMessage());
// Some errors while deleting.
}
return true;
}</pre>
</div>
</div></div>
<h2><a name="CreateYourOwnAPI-CreatingaCustomAdapter"></a>Creating a Custom Adapter</h2>
<p> In order to create custom webservice adapter, implement the Mage_Api_Model_Server_Adapter_Interface, which is shown below.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<div id="root">
<pre class="theme: Default; brush: php; gutter: false">interface Mage_Api_Model_Server_Adapter_Interface
{
/**
* Set handler class name for webservice
*
* @param string $handler
* @return Mage_Api_Model_Server_Adapter_Interface
*/
function setHandler($handler);
/**
* Retrive handler class name for webservice
*
* @return string [basc]
*/
function getHandler();
/**
* Set webservice api controller
*
* @param Mage_Api_Controller_Action $controller
* @return Mage_Api_Model_Server_Adapter_Interface
*/
function setController(Mage_Api_Controller_Action $controller);
/**
* Retrive webservice api controller
*
* @return Mage_Api_Controller_Action
*/
function getController();
/**
* Run webservice
*
* @return Mage_Api_Model_Server_Adapter_Interface
*/
function run();
/**
* Dispatch webservice fault
*
* @param int $code
* @param string $message
*/
function fault($code, $message);
} // Class Mage_Api_Model_Server_Adapter_Interface End</pre>
</div>
</div></div>
<p> Here is an example implementation for XML-RPC:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<div id="root">
<pre class="theme: Default; brush: php; gutter: false">class Mage_Api_Model_Server_Adapter_Customxmlrpc
extends Varien_Object
implements Mage_Api_Model_Server_Adapter_Interface
{
/**
* XmlRpc Server
*
* @var Zend_XmlRpc_Server
*/
protected $_xmlRpc = null;
/**
* Set handler class name for webservice
*
* @param string $handler
* @return Mage_Api_Model_Server_Adapter_Xmlrpc
*/
public function setHandler($handler)
{
$this->setData('handler', $handler);
return $this;
}
/**
* Retrive handler class name for webservice
*
* @return string
*/
public function getHandler()
{
return $this->getData('handler');
}
/**
* Set webservice api controller
*
* @param Mage_Api_Controller_Action $controller
* @return Mage_Api_Model_Server_Adapter_Xmlrpc
*/
public function setController(Mage_Api_Controller_Action $controller)
{
$this->setData('controller', $controller);
return $this;
}
/**
* Retrive webservice api controller
*
* @return Mage_Api_Controller_Action
*/
public function getController()
{
return $this->getData('controller');
}
/**
* Run webservice
*
* @param Mage_Api_Controller_Action $controller
* @return Mage_Api_Model_Server_Adapter_Xmlrpc
*/
public function run()
{
$this->_xmlRpc = new Zend_XmlRpc_Server();
$this->_xmlRpc->setClass($this->getHandler());
$this->getController()->getResponse()
->setHeader('Content-Type', 'text/xml')
->setBody($this->_xmlRpc->handle());
return $this;
}
/**
* Dispatch webservice fault
*
* @param int $code
* @param string $message
*/
public function fault($code, $message)
{
throw new Zend_XmlRpc_Server_Exception($message, $code);
}
} // Class Mage_Api_Model_Server_Adapter_Customxmlrpc End</pre>
</div>
</div></div>
<p><b>Notes:</b> The setHandler, getHandler, setController and getController methods have a simple implementation that uses the Varien_Object getData and setData methods.</p>
<p> The run and fault methods are a native implementation for an XML-RPC webservice. The run method defines webservice logic in this adapter for creating an XML-RPC server to handle XML-RPC requests. </p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<div id="root">
<pre class="theme: Default; brush: php; gutter: false">public function run()
{
$this->_xmlRpc = new Zend_XmlRpc_Server();
$this->_xmlRpc->setClass($this->getHandler());
$this->getController()->getResponse()
->setHeader('Content-Type', 'text/xml')
->setBody($this->_xmlRpc->handle());
return $this;
}</pre>
</div>
</div></div>
<p> The “fault” method allows you to send fault exceptions for XML-RPC service when handling requests. </p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<div id="root">
<pre class="theme: Default; brush: php; gutter: false">public function fault($code, $message)
{
throw new Zend_XmlRpc_Server_Exception($message, $code);
}</pre>
</div>
</div></div>
<h3><a name="CreateYourOwnAPI-CommonErrorMessages"></a>Common Error Messages</h3>
<p> The following are common error messages that you might receive when creating your own custom API. </p>
<p><b>Invalid API path</b></p>
<p> This error occurs when the methods listed in the <b>api.xml</b> file do not correspond exactly with those used in your PHP file. </p>
<p> For example, in your api.xml file, you might have this: </p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<div id="root">
<pre class="theme: Default; brush: java; gutter: false"><config>
<api>
<resources>
<checkout_cart translate="title" module="checkout">
<model>checkout/cart_api</model>
<title>Cart API</title>
<methods>
<list translate="title" module="checkout">
<title>Retrieve cart data</title>
<method>info</method>
</list>
</methods>
</checkout_cart>
</resources>
...
</api>
</config></pre>
</div>
</div></div>
<p> You should have a corresponding info method in your PHP file. </p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<div id="root">
<pre class="theme: Default; brush: php; gutter: false">class Mage_Checkout_Model_Cart_Api extends Mage_Cart_Model_Api_Resource
{
public function info()
{
...
}
}</pre>
</div>
</div></div>
<p> If you are missing this method, the error “Invalid api path” will be returned. </p>
<?php include(__ROOT__ . 'footer.php'); ?>