1

I am new in PHP and working to modify one API. Its built with Laravel Framework. I have API function like below in my controller.

public function DeleteOneMail(Request $request)
{
    $uid = $request->uid;

    if (\Request::is('api/*')) {
        if ($request->has('key')) {
            if (in_array($request->input('key'), explode(',', env('API_KEY')))) {
                if ($uid == '') {
                    return response()->make('Please Enter UID', 401);
                } else {
                    $client = Client::account('default');
                    $client->connect();
                    $inbox = $client->getFolder('INBOX');
                    $message = $inbox->getMessage($uid);
                    if ($message) {
                        return response(['success' => 1], 200);
                    } else {
                        return response(['success' => 0, 'message' => 'No Data Found'], 200); 
                    }
                }
            } else {
                return response()->make('Unauthorized Access. Please enter correct API Key', 401);
            }
        } else {
            return response()->make('Unauthorized Access. Please enter correct API Key', 401);
        }
    }
}

I am calling API like below

https://example.com/api/delete-one-mail?key=2221212&uid=214

Its working fine without any issue. Now I want pass multiple uid with request and so I can process that uid one by one with my api function. I am not getting idea how I can pass arrary and process it. Let me know if any expert can help me for solve my puzzle. Thanks!

4
  • I'd suggest if you're willing to remove multiple uid's, pass them via post body as an array, rather than through get. Will be much cleaner solution. Commented Feb 25, 2020 at 17:09
  • 1
    i agree with Ed T. but if you really want in GET request, you can use a list like &uid=214,215,217 Commented Feb 25, 2020 at 17:11
  • Also, in the part where you return "No Data Found", you're also sending 200 status code, that's a bit incorrect. Following RESTful principles, you should return 404 or 400, can't remember which one exactly suits the case. Commented Feb 25, 2020 at 17:13
  • 1
    @EdT. that would be 400. 404 is for when there's no matching route found. 400 would be more suitable if a route was found, but there is no data found. Commented Feb 25, 2020 at 17:14

2 Answers 2

1

Your can pass an array like this

https://example.com/api/delete-one-mail?key=2221212&uid[]=214&uid[]=111&uid[]=222

$request->uid should be an array but you can make sure (if anyone use the old url with ony one uid) by doing

$uids = Arr::wrap($request->uid);
Sign up to request clarification or add additional context in comments.

Comments

1

If you want to send an array by GET request just use []

https://example.com/api/delete-one-mail?key=2221212&uid[]=1&uid[]=2

In your controller you will get an array

$uid = $request->uid;
dd($uid);

As a result you will get

[1, 2]

2 Comments

I do not understood what is dd($uid); here. Thanks!
dd() is laravel helper function. It means Dump and Die, try and you'll see the best output ever)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.