skip to Main Content

Suppose there is a custom API to get giftcode.

The API has Referance and Amount as requird parameters whereas Activated and Quantity as optional parameters

If URL is:

http://ruteurl/rest/V1/giftcode/request?reference=XYZ&amount=50

It should run assuming Activated and Quantity to be 1 by default

If URL is:

http://ruteurl/rest/V1/giftcode/request?reference=XYZ&amount=50&activated=0&quantity=5

the values shoud be as provided in url and not the default ones.

How can we do this using php? (My platform is Magento 2)

2

Answers


  1. This validation can be done in the server side. Make it as a PHP utility.

    function validate_query_params($query_params)
    {
        foreach($query_params as $key => &$value)
        {
            // Check for mandator params 
            // If any mandatory params missing, echo/print response with error 
    
            // else proceed with optional params. Use default values if missing
    
        }
    }
    

    Query params available in $_GET.

    Login or Signup to reply.
  2. Use the Null coalescing operator (??) to get a default value for a non-existing key-value in an array. For example:

    $quantity = $_GET['quantity'] ?? 1;
    $activated = $_GET['activated'] ?? 1;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search