Issue
I have 2 apps, 1 is front and and the other 1 is webapi server.
In webapi, i have to exec a Store Procedure on mssql server. That SP has 52 input parameters. I want to call that SP in My API Controller. My question is how to pass those 52 parameter from my Front end to my API server. I'm successfully testing with static parameter in Postman.
The frontend is Laravel 5.8 and my API server is Laravel 6.2
Here's my API controller, still with static parameters that I want to get as an array '$allmyparams' (if possible) from my Frontend
public function as_add_reg($allmyparams)
{
//these are static params for testing purpose
$param1 = "ID02007160013";
$param2 = "2020-07-17 00:00:00";
$param3 = "00-00-77-32";
$param4 = '00128';
$param5 = '02 ';
$param6 = '11:27';
$param7 = '06 ';
$param8 = ' ';
$param9 = 'DESY ARIANA';
$param10 = '';
$param11 = 'RAYA KRONJO ';
$param12 = 'TANGERANG';
$param13 = ' ';
$param14 = ' ';
$param15 = ' ';
$param16 = ' ';
$param17 = ' ';
$param18 = 'TANGERANG ';
$param19 = '002 ';
$param20 = 'P';
$param21 = ' ';
$param22 = '1978-02-03 00:00:00';
$param23 = 'none ';
$param24 = ' ';
$param25 = '00002 ';
$param26 = '$0.0000';
$param27 = ' ';
$param28 = ' ';
$param29 = 'none ';
$param30 = 0;
$param31 = 42;
$param32 = 5;
$param33 = 13;
$param34 = '2020-07-16 11:28:44.920';
$param35 = 'mssql';
$param36 = 0;
$param37 = ' ';
$param38 = ' ';
$param39 = '2020-07-16 00:00:00';
$param40 = 'none ';
$param41 = '';
$param42 = '';
$param43 = ' ';
$param44 = 'none ';
$param45 = '';
$param46 = '';
$param47 = 'none ';
$param48 = 'none ';
$param49 = 'none ';
$param50 = 0;
$param51 = 0;
$param52 = '1';
$referral_registers = DB::connection('as_api')
->select('EXEC reg_Insert ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?',
array($param1, $param2, $param3, $param4, $param5, $param6, $param7, $param8, $param9, $param10,
$param11, $param12, $param13, $param14, $param15, $param16, $param17, $param18, $param19, $param20,
$param21, $param22, $param23, $param24, $param25, $param26, $param27, $param28, $param29, $param30,
$param31, $param32, $param33, $param34, $param35, $param36, $param37, $param38, $param39, $param40,
$param41, $param42, $param43, $param44, $param45, $param46, $param47, $param48, $param49, $param50,
$param51, $param52));
return response()->json([
'success' => true,
'data' => $referral_registers
]);
}
And In the frontend, here's the function that calls that webAPI server
$allmyparam=arrray(
"param1"=>1',
"param2"=>"example",
...
"param52"=>"example",
);
$response2 = $client->get($webapi_url . '/api/heru/as_add_reg/' . $allmyparams, [
'cookies' => $cookieJar,
'headers' => [
'Authorization' => 'Bearer ' . $webapi_token,
'Accept' => 'application/json',
],
]);
Solution
Without arguing the solution you share, here.
i think you are looking for:
public function controller_method(Request $request) {
$request->all();
}
See: https://laravel.com/docs/7.x/requests To get a more detailed explanation on how to handle request input
Or.. if you are having a flat array with only values, and want to get it as as array with key and values
You could use collection helpers of laravel mapWithKeys
See: https://laravel.com/docs/7.x/collections#method-mapwithkeys
Based on your comment, i guess you don't get what i mean..
$param1 = "ID02007160013";
$param2 = "2020-07-17 00:00:00";
$param3 = "00-00-77-32";
// ... and so on
//could be done as
$params[] = "val1"
$params[] = "val2"
// Then for example you can access it as an array
//Just like accessing $params
//If you need it as separate values, on a function argument, you could use //the spread operator like
...$params
See: https://laravel-news.com/spread-operator-in-array-expressions-coming-to-php-7-4
For some examples.. (and only works if you have a recent PHP version)
Then if you want it to convert to a an associative array
foreach ($params as $index => $param) {
$newArray["param${index}"] = $param;
}
You could also use the https://laravel.com/docs/7.x/collections helper methods for these, but guess, these are harder to understand for you for now.
.. Sending it as query parameter
$client->request('GET', 'http://httpbin.org', [
'query' => ['foo' => 'bar']
]);
See: http://docs.guzzlephp.org/en/stable/quickstart.html#query-string-parameters
If you insist having it as an string you could also use the native php function: https://www.php.net/manual/en/function.http-build-query.php
Answered By - Danny Ebbers Answer Checked By - Terry (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.