rajrathod
rajrathod

Reputation: 89

Passing Multiple Value For PHP API Function

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!

Upvotes: 1

Views: 1124

Answers (2)

Ruben Danielyan
Ruben Danielyan

Reputation: 768

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]

Upvotes: 1

Sébastien R
Sébastien R

Reputation: 96

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);

Upvotes: 1

Related Questions