1

I need one help. I need to get all product details using multiple product id/sku in array using REST API. I am explaining my code below.

$pid=[23,24,25]
foreach ($pid as $v) {
        $ch = curl_init("http://example.com/index.php/rest/V1/products?searchCriteria[filterGroups][0][filters][0][field]=entity_id&searchCriteria[filterGroups][0][filters][0][condition_type]=eq&searchCriteria[filterGroups][0][filters][0][value]=".$v);


        //curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        //curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
        $resp = curl_exec($ch);
        $resup = json_decode($resp, 1);
        $allProdArr[]=$resup;
    }

Here I am using loop for that reason also the response is coming late. Here I need Is there any REST API which can take the direct array of ids/sku and give the response in a single request. By using loop and calling passing the request each time give the late response which is not user friendly. Please help me to resolve this issue.

satya
  • 253
  • 1
  • 10

3 Answers3

1

Try below code:

    $pids=[23,24,25]
    $ch = curl_init("http://example.com/index.php/rest/V1/products?searchCriteria[filterGroups][0][filters][0][field]=entity_id&searchCriteria[filterGroups][0][filters][0][condition_type]=in&searchCriteria[filterGroups][0][filters][0][value]=".implode(',',$pids));

    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $resp = curl_exec($ch);
    $resup = json_decode($resp, 1);
    $allProdArr[]=$resup;
Prasanta Hatui
  • 1,811
  • 1
  • 10
  • 16
0

This approach can help in your case, just go through detail of this article: Get product collection with product ids

Om Prakash
  • 76
  • 1
  • 10
  • But this is not REST API implementation. – satya Sep 05 '18 at 07:21
  • Above article would help you to get desired data for your use case. After that just go through this article to implement as REST API perfectly. https://amasty.com/blog/how-to-start-with-magento-2-api/ – Om Prakash Sep 05 '18 at 07:23
0

You have to add your array in searchCriteria value and add the condition to check if entity_id is in your array or not.

Try the following code.

$ids = [1,2,3,4,5];
$ids = '['.implode(',', $ids).']';
$restResourceUri = "http://www.example.com/index.php/rest/V1/products?searchCriteria[filter_groups][0][filters][0][field]=entity_id&searchCriteria[filter_groups][0][filters][0][value]=$ids&searchCriteria[filter_groups][0][filters][0][condition_type]=in";
Dinesh Yadav
  • 6,447
  • 2
  • 23
  • 50