Magento 2 API Create Customer

Here I am creating a customer using REST API

<?php
 $magentoURL = "http://127.0.0.1/mage2/";
 $apiURL = $magentoURL."index.php/rest/V1/integration/admin/token";
 $data = array("username" => "APIUSER", "password" => "APIUSERPWD");
 $data_string = json_encode($data);

$ch = curl_init($apiURL);
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
 curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json","Content-Length: ".strlen($data_string)));
 $token = curl_exec($ch);

$customerData = [
 'customer' => [
 "email" => 'Test.User'.time().'@phpcodez.com',
 "firstname" => "Brand",
 "lastname" => "Muscle",
 "storeId" => 1,
 "websiteId" => 1
 ],
 "password" => "PHPCodez1&"
 ];

$ch = curl_init($magentoURL."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);
?>

Leave a Reply

Your email address will not be published. Required fields are marked *