Magento – Magento2 Rest api get admin customer details and orders

apicustomermagento2rest

If I login as an admin user, how can I get details of customer and orders list with payment details using REST API in Magento2

Best Answer

Below is the script for order api.

<?php 

error_reporting(E_ALL);
ini_set('display_errors', 1);
require ('/vendor/autoload.php');

require('/vendor/zendframework/zend-http/src/Headers.php');
require('/vendor/zendframework/zend-http/src/Request.php');
require('/vendor/zendframework/zend-http/src/Response.php');

/*Token Generated from SYstem Integratio*/

$token = '9kvn45tbopelidqj9ddita9rvyujk1tl';
$httpHeaders = new \Zend\Http\Headers();

$httpHeaders->addHeaders([
   'Authorization' => 'Bearer ' . $token,
   'Accept' => 'application/json',
   'Content-Type' => 'application/json'
]);

$request = new \Zend\Http\Request();
$request->setHeaders($httpHeaders);
$request->setUri('http://1270.0.1/magentoce27/index.php/rest/V1/orders/1');
$request->setMethod(\Zend\Http\Request::METHOD_GET);



$client = new \Zend\Http\Client();
$options = [
   'adapter'   => 'Zend\Http\Client\Adapter\Curl',
   'curloptions' => [CURLOPT_FOLLOWLOCATION => true],
   'maxredirects' => 0,
   'timeout' => 30
];
$client->setOptions($options);

$response = $client->send($request);

echo "<pre>".print_r(json_decode($response->getBody()),true)."</pre>";
?>

Here, is the script for customer api,

<?php 

error_reporting(E_ALL);
ini_set('display_errors', 1);
require ('/vendor/autoload.php');

require('/vendor/zendframework/zend-http/src/Headers.php');
require('/vendor/zendframework/zend-http/src/Request.php');
require('/vendor/zendframework/zend-http/src/Response.php');

/*Token Generated from SYstem Integratio*/

$token = '9kvn45tbopelidqj9ddita9rvyujk1tl';
$httpHeaders = new \Zend\Http\Headers();

$httpHeaders->addHeaders([
   'Authorization' => 'Bearer ' . $token,
   'Accept' => 'application/json',
   'Content-Type' => 'application/json'
]);

$request = new \Zend\Http\Request();
$request->setHeaders($httpHeaders);
$request->setUri('http://1270.0.1/magentoce27/index.php/rest/V1/customers/1');
$request->setMethod(\Zend\Http\Request::METHOD_GET);



$client = new \Zend\Http\Client();
$options = [
   'adapter'   => 'Zend\Http\Client\Adapter\Curl',
   'curloptions' => [CURLOPT_FOLLOWLOCATION => true],
   'maxredirects' => 0,
   'timeout' => 30
];
$client->setOptions($options);

$response = $client->send($request);

echo "<pre>".print_r(json_decode($response->getBody()),true)."</pre>";
?>

Kindly note that you might need to do changes in API as per your requirement.

Also you will need to generate token from admin panel and put that token into script and also change url as per your project setup.

Related Topic