标签归档:api

Magento API 使用

到后台建立API用户,System->Web Services->SOAP XMP-RPC/Roles和Users。

内容来自:http://www.magentocommerce.com/api/soap/introduction.html

Introduction
The Magento SOAP v1 API provides you with the ability to manage your eCommerce stores by providing calls for working with resources such as customers, categories, products, and sales orders. It also allows you to manage shopping carts and inventory.

A SOAP v2 API version has been available since Magento 1.3, and a WS-I compliant version has been available since Magento 1.6. 从Magento 1.3开始,SOAP v2 API可用,从Magento1.6版本开始WS-I兼容版本可用。

Supported Types 支持的类型

The Magento API supports SOAP and XML-RPC, where SOAP is the default protocol. 支持SOAP和XML-RPC,SOAP作为默认的协议。

>SOAP

To connect to Magento SOAP web services, load the WSDL into your SOAP client from either of these URLs: 把WSDL加载到客户端以链接上Magento SOAP Web服务:

http://domain.com/api/soap/?wsdl
//由于默认使用soap,所以也可以使用如下链接
http://domain.com/api/?wsdl

As of v1.3, you may also use the following URL to access the Magento API v2, which has been added to improve compatibility with Java and .NET: Magento 1.3以后,可以使用如下URL使用Magento API v2:

http://magentohost/api/v2_soap?wsdl=1

>XML-RPC
To use XML-RPC, load the following URL into your XML-RPC client:

http://magentohost/api/xmlrpc/

The following PHP example shows how to make XML-RPC calls:例子

$client = new Zend_XmlRpc_Client('http://magentohost/api/xmlrpc/');

// If somestuff requires api authentification,
// we should get session token
$session = $client->call('login', array('apiUser', 'apiKey'));

$client->call('call', array($session, 'somestuff.method', array('arg1', 'arg2', 'arg3')));
$client->call('call', array($session, 'somestuff.method', 'arg1'));
$client->call('call', array($session, 'somestuff.method'));
$client->call('multiCall', array($session,
     array(
        array('somestuff.method', 'arg1'),
        array('somestuff.method', array('arg1', 'arg2')),
        array('somestuff.method')
     )
));

// If you don't need the session anymore
$client->call('endSession', array($session));

The XML-RPC only supports the version 1 of the Magento API. XML-RPC只支持 Magento API 版本1(说明未来主要是SOAP)

API Methods

The following table contains the API methods that can be called from your SOAP or XML-RPC client on the Magento v1 API.

Method Description Return Value
startSession() Start the API session and return session ID. string
endSession(sessionId) End the API session. boolean
login(apiUser, apiKey) Start the API session, return the session ID, and authorize the API user. string
call(sessionId, resourcePath,array arguments) Call the API resource that is allowed in the current session. See Note below. mixed
multiCall(sessionId, array calls,array options) Call the API resource’s methods that are allowed for current session. See Notes below. array
resources(sessionId) Return a list of available API resources and methods allowed for the current session. array
globalFaults(sessionId) Return a list of fault messages and their codes that do not depend on any resource. array
resourceFaults(sessionId, resourceName) Return a list of the specified resource fault messages, if this resource is allowed in the current session. array

The Magento SOAP API v2 does not support the call() and multiCall() methods, and instead provides a separate method for each API resource. SOAP API 版本2不支持call()和mutiCall()方法(这些方法非常不优雅,版本2提供了一种优雅的方法)

Global API Faults

Fault Code Fault Message
0 Unknown Error
1 Internal Error. Please see log for details.
2 Access denied.
3 Invalid api path.
4 Resource path is not callable.

SOAP API Version v2
Since Magento 1.3, version v2 of the SOAP API has also been available. The main difference between v1 and v2 is that instead of using methods call and multiCall, it has separate methods for each action. 主要不同是不使用call和muticall:

// SOAP  v1
$params = array(array(
    'status'=>array('eq'=>'pending'),
    'customer_is_guest'=>array('eq'=>'1'))
));
$result = $client->call($sessionId, 'sales_order.list', $params);

// v2
$params = array('filter' => array(
    array('key' => 'status', 'value' => 'pending'),
    array('key' => 'customer_is_guest', 'value' => '1')
));
$result = $client->salesOrderList($sessionId, $params);

Note that the WSDL for SOAP v1 and SOAP v2 are different. Note that in SOAP v1, customizing the API did not involve changing the WSDL. In SOAP v2, changes to the WSDL are required. 针对SOAP v1 and SOAP v2的WSDL是不同的。

You can configure the SOAP v2 API to be WS-I compliant in the system configuration menu. To do this, set Services > Magento Core API > WS-I Compliance to Yes. 可以在后台配置WS-I兼容。

Note that the WSDL for the SOAP v2 API is different when in WS-I compliant mode.

Using the WS-I compliant SOAP v2 API WSDL, it is easy to automatically generate client classes for Java, .NET, and other languages using standard libraries. 启用了WS-I兼容可以为Jave .NET等自动创建客户端类。

永久链接:http://blog.ifeeline.com/556.html