2

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
 )
asked Oct 31, 2018 at 7:04
1
  • check my answer and let me know if you've any query. Commented Oct 31, 2018 at 7:18

4 Answers 4

5

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);
answered Oct 29, 2019 at 6:22
6
  • can you tell me plz where to put this code in my module directory?? Commented Nov 29, 2019 at 7:44
  • You can use this code anywhere, for convenient you can use in Model Commented Nov 29, 2019 at 8:33
  • can you give me specific directory for this, i cant understand this? Commented Nov 29, 2019 at 8:35
  • Could you explain your requirement ? Commented Nov 29, 2019 at 9:03
  • 1
    i want to create administrator hardcoded on back-end, through which i want to access all the resources of the administator, till know i generated the token through which i want to access all the resources, any help from you should appreciated, thanks:) Commented Nov 29, 2019 at 9:09
4

In order to display all products, you need to have at least one criteria sent to the Rest endpoint. If you have more than 1000 items, you could loop through the pages (although any example with> 1000 items would require more work to deal with that amount of data).

Note: you will need to install the zend http composer package as below: composer require zendframework/zend-http

<?php
require __DIR__ . '/vendor/autoload.php';
$customer_access_token = 'uvreguaelo0tug3tqmfx633420e3pth3';
$httpHeaders = new Zend\Http\Headers();
$httpHeaders->addHeaders([
 'Authorization' => 'Bearer ' . $customer_access_token,
 'Accept' => 'application/json',
 'Content-Type' => 'application/json',
]);
$request = new \Zend\Http\Request();
$request->setHeaders($httpHeaders);
$request->setUri('http://magento.test/rest/V1/products?fields=items[sku,name]&searchCriteria[pageSize]=1000');
$client = new Zend\Http\Client();
$response = $client->send($request);
$products = json_decode($response->getBody());
foreach ($products->items as $key => $product) {
 printf("Product SKU: %s, product name: %s\n", $product->sku, $product->name);
}
answered Jan 16, 2019 at 1:46
0

you can achieve this using catalogProductRepositoryV1

  • method GET
  • Authentication admin token

  • url: http://localhost.com/index.php/rest/V1/products

Response

{
 "items": [
 {
 "id": 0,
 "sku": "string",
 "name": "string",
 "attributeSetId": 0,
 "price": 0,
 "status": 0,
 "visibility": 0,
 "typeId": "string",
 "createdAt": "string",
 "updatedAt": "string",
 "weight": 0,
 "extensionAttributes": {
 "downloadableProductLinks": [
 {
 "id": 0,
 "title": "string",
 "sortOrder": 0,
 "isShareable": 0,
 "price": 0,
 "numberOfDownloads": 0,
 "linkType": "string",
 "linkFile": "string",
 "linkFileContent": {
 "fileData": "string",
 "name": "string",
 "extensionAttributes": {}
 },
 "linkUrl": "string",
 "sampleType": "string",
 "sampleFile": "string",
 "sampleFileContent": {
 "fileData": "string",
 "name": "string",
 "extensionAttributes": {}
 },
 "sampleUrl": "string",
 "extensionAttributes": {}
 }
 ],
 "downloadableProductSamples": [
 {
 "id": 0,
 "title": "string",
 "sortOrder": 0,
 "sampleType": "string",
 "sampleFile": "string",
 "sampleFileContent": {
 "fileData": "string",
 "name": "string",
 "extensionAttributes": {}
 },
 "sampleUrl": "string",
 "extensionAttributes": {}
 }
 ],
 "stockItem": {
 "itemId": 0,
 "productId": 0,
 "stockId": 0,
 "qty": 0,
 "isInStock": true,
 "isQtyDecimal": true,
 "showDefaultNotificationMessage": true,
 "useConfigMinQty": true,
 "minQty": 0,
 "useConfigMinSaleQty": 0,
 "minSaleQty": 0,
 "useConfigMaxSaleQty": true,
 "maxSaleQty": 0,
 "useConfigBackorders": true,
 "backorders": 0,
 "useConfigNotifyStockQty": true,
 "notifyStockQty": 0,
 "useConfigQtyIncrements": true,
 "qtyIncrements": 0,
 "useConfigEnableQtyInc": true,
 "enableQtyIncrements": true,
 "useConfigManageStock": true,
 "manageStock": true,
 "lowStockDate": "string",
 "isDecimalDivided": true,
 "stockStatusChangedAuto": 0,
 "extensionAttributes": {}
 },
 "giftcardAmounts": [
 {
 "websiteId": 0,
 "value": 0,
 "websiteValue": 0,
 "extensionAttributes": {}
 }
 ],
 "configurableProductOptions": [
 {
 "id": 0,
 "attributeId": "string",
 "label": "string",
 "position": 0,
 "isUseDefault": true,
 "values": [
 {
 "valueIndex": 0,
 "extensionAttributes": {}
 }
 ],
 "extensionAttributes": {},
 "productId": 0
 }
 ],
 "configurableProductLinks": [
 0
 ],
 "bundleProductOptions": [
 {
 "optionId": 0,
 "title": "string",
 "required": true,
 "type": "string",
 "position": 0,
 "sku": "string",
 "productLinks": [
 {
 "id": "string",
 "sku": "string",
 "optionId": 0,
 "qty": 0,
 "position": 0,
 "isDefault": true,
 "price": 0,
 "priceType": 0,
 "canChangeQuantity": 0,
 "extensionAttributes": {}
 }
 ],
 "extensionAttributes": {}
 }
 ]
 },
 "productLinks": [
 {
 "sku": "string",
 "linkType": "string",
 "linkedProductSku": "string",
 "linkedProductType": "string",
 "position": 0,
 "extensionAttributes": {
 "qty": 0
 }
 }
 ],
 "options": [
 {
 "productSku": "string",
 "optionId": 0,
 "title": "string",
 "type": "string",
 "sortOrder": 0,
 "isRequire": true,
 "price": 0,
 "priceType": "string",
 "sku": "string",
 "fileExtension": "string",
 "maxCharacters": 0,
 "imageSizeX": 0,
 "imageSizeY": 0,
 "values": [
 {
 "title": "string",
 "sortOrder": 0,
 "price": 0,
 "priceType": "string",
 "sku": "string",
 "optionTypeId": 0
 }
 ],
 "extensionAttributes": {}
 }
 ],
 "mediaGalleryEntries": [
 {
 "id": 0,
 "mediaType": "string",
 "label": "string",
 "position": 0,
 "disabled": true,
 "types": [
 "string"
 ],
 "file": "string",
 "content": {
 "base64EncodedData": "string",
 "type": "string",
 "name": "string"
 },
 "extensionAttributes": {
 "videoContent": {
 "mediaType": "string",
 "videoProvider": "string",
 "videoUrl": "string",
 "videoTitle": "string",
 "videoDescription": "string",
 "videoMetadata": "string"
 }
 }
 }
 ],
 "tierPrices": [
 {
 "customerGroupId": 0,
 "qty": 0,
 "value": 0,
 "extensionAttributes": {}
 }
 ],
 "customAttributes": [
 {
 "attributeCode": "string",
 "value": "string"
 }
 ]
 }
 ],
 "searchCriteria": {
 "filterGroups": [
 {
 "filters": [
 {
 "field": "string",
 "value": "string",
 "conditionType": "string"
 }
 ]
 }
 ],
 "sortOrders": [
 {
 "field": "string",
 "direction": "string"
 }
 ],
 "pageSize": 0,
 "currentPage": 0
 },
 "totalCount": 0
}
answered Oct 31, 2018 at 7:12
2
  • I'm trying to get product details in core php .... Is there any example for this? Commented Oct 31, 2018 at 7:19
  • Please check my updated question Commented Oct 31, 2018 at 7:22
0

You need to add searchCriteria field name at the end of your url as follows:

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

Please refer this link for your reference. It may helpful to you.

answered Jan 21, 2019 at 5:51

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.