tinyrocket/magento

此包已被废弃,不再维护。未建议替代包。

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

dev-master / 1.0.x-dev 2014-09-28 23:34 UTC

This package is not auto-updated.

Last update: 2020-01-20 09:40:42 UTC


README

一个简单且具有一定直观性的包,用于管理和交互Magento SOAP API。兼容Laravel 4和Magneto SOAP v1 & v2。

Build Status Latest Stable Version Total Downloads Latest Unstable Version License

注意: 此包仍处于开发测试阶段。建议不要在生产环境中使用。此包是为Laravel 4设计的,如果你需要Laravel 3版本的此包,请查看MatteoCastiglioni的包

安装

通过composer安装,请在需求中添加以下内容

"require": {
	...
	"tinyrocket/magento": "1.0.*"
	...
},

注意: 您可能需要将minimum-stability更改为dev

配置

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

到您的providers数组中

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

和到您的aliases数组中

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

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

php artisan config:publish tinyrocket/magento

设置SOAP连接

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

app/config/packages/tinyrocket/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类。这些类允许通过一些基本方法调用信息。

####对象集合

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

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

getFirstItem - 返回第一个响应项目

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

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

####对象

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兼容模式。