Magento2.0 SOAP API 示例
SOAP中认证方式有两种方式
-
使用
integrationAdminTokenServiceV1
接口通过后台登陆账号生成token
$request = new \SoapClient("http://bm.local.com/index.php/soap/?wsdl&services=integrationAdminTokenServiceV1", array("soap_version" => SOAP_1_2)); $token = $request->integrationAdminTokenServiceV1CreateAdminAccessToken(array("username"=>"admin", "password"=>"admin123")); $token = (array) $token;
-
直接通过后台
system->Integrations
创建账号赋予权限生成token
下面的示例除了第一个接口使用第一种token
生成方式验证,其他全部使用第二种方法验证
product类api
- 创建product
error_reporting(E_ALL);
ini_set('display_errors', 1);
# need to include
require('vendor/zendframework/zend-server/src/Client.php');
require('vendor/zendframework/zend-soap/src/Client.php');
require('vendor/zendframework/zend-soap/src/Client/Common.php');
$request = new \SoapClient("http://bm.local.com/index.php/soap/?wsdl&services=integrationAdminTokenServiceV1", array("soap_version" => SOAP_1_2));
$token = $request->integrationAdminTokenServiceV1CreateAdminAccessToken(array("username"=>"admin", "password"=>"admin123"));
$token = (array) $token;
$wsdlurl = 'http://bm.local.com/index.php/soap/default?wsdl&services=catalogProductRepositoryV1';
$opts = ['http' => ['header' => "Authorization: Bearer ".$token['result']];
$context = stream_context_create($opts);
$soapClient = new \Zend\Soap\Client($wsdlurl);
$soapClient->setSoapVersion(SOAP_1_2);
$soapClient->setStreamContext($context);
$productData=array (
'product' =>
array (
'sku' => 'TESTPRD005',
'name' => '1Women\'s Running - Pure Boost X Shoes',
'attributeSetId' => '4',
'price' => 90.25,
'status' => '1',
'visibility' => '4',
'typeId' => 'simple',
'createdAt' => '2018-06-14 15:20:55',
'updatedAt' => '2018-06-16 15:20:23',
'weight' => '2.5',
'tierPrices'=>array(
0=>array (
'customerGroupId' => '32000',
'qty' => 5,
'value' => 80,
),
1=>array (
'customerGroupId' => '32000',
'qty' => 10,
'value' => 79,
),
2=>array (
'customerGroupId' => '32000',
'qty' => 20,
'value' => 75,
),
),
'mediaGalleryEntries' =>
array (
0 =>
array (
'mediaType' => 'image',
'label' => 'Women\'s Running - Pure Boost X Shoes',
'position' => '1',
'disabled' => '',
'types' =>
array (
0 => 'image',
1 => 'small_image',
2 => 'thumbnail',
),
'content' =>
array (
'base64EncodedData' => base64_encode(file_get_contents("C:\Users\amanda.wang\Desktop\images\pro4.jpg")),
'type' => 'image/jpeg',
'name' => 'TESTPRD004_1.jpg',
),
),
1 =>
array (
'mediaType' => 'image',
'label' => 'Women\'s Running - Pure Boost X Shoes',
'position' => '1',
'disabled' => '',
'types' =>
array (),
'content' =>
array (
'base64EncodedData' => base64_encode(file_get_contents("C:\Users\amanda.wang\Desktop\images\pro1.jpg")),
'type' => 'image/jpeg',
'name' => 'TESTPRD004_2.jpg',
),
),
),
'customAttributes' =>
array (
0 =>
array (
'attributeCode' => 'description',
'value' => '<p>Lightweight and sleek, these women\'s running shoes are fueled by boost™ energy. The low-profile runners blend an energy-returning boost™ midsole with a STRETCHWEB outsole for a cushioned ride with terrific ground-feel. They feature a breathable mesh upper with a sock-like fit that offers all-around support. With a full boost™ midsole that keeps every stride charged with light, fast energy, the shoe has an upper that hovers over a free-floating arch.</p>',
),
1 =>
array (
'attributeCode' => 'short_description',
'value' => '<p>PURE BOOST X SHOES</p><p>NATURAL RUNNING SHOES WITH ARCH SUPPORT.</p>',
),
2 =>
array (
'attributeCode' => 'meta_title',
'value' => 'PURE BOOST X SHOES',
),
3 =>
array (
'attributeCode' => 'meta_keyword',
'value' => 'boost X, running, shoes, adidas',
),
4 =>
array (
'attributeCode' => 'meta_description',
'value' => 'NATURAL RUNNING SHOES WITH ARCH SUPPORT.',
),
5 =>
array (
'attributeCode' => 'tax_class_id',
'value' => '0',
),
6 =>
array (
'attributeCode' => 'category_ids',
'value' =>
array (
0 => '63',
),
),
7 =>
array (
'attributeCode' => 'special_price',
'value' =>70.25
),
10 =>
array (
'attributeCode' => 'cost',
'value' =>50
),
11 =>
array (
'attributeCode' => 'quantity_and_stock_status',
'value' =>array('qty'=>5000)
),
),
),
);
$result = $soapClient->catalogProductRepositoryV1save($productData);
function object_to_array($obj){
$_arr=is_object($obj)?get_object_vars($obj):$obj;
$arr=array();
foreach($_arr as $key=>$val){
$val=(is_array($val))||is_object($val)?object_to_array($val):$val;
$arr[$key]=$val;
}
return $arr;
}
$resultAry = object_to_array($result);
var_dump($resultAry);
- 产品上传图片
error_reporting(E_ALL);
ini_set('display_errors', 1);
# need to include
require('vendor/zendframework/zend-server/src/Client.php');
require('vendor/zendframework/zend-soap/src/Client.php');
require('vendor/zendframework/zend-soap/src/Client/Common.php');
$token = 'noc07mqeph5mg0ghc25xh78e002o7ahw';
$wsdlurl = 'http://bm.local.com/index.php/soap/default?wsdl&services=catalogProductAttributeMediaGalleryManagementV1';
$opts = ['http' => ['header' => "Authorization: Bearer " . $token]];
$context = stream_context_create($opts);
$soapClient = new \Zend\Soap\Client($wsdlurl);
$soapClient->setSoapVersion(SOAP_1_2);
$soapClient->setStreamContext($context);
$requestData = [
'id' => null,
'mediaType' => 'image',
'label' => 'Image Text',
'position' => 1,
"types" => [
"image",
],
'disabled' => false,
'content' => [
'base64EncodedData' => base64_encode(file_get_contents("C:\Users\amanda.wang\Desktop\images\pro1.jpg")),
'type' => 'image/jpeg',
'name' => 'picture_image_test_4.jpg'
]
];
$result = $soapClient->catalogProductAttributeMediaGalleryManagementV1Create(array('sku' => 'picture-test', 'entry' => $requestData));
var_dump($result);
- 获得所有category tree
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
# need to include
require('vendor/zendframework/zend-server/src/Client.php');
require('vendor/zendframework/zend-soap/src/Client.php');
require('vendor/zendframework/zend-soap/src/Client/Common.php');
$token = 'noc07mqeph5mg0ghc25xh78e002o7ahw';
$wsdlurl = 'http://bm.local.com/index.php/soap/default?wsdl&services=catalogCategoryManagementV1';
$opts = ['http' => ['header' => "Authorization: Bearer " . $token]];
$context = stream_context_create($opts);
$soapClient = new \Zend\Soap\Client($wsdlurl);
$soapClient->setSoapVersion(SOAP_1_2);
$soapClient->setStreamContext($context);
$result = $soapClient->catalogCategoryManagementV1getTree();
function objectToArray($obj){
$_arr=is_object($obj)?get_object_vars($obj):$obj;
$arr=array();
foreach($_arr as $key=>$val){
$val=(is_array($val))||is_object($val)?objectToArray($val):$val;
$arr[$key]=$val;
}
return $arr;
}
$resultAry = objectToArray($result);
var_dump($resultAry);
order类api
- 通过条件获取订单
error_reporting(E_ALL);
ini_set('display_errors', 1);
# need to include
require('vendor/zendframework/zend-server/src/Client.php');
require('vendor/zendframework/zend-soap/src/Client.php');
require('vendor/zendframework/zend-soap/src/Client/Common.php');
$token = 'noc07mqeph5mg0ghc25xh78e002o7ahw';
$wsdlurl = 'http://bm.local.com/index.php/soap/default?wsdl&services=salesOrderRepositoryV1';
$opts = ['http' => ['header' => "Authorization: Bearer ".$token]];
$context = stream_context_create($opts);
$soapClient = new \Zend\Soap\Client($wsdlurl);
$soapClient->setSoapVersion(SOAP_1_2);
$soapClient->setStreamContext($context);
$searchCriteria = [
'searchCriteria' => [
'filterGroups' => [
[
'filters' => [
[
'field' => 'increment_id',
'value' => '000000002',
'conditionType' => 'eq',
],
],
],
],
],
];
//$searchCriteria1 = [
// 'searchCriteria' => [
// 'filterGroups' => [
// [
// 'filters' => [
// [
// 'field' => 'status',
// 'value' => 'processing',
// 'conditionType' => 'eq',
// ],
// ],
// ],
// [
// 'filters' => [
// [
// 'field' => 'created_at',
// 'value' => '2018-04-26 00:00:00',
// 'conditionType' => 'gteq',
// ],
// ],
// ],
// [
// 'filters' => [
// [
// 'field' => 'created_at',
// 'value' => '2018-04-29 00:00:00',
// 'conditionType' => 'lteq',
// ],
// ],
// ],
// ],
//
// ],
//];
//echo "<pre>";
$result = $soapClient->salesOrderRepositoryV1getList($searchCriteria);
//$result = $soapClient->salesShipmentRepositoryV1Create();
echo "<pre>"; print_r($result);
- 修改订单状态
error_reporting(E_ALL);
ini_set('display_errors', 1);
# need to include
require('vendor/zendframework/zend-server/src/Client.php');
require('vendor/zendframework/zend-soap/src/Client.php');
require('vendor/zendframework/zend-soap/src/Client/Common.php');
$token = 'noc07mqeph5mg0ghc25xh78e002o7ahw';
$wsdlurl = 'http://bm.local.com/index.php/soap/default?wsdl&services=salesOrderManagementV1';
$opts = ['http' => ['header' => "Authorization: Bearer ".$token]];
$context = stream_context_create($opts);
$soapClient = new \Zend\Soap\Client($wsdlurl);
$soapClient->setSoapVersion(SOAP_1_2);
$soapClient->setStreamContext($context);
$commentData= [
'comment' => '',
'isCustomerNotified' => 0,
'parentId' => 2,
'status' => 'processing',
'isVisibleOnFront' => 0,
];
$requestData = ['id' => 2, 'statusHistory' => $commentData];
$result = $soapClient->salesOrderManagementV1addComment($requestData);
echo "<pre>"; print_r($result);
- 创建shipment
error_reporting(E_ALL);
ini_set('display_errors', 1);
# need to include
require('vendor/zendframework/zend-server/src/Client.php');
require('vendor/zendframework/zend-soap/src/Client.php');
require('vendor/zendframework/zend-soap/src/Client/Common.php');
$token = 'noc07mqeph5mg0ghc25xh78e002o7ahw';
$wsdlurl = 'http://bm.local.com/index.php/soap/default?wsdl&services=salesOrderRepositoryV1';
$opts = ['http' => ['header' => "Authorization: Bearer ".$token]];
$context = stream_context_create($opts);
$soapClient = new \Zend\Soap\Client($wsdlurl);
$soapClient->setSoapVersion(SOAP_1_2);
$soapClient->setStreamContext($context);
$searchCriteria = [
'searchCriteria' => [
'filterGroups' => [
[
'filters' => [
[
'field' => 'increment_id',
'value' => '000000003',
'conditionType' => 'eq',
],
],
],
],
],
];
$result = $soapClient->salesOrderRepositoryV1getList($searchCriteria);
function object_to_array($obj){
$_arr=is_object($obj)?get_object_vars($obj):$obj;
$arr=array();
foreach($_arr as $key=>$val){
$val=(is_array($val))||is_object($val)?object_to_array($val):$val;
$arr[$key]=$val;
}
return $arr;
}
$resultAry = object_to_array($result);
$resultVal = $resultAry['result']['items']['item'];
$items = $resultVal['items'];
$itemnew = array();
$totalQty=0;
if(isset($items['item'][0]) && count($items['item'][0]) > 0)
{
foreach($items['item'] as $val)
{
$val['orderItemId'] = $val['itemId'];
$val['qty'] = $val['qtyOrdered'];
$itemnew[] = $val;
$totalQty = $totalQty + $val['qtyOrdered'];
}
}else{
$val = $items['item'];
$val['orderItemId'] = $val['itemId'];
$val['qty'] = $val['qtyOrdered'];
$itemnew[] = $val;
$totalQty = $totalQty + $val['qtyOrdered'];
}
$items['item'] = $itemnew;
$trackSearchCriteria = [
'searchCriteria' => [
'filterGroups' => [
[
'filters' => [
[
'field' => 'order_id',
'value' => $resultVal['entityId'],
'conditionType' => 'eq',
],
],
],
],
],
];
$tracks=[
[
'carrierCode' => 'ups',
'orderId' => $resultVal['entityId'],
'title' => 'United Parcel Service',
'description' => null,
'trackNumber' => '1233443',
'parentId' => null,
'createdAt' => null,
'updatedAt' => null,
'qty' => null,
'weight' => null
]
];
$storeId = $resultVal['storeId'];
$customerId = $resultVal['customerId'];
$billingAddressId = $resultVal['billingAddressId'];
$shippingAddressId = $resultVal['billingAddressId'];
$totalWeight = $resultVal['weight'];
$shippingLabel = $resultVal['shippingDescription'];
$trackDescription = $resultVal['shippingDescription'];
$shipment_values['storeId'] = $storeId;
$shipment_values['totalWeight'] = $totalWeight;
$shipment_values['totalQty'] = $totalQty;
$shipment_values['emailSent'] = null;
$shipment_values['orderId'] = $resultVal['entityId'];
$shipment_values['customerId'] = $customerId;
$shipment_values['shippingAddressId'] = $shippingAddressId;
$shipment_values['billingAddressId'] = $billingAddressId;
$shipment_values['shipmentStatus'] = null;
$shipment_values['shippingLabel'] = $shippingLabel;
$shipment_values['items'] = $items;
$shipment_values['tracks'] = $tracks;
$shipment_values['comments'] = 'your order has been shipped';
$wsdlurl = 'http://bm.local.com/index.php/soap/default?wsdl&services=salesShipmentRepositoryV1';
$opts = ['http' => ['header' => "Authorization: Bearer ".$token]];
$context = stream_context_create($opts);
$soapClient = new \Zend\Soap\Client($wsdlurl);
$soapClient->setSoapVersion(SOAP_1_2);
$soapClient->setStreamContext($context);
$serviceArgs = array("entity" => $shipment_values);
$soapResponse = $soapClient->salesShipmentRepositoryV1save($serviceArgs);
var_dump($soapResponse);
- 添加运单号
track
error_reporting(E_ALL);
ini_set('display_errors', 1);
# need to include
require('vendor/zendframework/zend-server/src/Client.php');
require('vendor/zendframework/zend-soap/src/Client.php');
require('vendor/zendframework/zend-soap/src/Client/Common.php');
$token = 'noc07mqeph5mg0ghc25xh78e002o7ahw';
$wsdlurl = 'http://bm.local.com/index.php/soap/default?wsdl&services=salesShipmentRepositoryV1';
$opts = ['http' => ['header' => "Authorization: Bearer ".$token]];
$context = stream_context_create($opts);
$soapClient = new \Zend\Soap\Client($wsdlurl);
$soapClient->setSoapVersion(SOAP_1_2);
$soapClient->setStreamContext($context);
$searchCriteria = [
'searchCriteria' => [
'filterGroups' => [
[
'filters' => [
[
'field' => 'order_id',
'value' => '3',
'conditionType' => 'eq',
],
],
],
],
],
];
$result = $soapClient->salesShipmentRepositoryV1getList($searchCriteria);
function object_to_array($obj){
$_arr=is_object($obj)?get_object_vars($obj):$obj;
$arr=array();
foreach($_arr as $key=>$val){
$val=(is_array($val))||is_object($val)?object_to_array($val):$val;
$arr[$key]=$val;
}
return $arr;
}
$parentId = object_to_array($result)['result']['items']['item']['entityId'];
$wsdlurl = 'http://bm.local.com/index.php/soap/default?wsdl&services=salesShipmentTrackRepositoryV1';
$tracks=
[
'carrierCode' => 'ups',
'orderId' => 1,
'title' => 'United Parcel Service',
'description' => null,
'trackNumber' => '8989898',
'parentId' => $parentId,
'createdAt' => null,
'updatedAt' => null,
'qty' => null,
'weight' => null
];
$opts = ['http' => ['header' => "Authorization: Bearer ".$token]];
$context = stream_context_create($opts);
$soapClient = new \Zend\Soap\Client($wsdlurl);
$soapClient->setSoapVersion(SOAP_1_2);
$soapClient->setStreamContext($context);
$serviceArgs = array("entity" => $tracks);
//var_dump($shipment_values);exit;
$soapResponse = $soapClient->salesShipmentTrackRepositoryV1save($serviceArgs);
var_dump($soapResponse);
customer类api
- 通过ID获取customer
error_reporting(E_ALL);
ini_set('display_errors', 1);
# need to include
require('vendor/zendframework/zend-server/src/Client.php');
require('vendor/zendframework/zend-soap/src/Client.php');
require('vendor/zendframework/zend-soap/src/Client/Common.php');
$token = 'noc07mqeph5mg0ghc25xh78e002o7ahw';
$wsdlurl = 'http://bm.local.com/index.php/soap/default?wsdl&services=customerCustomerRepositoryV1';
$opts = ['http' => ['header' => "Authorization: Bearer ".$token]];
$context = stream_context_create($opts);
$soapClient = new \Zend\Soap\Client($wsdlurl);
$soapClient->setSoapVersion(SOAP_1_2);
$soapClient->setStreamContext($context);
$requestData = ['customerId' => 1];
$result = $soapClient->customerCustomerRepositoryV1getById($requestData);
echo "<pre>"; print_r($result);