构建一个请求
为了配置web API,开发人员定义了每个API调用中的一些元素 <module root dir>/vendor/<vendor-name>/<module-name>/etc/webapi.xml 文件, <vendor-name> 是您的vendor名称 (如, Magento) 和 <module-name> 你的模块名称 (与它的定义完全匹配composer.json). 例如,客户服务的web API定义在 <Magento 2 安装目录>/vendor/magento/module-customer/etc/webapi.xml 配置文件。服务数据接口和建设者定义API调用所需的可选参数和返回值。
概述
每个Magento Web API调用包含这些元素的组合:
- HTTP verb
 - Endpoint
 - HTTP headers
 - Call payload
 
下表描述了这些API调用元素:
| Element | Specifies | 
|---|---|
HTTP verb  | 
              对Endpoint执行的操作。  | 
            
Endpoint  | 
              实现请求的“服务器”“组合”,它与正在请求的请求的资源组合在一起。  | 
            
HTTP headers  | 
              认证token,呼叫请求和响应格式,等信息。  | 
            
Call payload  | 
              一组输入参数和属性,您提供的请求。  | 
            
HTTP verb
指定请求中的这些HTTP verbs中的一个:
GETPUTPOSTDELETE
Endpoint
endpoint是满足请求、Web服务、正在进行请求的资源的“服务器”的组合,以及任何模板参数。
例如, 在http://magento.ll/index.php/rest/V1/customerGroups/:id endpoint, 服务是 magento.ll/index.php/, web 服务是 rest, 资源是 /V1/customerGroups, 和模板参数是 id.
HTTP headers
在cURL命令中指定HTTP头, 使用-H 选项。
Call payload
例子:
{
    "customers": {
        "customer": {
            "email": "user@example.com",
            "firstname": "John",
            "lastname": "Doe"
        },
        "addresses": [
            {
                "defaultShipping": true,
                "defaultBilling": true,
                "firstname": "John",
                "lastname": "Doe",
                "region": {
                    "regionCode": "CA",
                    "region": "California",
                    "regionId": 12
                },
                "postcode": "90001",
                "street": ["Zoe Ave"],
                "city": "Los Angeles",
                "telephone": "555-000-00-00",
                "countryId": "US"
            }
        ]
    }
}
          构建一个请求
- 打开 Magento/Customer/etc/webapi.xml 配置文件。
 - 
              
查找定义路径的路由元素
createAccount:<route url="/V1/customers" method="POST"> <service class="Magento\Customer\Api\AccountManagementInterface" method="createAccount"/> <resources> <resource ref="anonymous"/> </resources> </route> - 
              
使用
method和url值在route元素构建一个URI。POST /V1/customers
 - 
              
使用
class属性在service元素定义一个接口.例子中接口是
AccountManagementInterfacePHP文件.打开 AccountManagementInterface.php 文件查找
createAccount方法, 如下所示:public function createAccount( \Magento\Customer\Api\Data\CustomerInterface $customer, $password = null, $redirectUrl = '' ); 
客户搜索API请求示例
- 
              
准备
Authorization,Accept和Content-Type要传递给请求对象的头文件。使用认证token返回Magento token 服务。 - 
              
打开 Magento/Customer/etc/webapi.xml 配置文件查找 CustomerRepositoryInterface 接口和
getList方法。 - 
              
设置headers、URI和方法设置为请求对象。 使用 URI
/V1/customers/search和 方法GET值.searchCriteria参数应用于完成客户搜索查询。 - 
              
编写一个HTTP Curl客户端对象并将请求对象传递给
Client::send()方法。 - 
              
此请求以JSON格式返回所有客户的列表。还可以通过更改
Accept标头来指定xml格式。 
$token = 'token'; $httpHeaders = new \Zend\Http\Headers(); $httpHeaders->addHeaders([ 'Authorization' => 'Bearer ' . $token, 'Accept' => 'application/json', 'Content-Type' => 'application/json' ]);
$request = new \Zend\Http\Request();
$request->setHeaders($httpHeaders);
$request->setUri('http://magento.ll/rest/V1/customers/search');
$request->setMethod(\Zend\Http\Request::METHOD_GET);
$params = new \Zend\Stdlib\Parameters([
   'searchCriteria' => '*'
]);
$request->setQuery($params);
            $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);
下一步
通过运行web API调用 cURL 命令 或 REST 客户端.