Magento 产品的 API
Magento Product's API
我的商店有 magento2,我想使用 REST API 获取所有 configurable/simple 产品及其子(变体)产品。我能够在单个 REST API 调用中获取所有产品,但它没有为我提供可配置产品的子项。
我需要如下所示的可配置产品:
{
"id":1,
"parent_id":1,
"name":"myProduct",
"is_active":true,
.................
"children_data":[
{
"id":1,
"parent_id":1,
"name":"myProduct",
"is_active":true,
.................
},
{
"id":1,
"parent_id":1,
"name":"myProduct",
"is_active":true,
.................
}
]
}
您需要使用以下 REST API 来获取子数据
GET /V1/configurable-products/{sku}/children
回复:
[
{
"sku": "MH01-XS-Black",
"name": "Chaz Kangeroo Hoodie-XS-Black",
"attribute_set_id": 9,
"price": 52,
"status": 1,
"type_id": "simple",
"created_at": "2015-11-20 08:12:24",
"updated_at": "2015-11-20 08:12:24",
"weight": 1,
"extension_attributes": [],
"product_links": [],
"tier_prices": [],
"custom_attributes": [
{
"attribute_code": "required_options",
"value": "0"
},
{
"attribute_code": "has_options",
"value": "0"
},
{
"attribute_code": "tax_class_id",
"value": "2"
},
{
"attribute_code": "image",
"value": "/m/h/mh01-black_main.jpg"
},
{
"attribute_code": "category_ids",
"value": [
"2",
"15",
"36"
]
},
{
"attribute_code": "size",
"value": "167"
},
{
"attribute_code": "color",
"value": "49"
},
{
"attribute_code": "small_image",
"value": "/m/h/mh01-black_main.jpg"
},
{
"attribute_code": "thumbnail",
"value": "/m/h/mh01-black_main.jpg"
},
{
"attribute_code": "url_key",
"value": "chaz-kangeroo-hoodie-xs-black"
},
{
"attribute_code": "msrp_display_actual_price_type",
"value": "0"
}
]
},
...
]
并且如果您需要所有可配置选项并调用以下 API
GET /V1/configurable-products/{sku}/options/all
回复:
[
{
"id": 3,
"attribute_id": "90",
"label": "Color",
"position": 0,
"values": [
{
"value_index": 49
},
{
"value_index": 52
},
{
"value_index": 56
}
],
"product_id": 67
},
{
"id": 2,
"attribute_id": "137",
"label": "Size",
"position": 0,
"values": [
{
"value_index": 167
},
{
"value_index": 168
},
{
"value_index": 169
},
{
"value_index": 170
},
{
"value_index": 171
}
],
"product_id": 67
}
]
如何使用 REST APIs
1. 身份验证
Magento 允许开发人员在配置文件 webapi.xml
中定义网络 API 资源及其权限。这里有更多关于公开 services as Web APIs.
的细节
在进行网络 API 调用之前,您必须验证您的身份并具有访问 API 资源所需的权限(授权)。身份验证允许 Magento 识别调用者的用户类型。根据用户(管理员、集成、客户或来宾)的访问权限,确定 API 呼叫的资源可访问性。
2。构建请求
每个 Magento 网络 API 调用包含以下元素的组合:
2.1 HTTP 动词
GET
。请求传输目标的当前表示
资源。如果省略动词,则默认为 GET
。
PUT
。要求
目标资源的状态被创建或替换为
由包含在请求消息中的表示定义的状态
有效载荷。
POST
。请求源服务器接受
请求中包含的表示作为要由处理的数据
目标资源。
DELETE
。请求源服务器删除
目标资源。
2.2 ConfigurableProduct 的端点
GET /V1/configurable-products/:sku/children
DELETE /V1/configurable-products/:sku/children/:childSku
PUT /V1/configurable-products/variation
POST /V1/configurable-products/:sku/child
GET /V1/configurable-products/:sku/options/:id
GET /V1/configurable-products/:sku/options/all
POST /V1/configurable-products/:sku/options
PUT /V1/configurable-products/:sku/options/:id
DELETE /V1/configurable-products/:sku/options/:id
2.3 HTTP headers
To specify an HTTP header in a cURL command, use the -H
or --header
option.
在您的网络 API 调用中指定以下一个或多个 HTTP header:
.---------------.-----------------------------------.
| HTTP header | Syntax |
|---------------|-----------------------------------|
| Authorization | Authorization: Bearer <TOKEN> |
| Accept | Accept: application/<FORMAT> |
| Content-Type | Content-Type:application/<FORMAT> |
'---------------'-----------------------------------'
其中 <TOKEN>
是由 Magento 令牌服务 return 提供的身份验证令牌。请参阅身份验证。
2.4 调用载荷
调用负载是您随请求提供的一组输入参数和属性。 API 操作具有 必需的 和 可选的 输入。
您在 URI 中指定输入参数。
您在 JSON 或 XML-formatted 请求中指定输入属性 body。
2.5构造请求
准备 Authorization
、Accept
和 Content-Type
header 以传递给请求 object。使用由 Magento 令牌服务 return 提供的授权令牌。
$token = 'token';
$httpHeaders = new \Zend\Http\Headers();
$httpHeaders->addHeaders([
'Authorization' => 'Bearer ' . $token,
'Accept' => 'application/json',
'Content-Type' => 'application/json'
]);
打开Magento/ConfigurableProduct/etc/webapi.xml configuration file and find the getChildren
method from Magento\ConfigurableProduct\Api\LinkManagementInterface界面
将 headers、URI 和方法设置为请求 object。
$request = new \Zend\Http\Request();
$request->setHeaders($httpHeaders);
$request->setUri('http://yourdomain.com/rest/V1/configurable-products/:sku/children');
$request->setMethod(\Zend\Http\Request::METHOD_GET);
$params = new \Zend\Stdlib\Parameters([
'sku' => 'sku-needed-for-example'
]);
$request->setQuery($params);
准备 HTTP Curl 客户端 object 并将请求 object 传递给 Client::send()
方法。
$client = new \Zend\Http\Client();
$options = [
'adapter' => 'Zend\Http\Client\Adapter\Curl',
'curloptions' => [CURLOPT_FOLLOWLOCATION => true],
'maxredirects' => 0,
'timeout' => 30
];
$client->setOptions($options);
$response = $client->send($request);
此请求 return 是 JSON
格式的所有孩子的列表。您还可以通过更改请求的 Accept
header 来指定 XML
格式。
3。使用 cURL 来 运行 请求
cURL 是一个 command-line 工具,可让您从命令行或 shell 脚本传输和接收 HTTP 请求和响应。它可用于 Linux 分布、Mac OS X 和 Windows.
要使用 cURL 来 运行 您的 REST web API 调用,请使用 cURL 命令语法来构建 cURL 命令。
要在调用中创建端点,请将您在步骤 2.5 构造请求中构造的 REST URI 附加到此 URL:
https://<MAGENTO_HOST_OR_IP>/<MAGENTO_BASE_INSTALL_DIR>/rest/
有关 cURL 命令选项的完整列表,请参阅 curl.1 the man page。
4。查看响应
每个网站 API 调用 return 一个 HTTP 状态代码和一个响应负载。当发生错误时,响应 body 也 return 是一条错误消息。
HTTP 状态码
每个网站 API 调用 return 一个反映请求结果的 HTTP 状态代码:
.===========.=================.=================================================.
| HTTP code | Meaning | Description |
|===========|=================|=================================================|
| 200 | Success | M2 return HTTP 200 to the caller upon success. |
|-----------|-----------------|-------------------------------------------------|
| | | If service implementation throws either |
| | | `Magento_Service_Exception` or its derivative, |
| | | the framework returns a HTTP 400 with a error |
| | | response including the service-specific error |
| 400 | Bad Request | code and message. This error code could |
| | | indicate a problem such as a missing required |
| | | parameter or the supplied data didn't pass |
| | | validation. |
|-----------|-----------------|-------------------------------------------------|
| 401 | Unauthorized | The caller was not authorized to perform the |
| | | request. For example, the request included an |
| | | invalid token or a user with customer |
| | | permissions attempted to access an object that |
| | | requires administrator permissions. |
|-----------|-----------------|-------------------------------------------------|
| 403 | Forbidden | Access is not allowed for reasons that are not |
| | | covered by error code 401. |
|-----------|-----------------|-------------------------------------------------|
| 404 | Not found | The specified REST endpoint does not exist. The |
| | | caller can try again. |
|-----------|-----------------|-------------------------------------------------|
| 405 | Not allowed | A request was made of a resource using a method |
| | | that is not supported by that resource. For |
| | | example, using GET on a form which requires data|
| | | to be presented via POST, or using PUT on a |
| | | read-only resource. |
|-----------|-----------------|-------------------------------------------------|
| 406 | Not acceptable | The requested resource is only capable of |
| | | generating content that is not acceptable |
| | | according to the Accept headers sent in the |
| | | request. |
|-----------|-----------------|-------------------------------------------------|
| 500 | System Errors | If service implementation throws any other |
| | | exception like network errors, database |
| | | communication, framework returns HTTP 500. |
'==========='================='================================================='
响应负载
POST、PUT 和 GET web API 调用 return 响应负载。此负载是 JSON- 或 XML-formatted 响应 body。请求中的 Accept: application/ header 确定响应的格式 body,其中 FORMAT 是 json 或 xml.
DELETE 调用成功 return 正确。不成功的 DELETE 调用 return 的负载类似于其他调用。
格式错误
发生错误时,响应body包含错误代码、错误消息和可选参数。
.------------.---------------------------------------------------------------.
| Part | Description |
|------------|---------------------------------------------------------------|
| code | The status code representing the error. |
|------------|---------------------------------------------------------------|
| message | The message explaining the error. |
|------------|---------------------------------------------------------------|
| parameters | Optional. An array of attributes used to generate a different |
| | and/or localized error message for the client. |
'------------'---------------------------------------------------------------'
我的商店有 magento2,我想使用 REST API 获取所有 configurable/simple 产品及其子(变体)产品。我能够在单个 REST API 调用中获取所有产品,但它没有为我提供可配置产品的子项。
我需要如下所示的可配置产品:
{
"id":1,
"parent_id":1,
"name":"myProduct",
"is_active":true,
.................
"children_data":[
{
"id":1,
"parent_id":1,
"name":"myProduct",
"is_active":true,
.................
},
{
"id":1,
"parent_id":1,
"name":"myProduct",
"is_active":true,
.................
}
]
}
您需要使用以下 REST API 来获取子数据
GET /V1/configurable-products/{sku}/children
回复:
[
{
"sku": "MH01-XS-Black",
"name": "Chaz Kangeroo Hoodie-XS-Black",
"attribute_set_id": 9,
"price": 52,
"status": 1,
"type_id": "simple",
"created_at": "2015-11-20 08:12:24",
"updated_at": "2015-11-20 08:12:24",
"weight": 1,
"extension_attributes": [],
"product_links": [],
"tier_prices": [],
"custom_attributes": [
{
"attribute_code": "required_options",
"value": "0"
},
{
"attribute_code": "has_options",
"value": "0"
},
{
"attribute_code": "tax_class_id",
"value": "2"
},
{
"attribute_code": "image",
"value": "/m/h/mh01-black_main.jpg"
},
{
"attribute_code": "category_ids",
"value": [
"2",
"15",
"36"
]
},
{
"attribute_code": "size",
"value": "167"
},
{
"attribute_code": "color",
"value": "49"
},
{
"attribute_code": "small_image",
"value": "/m/h/mh01-black_main.jpg"
},
{
"attribute_code": "thumbnail",
"value": "/m/h/mh01-black_main.jpg"
},
{
"attribute_code": "url_key",
"value": "chaz-kangeroo-hoodie-xs-black"
},
{
"attribute_code": "msrp_display_actual_price_type",
"value": "0"
}
]
},
...
]
并且如果您需要所有可配置选项并调用以下 API
GET /V1/configurable-products/{sku}/options/all
回复:
[
{
"id": 3,
"attribute_id": "90",
"label": "Color",
"position": 0,
"values": [
{
"value_index": 49
},
{
"value_index": 52
},
{
"value_index": 56
}
],
"product_id": 67
},
{
"id": 2,
"attribute_id": "137",
"label": "Size",
"position": 0,
"values": [
{
"value_index": 167
},
{
"value_index": 168
},
{
"value_index": 169
},
{
"value_index": 170
},
{
"value_index": 171
}
],
"product_id": 67
}
]
如何使用 REST APIs
1. 身份验证
Magento 允许开发人员在配置文件 webapi.xml
中定义网络 API 资源及其权限。这里有更多关于公开 services as Web APIs.
在进行网络 API 调用之前,您必须验证您的身份并具有访问 API 资源所需的权限(授权)。身份验证允许 Magento 识别调用者的用户类型。根据用户(管理员、集成、客户或来宾)的访问权限,确定 API 呼叫的资源可访问性。
2。构建请求
每个 Magento 网络 API 调用包含以下元素的组合:
2.1 HTTP 动词
GET
。请求传输目标的当前表示 资源。如果省略动词,则默认为GET
。PUT
。要求 目标资源的状态被创建或替换为 由包含在请求消息中的表示定义的状态 有效载荷。POST
。请求源服务器接受 请求中包含的表示作为要由处理的数据 目标资源。DELETE
。请求源服务器删除 目标资源。
2.2 ConfigurableProduct 的端点
GET /V1/configurable-products/:sku/children
DELETE /V1/configurable-products/:sku/children/:childSku
PUT /V1/configurable-products/variation
POST /V1/configurable-products/:sku/child
GET /V1/configurable-products/:sku/options/:id
GET /V1/configurable-products/:sku/options/all
POST /V1/configurable-products/:sku/options
PUT /V1/configurable-products/:sku/options/:id
DELETE /V1/configurable-products/:sku/options/:id
2.3 HTTP headers
To specify an HTTP header in a cURL command, use the
-H
or--header
option.
在您的网络 API 调用中指定以下一个或多个 HTTP header:
.---------------.-----------------------------------.
| HTTP header | Syntax |
|---------------|-----------------------------------|
| Authorization | Authorization: Bearer <TOKEN> |
| Accept | Accept: application/<FORMAT> |
| Content-Type | Content-Type:application/<FORMAT> |
'---------------'-----------------------------------'
其中 <TOKEN>
是由 Magento 令牌服务 return 提供的身份验证令牌。请参阅身份验证。
2.4 调用载荷
调用负载是您随请求提供的一组输入参数和属性。 API 操作具有 必需的 和 可选的 输入。
您在 URI 中指定输入参数。
您在 JSON 或 XML-formatted 请求中指定输入属性 body。
2.5构造请求
准备
Authorization
、Accept
和Content-Type
header 以传递给请求 object。使用由 Magento 令牌服务 return 提供的授权令牌。$token = 'token'; $httpHeaders = new \Zend\Http\Headers(); $httpHeaders->addHeaders([ 'Authorization' => 'Bearer ' . $token, 'Accept' => 'application/json', 'Content-Type' => 'application/json' ]);
打开Magento/ConfigurableProduct/etc/webapi.xml configuration file and find the
getChildren
method from Magento\ConfigurableProduct\Api\LinkManagementInterface界面将 headers、URI 和方法设置为请求 object。
$request = new \Zend\Http\Request(); $request->setHeaders($httpHeaders); $request->setUri('http://yourdomain.com/rest/V1/configurable-products/:sku/children'); $request->setMethod(\Zend\Http\Request::METHOD_GET); $params = new \Zend\Stdlib\Parameters([ 'sku' => 'sku-needed-for-example' ]); $request->setQuery($params);
准备 HTTP Curl 客户端 object 并将请求 object 传递给
Client::send()
方法。$client = new \Zend\Http\Client(); $options = [ 'adapter' => 'Zend\Http\Client\Adapter\Curl', 'curloptions' => [CURLOPT_FOLLOWLOCATION => true], 'maxredirects' => 0, 'timeout' => 30 ]; $client->setOptions($options); $response = $client->send($request);
此请求 return 是
JSON
格式的所有孩子的列表。您还可以通过更改请求的Accept
header 来指定XML
格式。
3。使用 cURL 来 运行 请求
cURL 是一个 command-line 工具,可让您从命令行或 shell 脚本传输和接收 HTTP 请求和响应。它可用于 Linux 分布、Mac OS X 和 Windows.
要使用 cURL 来 运行 您的 REST web API 调用,请使用 cURL 命令语法来构建 cURL 命令。
要在调用中创建端点,请将您在步骤 2.5 构造请求中构造的 REST URI 附加到此 URL:
https://<MAGENTO_HOST_OR_IP>/<MAGENTO_BASE_INSTALL_DIR>/rest/
有关 cURL 命令选项的完整列表,请参阅 curl.1 the man page。
4。查看响应
每个网站 API 调用 return 一个 HTTP 状态代码和一个响应负载。当发生错误时,响应 body 也 return 是一条错误消息。
HTTP 状态码
每个网站 API 调用 return 一个反映请求结果的 HTTP 状态代码:
.===========.=================.=================================================.
| HTTP code | Meaning | Description |
|===========|=================|=================================================|
| 200 | Success | M2 return HTTP 200 to the caller upon success. |
|-----------|-----------------|-------------------------------------------------|
| | | If service implementation throws either |
| | | `Magento_Service_Exception` or its derivative, |
| | | the framework returns a HTTP 400 with a error |
| | | response including the service-specific error |
| 400 | Bad Request | code and message. This error code could |
| | | indicate a problem such as a missing required |
| | | parameter or the supplied data didn't pass |
| | | validation. |
|-----------|-----------------|-------------------------------------------------|
| 401 | Unauthorized | The caller was not authorized to perform the |
| | | request. For example, the request included an |
| | | invalid token or a user with customer |
| | | permissions attempted to access an object that |
| | | requires administrator permissions. |
|-----------|-----------------|-------------------------------------------------|
| 403 | Forbidden | Access is not allowed for reasons that are not |
| | | covered by error code 401. |
|-----------|-----------------|-------------------------------------------------|
| 404 | Not found | The specified REST endpoint does not exist. The |
| | | caller can try again. |
|-----------|-----------------|-------------------------------------------------|
| 405 | Not allowed | A request was made of a resource using a method |
| | | that is not supported by that resource. For |
| | | example, using GET on a form which requires data|
| | | to be presented via POST, or using PUT on a |
| | | read-only resource. |
|-----------|-----------------|-------------------------------------------------|
| 406 | Not acceptable | The requested resource is only capable of |
| | | generating content that is not acceptable |
| | | according to the Accept headers sent in the |
| | | request. |
|-----------|-----------------|-------------------------------------------------|
| 500 | System Errors | If service implementation throws any other |
| | | exception like network errors, database |
| | | communication, framework returns HTTP 500. |
'==========='================='================================================='
响应负载
POST、PUT 和 GET web API 调用 return 响应负载。此负载是 JSON- 或 XML-formatted 响应 body。请求中的 Accept: application/ header 确定响应的格式 body,其中 FORMAT 是 json 或 xml.
DELETE 调用成功 return 正确。不成功的 DELETE 调用 return 的负载类似于其他调用。
格式错误
发生错误时,响应body包含错误代码、错误消息和可选参数。
.------------.---------------------------------------------------------------.
| Part | Description |
|------------|---------------------------------------------------------------|
| code | The status code representing the error. |
|------------|---------------------------------------------------------------|
| message | The message explaining the error. |
|------------|---------------------------------------------------------------|
| parameters | Optional. An array of attributes used to generate a different |
| | and/or localized error message for the client. |
'------------'---------------------------------------------------------------'