21

I am very confused about what kind of mistake I am doing to follow these steps provided here at the following link :

Create new Magento Rest API to get category list in Magento

After doing all these steps and even editing, after reading all the solutions still getting the same error as getting before the implementation of this link steps.

To Create a new Magento Rest API to get category list in Magento 1.9,
Where I can get the category tree?

1
  • can you please which error are you getting? Commented Aug 28, 2019 at 7:56

3 Answers 3

1
<?php
$userData = array("username" => "admin", "password" => "15325833");
$ch = curl_init("http://website.com/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-Length: " . strlen(json_encode($userData))));
$token = curl_exec($ch);
$request = "http://website.com/rest/V1/categories";  
$ch = curl_init($request);
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);
print_r(json_decode($result));
?>
0

By default, Magento 2 allows access without authentication for few endpoints, which are declared as anonymous resources

Stores > Settings > Configuration > Services > Magento Web API > Web API Security > Allow Anonymous Guest Access

Now, It is possible to get the categories without authentication by using this endpoint. and run url in postman.

http://example.com/rest/V1/categories
0

In Magento 2 By default, you get all the categories including all root categories too.

Try This Steps:

Request: {{your_base_url}}/rest/all/V1/categories

Method: Get

Your Headers should be - Authorization Bearer "youradminaccesstoken"

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.