1

I want to search customer by name. Any options to search customer by name using Rest API in Magento 2

2
  • yes issue resolved
    – Gagan
    Mar 15 '19 at 6:31
  • 1
    Gagan, if answer helps you then you mark accepted to those answer, so it will help future readers, you can mark accept answer by clicking (✓) next to answer Mar 15 '19 at 7:09
1

You can use GET /V1/customers/search with searchcriteria.

Something like the following:

http://my.website.com/index.php/rest/V1/customers/search? 
searchCriteria[filterGroups][0][filters][0][field]=firstname& 
searchCriteria[filterGroups][0][filters][0][value]=%& 
searchCriteria[filterGroups][0][filters][0][condition_type]=like
1
  • It get all customer name, how to get customer according name please explain according explain
    – Gagan
    Mar 14 '19 at 15:21
1

In order to search customer by name using REST API. I use the firstname for search, you can use lastname as well.

<?php
 //**** Use your Url ********************//
 $magentoURL = "http://127.0.0.1/mage2/"; 

 $apiURL = $magentoURL."index.php/rest/V1/integration/admin/token";
 $data = array("username" => "APIUSER", "password" => "APIUSERWD");
 $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);

//decoding generated token and saving it in a variable
 $token= json_decode($token);

//******************************************//

//Using above token into header
 $headers = array("Authorization: Bearer ".$token);

// **** Your search customer name **********//
 $customername = "abc";    

//API URL to get all Magento 2 modules
$requestUrl = $magentoURL.'index.php/rest/V1/customers/search?searchCriteria[filter_groups][0][filters][0][field]=firstname&searchCriteria[filter_groups][0][filters][0][value]=%25'.$customername.'%25&searchCriteria[filter_groups][0][filters][0][condition_type]=like';

$ch = curl_init($requestUrl);
 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 $result = curl_exec($ch);

//decoding result
 $result= json_decode($result);

//printing result
 echo "<pre>";print_r($result);

?>

I hope this will help

2
  • @Gagan If it's work then please mark my answer correct so it's helps others as well. keep coding :) thanks :) Mar 26 '19 at 6:16
  • can we search by email?
    – Pawankumar
    Jan 25 '21 at 15:15

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.