Hello Magento Friends,
In today’s article, I am going to discuss How to Get Products by ID using the REST API in Magento 2.
REST API is used to send requests and receive responses. Developers can get product information using product IDs in Magento 2.
Related Article – Magento 2: How to Get Order Details using REST API
Learn how to Get Products by ID using REST API in Magento 2
Steps to Get Products by ID using REST API in Magento 2
Step 1: Create the rootscript.php script file and add the below code
<?php $baseUrl = " http://yourdomain/"; // your magento base url $userData = array("username" => "admin", "password" => "admin@123"); $ch = curl_init($baseUrl."/rest/V1/integration/admin/token"); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($userData)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Lenght: " . strlen(json_encode($userData)))); $token = curl_exec($ch); $ch = curl_init($baseUrl."rest/V1/products?searchCriteria[filterGroups][0][filters][0][field]=entity_id&searchCriteria[filterGroups][0][filters][0][value]=Your_Product_ID&searchCriteria[filterGroups][0][filters][0][condition_type]=eq"); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token))); $result = curl_exec($ch); $result = json_decode($result, 1); echo '<pre>'; print_r($result); echo '</pre>'; ?>
Conclusion:
This way you can Get Products by ID using the REST API in Magento 2. If you have any queries let me know in the comment box.
Happy Coding!