Magento2 REST API – How to Get All Product Details

magento2rest api

I have created Integration in System->Integration. And Set access to catelog ->products to the integration. I can get Single Product by Sku using

http://localhost.com/index.php/rest/V1/products/:sku

But i need to display all the products. How to get the result.

Thanks in advance.

My Code to get product details

function sign($method, $url, $data, $consumerSecret, $tokenSecret)
{
    $url = urlEncodeAsZend($url);

    $data = urlEncodeAsZend(http_build_query($data, '', '&'));
    $data = implode('&', [$method, $url, $data]);

    $secret = implode('&', [$consumerSecret, $tokenSecret]);

    return base64_encode(hash_hmac('sha1', $data, $secret, true));
}

function urlEncodeAsZend($value)
{
    $encoded = rawurlencode($value);
    $encoded = str_replace('%7E', '~', $encoded);
    return $encoded;
}

// REPLACE WITH YOUR ACTUAL DATA OBTAINED WHILE CREATING NEW INTEGRATION
$consumerKey = 'key';
$consumerSecret = 'key';
$accessToken = 'key';
$accessTokenSecret = 'key';

$method = 'GET';
$url = 'http://localhost.com/index.php/rest/V1/products';

//
$data = [
    'oauth_consumer_key' => $consumerKey,
    'oauth_nonce' => md5(uniqid(rand(), true)),
    'oauth_signature_method' => 'HMAC-SHA1',
    'oauth_timestamp' => time(),
    'oauth_token' => $accessToken,
    'oauth_version' => '1.0',
];

$data['oauth_signature'] = sign($method, $url, $data, $consumerSecret, $accessTokenSecret);

$curl = curl_init();

curl_setopt_array($curl, [
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => $url,
    CURLOPT_HTTPHEADER => [
        'Authorization: OAuth ' . http_build_query($data, '', ',')
    ]
]);

$result = curl_exec($curl);
curl_close($curl);
echo '<pre>';print_r(json_decode($result));

But it returns error

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

Best Answer

First get admin token,

$adminUrl='http:Yourlurl/rest/V1/integration/admin/token';
$ch = curl_init();
$data = array("username" => "admin_username", "password" => "admin_password");
$dataString = json_encode($data);
$ch = curl_init($adminUrl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($dataString))
);
$token = curl_exec($ch);
$token = json_decode($token);
print_r($token);
curl_close($ch);

You can make use of below code, If you have many products increase the page_size.

<?php
$productUrl='http:yourlUrl/rest/V1/products?searchCriteria[page_size]=20';

$ch = curl_init($productUrl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Content-Type: application/json",
    "Authorization: Bearer $token"
    )
);
$productList = curl_exec($ch);
$err      = curl_error($ch);
$products = json_decode($productList);
echo '<pre>';print_r($products );
curl_close($ch);