2

I want to create and update customers, order in Magento-2. But I am facing problem in how to pass data to create customer. Can anyone give me an example for this?

I passed data like:

$cusData = "rest/V1/customers/{'customers': {'customer':{'email':'[email protected]','firstname':'Jhon','lastname':'test'}}}";

$cusMethod = 'PUT';

$cusJsonData = getData($token,$cusData,$cusMethod);

function getData($token,$data,$method)
{
    try{
        $requestUrl = BASEURL.$data;
        $ch = curl_init($requestUrl);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
        return curl_exec($ch);
    }catch(Exception $e){
        echo $e->getMessage();
    }
}   

2 Answers 2

1

Create New Customer

    $userData = array("username" => "admin", "password" => "admin123");
    $ch = curl_init("http://magento213/index.php/rest/V1/integration/admin/token");
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($userData));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Lenght: " . strlen(json_encode($userData))));

    $token = curl_exec($ch);


    $customerData = [
        'customer' => [
            "email" => "[email protected]",
            "firstname" => "John",
            "lastname" => "Doe",
            "storeId" => 1,
            "websiteId" => 1
        ],
        "password" => "Demo1234"
    ];

    $ch = curl_init("http://magento213/index.php/rest/V1/customers");
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($customerData));
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));

    $result = curl_exec($ch);

    $result = json_decode($result, 1);
    echo '<pre>';print_r($result);

For Update Customer

$customerData = [
    'customer' => [
        'id' => 10,
        "email" => "[email protected]",
        "firstname" => "John2",
        "lastname" => "Doe2",
        "storeId" => 1,
        "websiteId" => 1
    ],
    "password" => "Demo1234"
];

$ch = curl_init("http://magento213/index.php/rest/V1/customers/10");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($customerData));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));

$result = curl_exec($ch);

$result = json_decode($result, 1);
echo '<pre>';print_r($result);

API LIST Create a customer account

0

You can find examples and detailed informations in the official documentation: http://devdocs.magento.com/swagger/index_20.html#/

For customer creation go to the customerCustomerRepositoryV1 entry and choose the PUT entries for customer creation

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.