Question

I need customer login and customer forgot password using Magento 2 API. Can you please help me. I was search but not getting any code from google.

Was it helpful?

Solution

For the API, you should read more:

For example, this below code will return the customer info:

<?php

$userData = ["username" => "customeruser@gmail.com", "password" => "123456"];
$ch = curl_init("http://magen2.loc/rest/V1/integration/customer/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);

//Get customer info
$ch = curl_init("http://magen2.loc/rest/V1/customers/me");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); // Get method
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
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 the customer forgot password:

$emailcontent = [
    "email"=> "test@gmail.com",
    "template" => "email_reset", // Using template email reset
    "websiteId" => 1
];



$ch = curl_init("http://magen2.loc/rest/V1/customers/password");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); // Put method
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($emailcontent));
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);
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top