alirezamirsepassi/magento-soap-api

使用SOAP v1和v2的简单Magento集成。

dev-master / 1.0.x-dev 2017-11-23 18:38 UTC

This package is auto-updated.

Last update: 2024-09-29 05:08:02 UTC


README

这是一个简单且易于理解的包,用于管理和与Magento SOAP API交互。兼容Laravel 4和Magneto SOAP v1 & v2。

安装

要使用composer安装,请将以下内容添加到您的需求中

"require": {
	...
	"AlirezaMirsepassi/magento-soap-api": "1.0.*"
	...
},

配置

将以下内容添加到您的Laravel应用程序配置(app/config/app.php)中

到您的 providers 数组中

'providers' => array(
    ...
	'AlirezaMirsepassi\Magento\MagentoServiceProvider',
	...
),

和到您的 aliases 数组中

'aliases' => array(
    ...
	'Magento' => 'AlirezaMirsepassi\Magento\Facades\Magento',
	...
),

通过在CLI中运行以下命令来发布包配置文件

php artisan config:publish AlirezaMirsepassi/magento

设置SOAP连接

开始使用Magento集成的最快方法是添加您的连接到新发布的配置文件。该文件位于

app/config/packages/AlirezaMirsepassi/magento/config.php

您可以为保存的连接数量设置无限制,但您应该设置一个默认配置来处理回退。在配置文件中,使用您的Magento SOAP信息设置以下内容。

注意:使用您商店的基本URL,而不是SOAP端点的URL。(例如,使用http://magentostore.com而不是http://magentostore.com/api/soap?wsdl

'connections' => [
    ...
	'default'	=>	[
		'site_url'	=>	'http://magentohost',
		'user'		=>	'',
		'key'		=>	'',
		'version'   =>  'v2'
	],
	...
]

第一个参数定义了连接的名称,应该是唯一的。API版本是可选的,默认情况下将适用于所有连接的v2,除非您设置其他版本。

##使用方法

Magento网站上提供了可能方法的详尽列表

用于与SOAP API交互有两种基本方法

对于 SOAP v2

Magento::call()->someSoapRequest($requestParams)

// Example
Magento::call()->customerCustomerList()

对于 SOAP v1

Magento::get('some.method', array($requestParams))

//Example
Magento::get('customer.list')

或者,您可以直接调用 SOAP v2 请求的方法

Magento::someSoapRequest($requestParams)

// Example
 $customerParams = array(
	'email' 		=> 'customer@example.org', 
	'firstname' 	=> 'Dough', 
	'lastname' 		=> 'Deeks', 
	'password' 		=> 'password', 
	'website_id' 	=> 1, 
	'store_id' 		=> 1, 
	'group_id' 		=> 1
);
$customerId = Magento::customerCustomerCreate($customerParams)

###处理结果

为了使使用SOAP API更加直观,一些请求结果以数据对象和集合的形式返回,灵感来自Magento中的Varien_ObjectVarien_Object_Collection类。这些类允许使用一些基本方法调用信息。

####对象集合 对于返回一组项目的SOAP响应,结果以包含单个对象的集合对象返回。这些集合有四个基本方法。

getCollection() - 返回所有项目作为一组对象

getCount() - 返回集合中的项目数量

getFirstItem() - 返回第一个响应项目

getLastItem() - 返回最后一个响应项目

foreach ( Magento::salesOrderList()->getCollection() as $order ) {
    // Do stuff
}

####对象 对于返回单个数组项的SOAP响应,或者在迭代响应集合时,使用MagnetoObject。此对象带有几个方法,对于Magento开发人员应该很熟悉

getData(optional $key) - 要么返回对象的所有值,要么返回与提供的键匹配的单个值。

getId() - 返回给定响应对象的主键

与一个 Varien_Object 一样,您还可以使用魔术获取器从对象中提取信息。例如,您可以使用以下两种方法返回订单对象的incrementId

foreach ( Magento::salesOrderList()->getCollection() as $order ) {
    
    // with data
    echo $order->getData('increment_id')
    
    // with magic getter
    echo $order->getIncrementId()
}

####单个结果 对于返回单个值或布尔值的SOAP响应,结果以字符串/整数/布尔值返回

// will return an integer
echo Magento::customerCustomerCreate($customerParams)

### 多重连接工作 您可以选择使用连接到多个Magento网站。这可以通过向包配置中添加一个辅助连接或实时创建一个连接来实现。连接会被存储,所以一旦注册,您可以通过引用其唯一的标识符或新创建的连接对象来继续使用该连接。

实时创建一个连接

$connection = Magento::createConnection($name, $url, $user, $key, $version)

使用存储的连接

// SOAP v2
$orders = Magento::call($connection)->salesOrderList();
$orders = Magento::call('unique_identifier')->salesOrderList();

// SOAP v1
$customers = Magento::get('customers.list', null, $connection)
$customers = Magento::get('customers.list', null, 'unique_identifier')

以编程方式注册一个连接

Magento::createAndRegisterConnection($name, $url, $user, $key, $version)

查看可用的连接列表

print_r( Magento::getAvailableConnections() )

设置主要连接 默认情况下,除非显式传递给SOAP调用,否则在执行所有调用时使用配置文件中找到的默认连接。要更改默认使用的连接,您可以使用以下方法:

Magento::setPrimaryConnection('unique_identifier')

// Then use for subsequent calls
Magento::salesOrderList()

查看当前的主要连接

echo Magento::getPrimaryConnection()

从内存中删除一个连接

Magento::unregister('unique_identifier')

创建一个临时连接

$connection = Magento::createAndForgetConnection($name, $url, $user, $key, $version)

// Then reference it in the call
$orders = Magento::call($connection)->salesOrderList()

### 辅助工具

获取对象/集合的魔法获取器 虽然只是用来返回数据,但您可以使用以下方法获取给定对象或集合的可用函数列表

// For a collection
echo Magento::call()->customerCustomerList()->getFunctions();

// For an object
$customer->getFunctions();

这将返回类似以下的内容

Array
(
    [0] => getCustomerId
    [1] => getCreatedAt
    [2] => getUpdatedAt
    [3] => getStoreId
    [4] => getWebsiteId
    [5] => getCreatedIn
    [6] => getEmail
    [7] => getFirstname
    [8] => getLastname
    [9] => getGroupId
    [10] => getDob
    [11] => getPasswordHash
)

测试您的SOAP连接 默认返回布尔值,但可以通过将第二个参数标记为true来返回头部信息。

var_dump( Magento::testConnection('unique_identifier', $returnHeaders = false) )

获取Magento版本 返回默认连接或传入连接的构建版本。

// Example return Community 1.9.0.0
var_dump( Magento::getMagentoVersion(optional $connection) )

### XML支持 目前,不支持XML请求和响应。但这计划在未来版本中实现。

### WS-I合规模式 目前没有强制执行WS-I合规模式的方法。