Skip to content

Fetch Products using Slug in Magento

<?php
$adminToken = "";

$slug = "dish-washing-powder";
$url = "https://mysite.com/rest/V1/products?searchCriteria[filter_groups][0][filters][0][field]=url_key&searchCriteria[filter_groups][0][filters][0][value]=$slug&searchCriteria[filter_groups][0][filters][0][condition_type]=eq";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "Authorization: Bearer $adminToken",
    "Content-Type: application/json"
]);

$response = curl_exec($ch);
curl_close($ch);

$products = json_decode($response, true);

echo "<pre>";
print_r($products);
echo "</pre>";
?>

Here the output is not fetching any details of the product. Any help would be appreciated