Magento – How to get all product details using multiple product id/sku in array REST API

magento2rest api

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.

Best Answer

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;