3

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

2

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.

|improve this answer|||||
0

i was put this code for get all data of customers

$request->setUri('localhost/magento2/rest/V1/customers');

$request->setMethod(\Zend\Http\Request::METHOD_POST);

and got the below error when i print the response.

stdClass Object
(
   [message] => %fieldName is a required field.
   [parameters] => stdClass Object
    (
        [fieldName] => customer
    )
)

can you please suggest me how can i resolve the issue..

|improve this answer|||||

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.