2016-11-21 53 views
6

我正在使用Magento版本。 2.1.2 REST API來創建用戶,以下這一點: http://devdocs.magento.com/guides/m1x/api/rest/Resources/resource_customers.html#RESTAPI-Resource-Customers-HTTPMethod-POST-customersPhp Magento Api Rest創建客戶密碼問題:

$data = [ 
     "customer" => [ 
      "firstname" => 'Earl', 
      "lastname" => 'Hickey', 
      "email" => '[email protected]', 
      "password" => 'password', 
      "website_id" => 1, 
      'store_id' => 1, 
      "group_id" => 1 
     ] 
    ]; 

    $token = $this->get('lp_api')->getToken(); 
    $ch = curl_init($this->endpoint . 'customers'); 

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); 
    curl_setopt($ch, CURLOPT_VERBOSE, true);  
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
     "Content-Type: application/json", "Authorization: Bearer " . json_decode($token), 
     ) 
    ); 

     // var_dump(curl_getinfo($c)); 
    $result = curl_exec($ch); 

如果我發送密碼(如上面的例子),我有以下錯誤:

Next Exception: Report ID: webapi-583357a3bf02f; Message: Property "Password" does not have corresponding setter in class "Magento\Customer\Api\Data\CustomerInterface". in /var/www/html/www.magento.dev/vendor/magento/framework/Webapi/ErrorProcessor.php:195 

我注意到如果我從$ data數組中刪除「password」=>'password',就會創建一個沒有密碼的用戶(對我而言似乎很奇怪)。

我找不到這個錯誤的任何幫助。 任何想法的人?

+0

入住這裏一步一步的指導:http://magento.stackexchange.com/questions/150581/magento-2-how-to-call-rest- api-to-register-a-new-customer/156818#156818 – Manish

回答

2

請參閱下面的鏈接Magento 2.x版本。 http://devdocs.magento.com/swagger/index_20.html#/

我已經使用下面的身體通過Rest Api創建客戶,並且它正常工作。

{ 「客戶」:{

「電子郵件」: 「[email protected]」, 「名字」: 「X」, 「姓氏」, 「Y」, 「website_id」: 1, 「GROUP_ID」:1, 「custom_attributes」:[{ 「attribute_code」: 「mobile_no」, 「值」: 「1234567890」 } ]

},

「密碼」: 「123456」

}

+0

謝謝,作品像一個魅力。 但是,爲什麼在那個文檔中密碼是客戶的一部分? http://devdocs.magento.com/guides/m1x/api/rest/Resources/resource_customers.html#RESTAPI-Resource-Customers-HTTPMethod-POST-customers。 最後一個問題,在大搖大擺的地方,你究竟在哪裏找到這個信息? 我唯一能找到的是「customerAccountManagementV1」,而且這裏沒有提到密碼。 – user2227498

+0

鏈接:http://devdocs.magento.com/guides/m1x/api/rest/Resources/resource_customers.html#RESTAPI-Resource-Customers-HTTPMethod-POST-customers – qwerty

+0

對不起,以上評論。 鏈接:http://devdocs.magento.com/guides/m1x/api/rest/Resources/resource_customers.html#RESTAPI-Resource-Customers-HTTPMethod-POST-customers 用於Magento 1.x版本。 在文檔中,他們只給出了示例,我分享了我使用它的方式。 在swagger中,您可以單擊customerCustomerRepositoryV1鏈接,您將通過它的示例獲取所有與客戶相關的API。 – qwerty