4

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.

4

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);
  • Thanks As of now login is perfect working. But in forgot password i got some error. You can see below error. Invalid type for value: "password". Expected Type: "int". can you please help me resolve the issue. – Kartik Asodariya Feb 7 '17 at 6:28
  • @KartikAsodariya you used the second code lines? – Khoa TruongDinh Feb 7 '17 at 6:35
  • I was use both code and login working fine but getting some issues with forgot password which you can see above comment – Kartik Asodariya Feb 7 '17 at 6:50
  • @KartikAsodariya strange, I have checked, my code lines are working fine. Which is your Magento version? – Khoa TruongDinh Feb 7 '17 at 7:05
  • Magento ver. 2.1.3 – Kartik Asodariya Feb 7 '17 at 7:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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