Error while using REST api in magento

Error while using REST api in magento

July 3, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Error while using REST api in magento

Solving Magento REST API Errors: A Comprehensive Guide

As an experienced developer, I understand the frustration you’re experiencing while working with Magento’s REST API locally on your XAMPP server. Let me walk you through the issues step by step and provide solutions to resolve these problems.

Error 1: Class ‘OAuth’ Not Found

The first error you encountered is due to a missing OAuth class required for handling OAuth authorization in Magento’s REST API setup. This issue occurs because the PHP OAuth extension is not installed on your XAMPP server by default.

Solution:

To fix this, you need to install the PHP OAuth extension on your XAMPP server. Here are the steps:

  1. Download the latest version of php_pear_OAuth from here.

  2. Extract the contents and navigate to the extracted folder.

  3. Run the following command in the terminal:

    php pecl install oauth
    
  4. Restart your Apache server for the changes to take effect.

    • On Windows: Open XAMPP control panel, click “Stop” next to “Apache,” then click “Start.”
    • On Linux/MacOS: Use the command sudo service apache2 restart.

After completing these steps, the OAuth class should be available, and your initial code in dm.php should work without throwing a “Class ‘OAuth’ not found” error.

Error 2: Access Denied (403)

The second issue you faced was an “Access denied (403)” error when trying to retrieve customer information using cURL. This error occurs because the system is unable to authenticate your API request, which is required for accessing Magento’s REST API endpoints.

Solution:

To resolve this issue, you need to set up proper authentication credentials and use them while making requests to the Magento REST API.

  1. Log in to your Magento admin panel at http://localhost/mage2/admin.

  2. Navigate to “System” > “Configuration.”

  3. In the left sidebar, under “Sales,” click on “Magento REST API.”

  4. Expand the “General Configuration” section and ensure that the following settings are enabled:

    • Enabled: Yes
    • Custom Access Key Generation: Yes
  5. Save your configuration changes.

  6. Generate a new access key by going to “System” > “REST API Access.”

  7. Copy the generated Consumer Key and Consumer Secret values.

Now, let’s modify your dm.php file to use these credentials for authentication:

<?php
$ch = curl_init('http://localhost/mage2/api/rest/customers');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_USERPWD, 'consumer_key:consumer_secret'); // Replace with your actual credentials
$customers = curl_exec($ch);
echo $customers;
?>

Remember to replace 'consumer_key:consumer_secret' in the CURLOPT_USERPWD line with the Consumer Key and Consumer Secret values you generated earlier.

Conclusion

By following these steps, you should now be able to work seamlessly with Magento’s REST API locally on your XAMPP server without encountering the errors mentioned above. Make sure to keep your PHP and Magento installations up-to-date for optimal performance and security.

For more advanced topics and best practices related to Magento development, I highly recommend exploring our website at https://laravelcompany.com. We provide in-depth tutorials, troubleshooting guides, and expert insights on various aspects of Magento development.

API Extensions Customers Errors

Our network