2

I got product SKU as per category id with following code

$requestUrl='https://www.vaibhavjewellers.com/staging/index.php/rest/V1/categories/3/products/';

With the above code, I am getting all SKU for category id 3.
After getting product SKU I want to get all product details.

$requestUrls="https://www.vaibhavjewellers.com/staging/index.php/rest/all/V1/products?searchCriteria[filter_groups][0][filters][0][field]=sku&searchCriteria[filter_groups][0][filters][0][value]=%201VA1184%25
";

But above code returning nothing ....
Anyone can help me regarding this Thank you.

<?php
//API URL for authentication
$apiURL="https://www.vaibhavjewellers.com/staging/index.php/rest/V1/integration/admin/token";
//parameters passing with URL
$data = array("username" => "sarveshmobileapp", "password" => "Sarvesh@123");
$data_string = json_encode($data);
$ch = curl_init($apiURL);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json","Content-Length: ".strlen($data_string)));
$token = curl_exec($ch);
//decoding generated token and saving it in a variable
$token=  json_decode($token);
//******************************************//
//Using above token into header
$headers = array("Authorization: Bearer ".$token);
//API URL to get all Magento 2 modules
$requestUrl='https://www.vaibhavjewellers.com/staging/index.php/rest/V1/categories/3/products/'; ///V1/products?searchCriteria[pageSize]=20 ///V1/products?searchCriteria
$ch = curl_init($requestUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
//printing result
//print_r($result);
//decoding result
$results=  json_decode($result,true);
foreach($results as $result){
    /*echo '<pre>';
    print_r($result);
    echo '</pre>';*/
    echo $sku = $result['sku'].'<br>';
}
echo $requestUrls="https://www.vaibhavjewellers.com/staging/index.php/rest/all/V1/products?searchCriteria[filter_groups][0][filters][0][field]=sku&searchCriteria[filter_groups][0][filters][0][value]=124-VG2303
"; ///V1/products?searchCriteria[pageSize]=20 ///V1/products?searchCriteria
$ch = curl_init($requestUrls);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$resultr = curl_exec($ch);
//printing result
print_r($resultr);
1

1 Answer 1

1

You can try this one:

$requestUrls="https://www.vaibhavjewellers.com/staging/index.php/rest/all/V1/products?searchCriteria[filter_groups][0][filters][0][field]=sku&searchCriteria[filter_groups][0][filters][0][value]=201VA1184";   

UPDATE:

<?php
//API URL for authentication
$apiURL="https://www.vaibhavjewellers.com/staging/index.php/rest/V1/integration/admin/token";
//parameters passing with URL
$data = array("username" => "sarveshmobileapp", "password" => "Sarvesh@123");
$data_string = json_encode($data);
$ch = curl_init($apiURL);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json","Content-Length: ".strlen($data_string)));
$token = curl_exec($ch);
//decoding generated token and saving it in a variable
$token=  json_decode($token);
//******************************************//
//Using above token into header
$headers = array("Authorization: Bearer ".$token);
//API URL to get all Magento 2 modules
$requestUrl='https://www.vaibhavjewellers.com/staging/index.php/rest/V1/categories/3/products/'; ///V1/products?searchCriteria[pageSize]=20 ///V1/products?searchCriteria
$ch = curl_init($requestUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
//printing result
//print_r($result);
//decoding result
$results=  json_decode($result,true);
foreach($results as $result){
    /*echo '<pre>';
    print_r($result);
    echo '</pre>';*/
    $sku = $result['sku'].'<br>';
}
$requestUrls="https://www.vaibhavjewellers.com/staging/index.php/rest/all/V1/products?searchCriteria[filter_groups][0][filters][0][field]=sku&searchCriteria[filter_groups][0][filters][0][value]=201VA1184";
$ch1 = curl_init($requestUrls);
curl_setopt($ch1,CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Authorization: Bearer '. $token
    )
);
curl_setopt($ch1, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch1);

//decoding result 
$r = json_decode($result, true); 

echo $r['items']['0']['name'].'<br>'; 
echo $r['items']['0']['price'].'<br>'; 

 for($i=6; $i<=8; $i++){

    echo $r['items']['0']['custom_attributes'][$i]['attribute_code'].'<br>';
    echo $r['items']['0']['custom_attributes'][$i]['value'].'<br>';
 }
6
  • i am getting error Consumer is not authorized to access %resources Apr 22, 2019 at 10:45
  • you should use a token for that in your authentication
    – fmsthird
    Apr 22, 2019 at 10:46
  • using rest/V1/integration/admin/token
    – fmsthird
    Apr 22, 2019 at 10:47
  • i am passing token kindly check updated questions Apr 22, 2019 at 10:48
  • now i want to remove all data expect name, price image Apr 22, 2019 at 11:43

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.