Issue posting a PHP filtered array to an API
up vote
0
down vote
favorite
Am working on a form whereby the data submitted the user am posting to an API via PHP curl. From the form, I have inputs that are controlled by a drop-down list where the user fills children details. It all works fine except that I need to post the inputs from the drop-down on the API which throws an error. The origin of the error is problem posting the filtered results from the array...
~ Please assist?
Am array containing Children inputs from a dropdown
$a= [
'child1'=> isset($request->childDetail1Dob) ? $request->childDetail1Dob : null,
'child2'=> isset($request->childDetail2Dob) ? $request->childDetail2Dob : null,
'child3'=> isset($request->childDetail3Dob) ? $request->childDetail3Dob : null,
'child4'=> isset($request->childDetail4Dob) ? $request->childDetail4Dob : null,
'child5'=> isset($request->childDetail5Dob) ? $request->childDetail5Dob : null,
];
Filter array to eliminate fields with null values
$b = array_filter($a, function($k) use ($a) { return $k!=null; });
Data am trying to post
$data = array(
'DobPrincipalTraveller' => "1992-02-05",
'TravelStartDate' => "2018-11-23",
'TravelEndDate' => "2018-11-30",
'CoverOption' => "Grade 4",
'DobOfSpouse' => "0000-00-00",
'Children' => $b,
'WithSpouse' => $request->spouse == null ? '0' : '1'
);
Posts data to the API via POST
$travelplan = $this->global_Curl_Meta(
$data, 'api/travel/get-plans')->data;
Curl function
public function global_Curl_Meta($data, $url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, (env('API_ENDPOINT_NGINX_IP') . '/' . $url));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
//Post and convert array to JSON
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
$response = json_decode(curl_exec($ch));
curl_close($ch);
return $response;
}
Sample data required by the API
{"DobPrincipalTraveller":"1978-01-22",
"TravelStartDate":"2018-11-22",
"TravelEndDate":"2018-11-25",
"CoverOption":"Standard",
"WithSpouse":"1",
"DobOfSpouse":"02-08-1979",
"Children":[
{"DateOfBirth":"2015-05-23"},
{"DateOfBirth":"2016-09-13"}
]
}
php api arraylist
add a comment |
up vote
0
down vote
favorite
Am working on a form whereby the data submitted the user am posting to an API via PHP curl. From the form, I have inputs that are controlled by a drop-down list where the user fills children details. It all works fine except that I need to post the inputs from the drop-down on the API which throws an error. The origin of the error is problem posting the filtered results from the array...
~ Please assist?
Am array containing Children inputs from a dropdown
$a= [
'child1'=> isset($request->childDetail1Dob) ? $request->childDetail1Dob : null,
'child2'=> isset($request->childDetail2Dob) ? $request->childDetail2Dob : null,
'child3'=> isset($request->childDetail3Dob) ? $request->childDetail3Dob : null,
'child4'=> isset($request->childDetail4Dob) ? $request->childDetail4Dob : null,
'child5'=> isset($request->childDetail5Dob) ? $request->childDetail5Dob : null,
];
Filter array to eliminate fields with null values
$b = array_filter($a, function($k) use ($a) { return $k!=null; });
Data am trying to post
$data = array(
'DobPrincipalTraveller' => "1992-02-05",
'TravelStartDate' => "2018-11-23",
'TravelEndDate' => "2018-11-30",
'CoverOption' => "Grade 4",
'DobOfSpouse' => "0000-00-00",
'Children' => $b,
'WithSpouse' => $request->spouse == null ? '0' : '1'
);
Posts data to the API via POST
$travelplan = $this->global_Curl_Meta(
$data, 'api/travel/get-plans')->data;
Curl function
public function global_Curl_Meta($data, $url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, (env('API_ENDPOINT_NGINX_IP') . '/' . $url));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
//Post and convert array to JSON
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
$response = json_decode(curl_exec($ch));
curl_close($ch);
return $response;
}
Sample data required by the API
{"DobPrincipalTraveller":"1978-01-22",
"TravelStartDate":"2018-11-22",
"TravelEndDate":"2018-11-25",
"CoverOption":"Standard",
"WithSpouse":"1",
"DobOfSpouse":"02-08-1979",
"Children":[
{"DateOfBirth":"2015-05-23"},
{"DateOfBirth":"2016-09-13"}
]
}
php api arraylist
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Am working on a form whereby the data submitted the user am posting to an API via PHP curl. From the form, I have inputs that are controlled by a drop-down list where the user fills children details. It all works fine except that I need to post the inputs from the drop-down on the API which throws an error. The origin of the error is problem posting the filtered results from the array...
~ Please assist?
Am array containing Children inputs from a dropdown
$a= [
'child1'=> isset($request->childDetail1Dob) ? $request->childDetail1Dob : null,
'child2'=> isset($request->childDetail2Dob) ? $request->childDetail2Dob : null,
'child3'=> isset($request->childDetail3Dob) ? $request->childDetail3Dob : null,
'child4'=> isset($request->childDetail4Dob) ? $request->childDetail4Dob : null,
'child5'=> isset($request->childDetail5Dob) ? $request->childDetail5Dob : null,
];
Filter array to eliminate fields with null values
$b = array_filter($a, function($k) use ($a) { return $k!=null; });
Data am trying to post
$data = array(
'DobPrincipalTraveller' => "1992-02-05",
'TravelStartDate' => "2018-11-23",
'TravelEndDate' => "2018-11-30",
'CoverOption' => "Grade 4",
'DobOfSpouse' => "0000-00-00",
'Children' => $b,
'WithSpouse' => $request->spouse == null ? '0' : '1'
);
Posts data to the API via POST
$travelplan = $this->global_Curl_Meta(
$data, 'api/travel/get-plans')->data;
Curl function
public function global_Curl_Meta($data, $url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, (env('API_ENDPOINT_NGINX_IP') . '/' . $url));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
//Post and convert array to JSON
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
$response = json_decode(curl_exec($ch));
curl_close($ch);
return $response;
}
Sample data required by the API
{"DobPrincipalTraveller":"1978-01-22",
"TravelStartDate":"2018-11-22",
"TravelEndDate":"2018-11-25",
"CoverOption":"Standard",
"WithSpouse":"1",
"DobOfSpouse":"02-08-1979",
"Children":[
{"DateOfBirth":"2015-05-23"},
{"DateOfBirth":"2016-09-13"}
]
}
php api arraylist
Am working on a form whereby the data submitted the user am posting to an API via PHP curl. From the form, I have inputs that are controlled by a drop-down list where the user fills children details. It all works fine except that I need to post the inputs from the drop-down on the API which throws an error. The origin of the error is problem posting the filtered results from the array...
~ Please assist?
Am array containing Children inputs from a dropdown
$a= [
'child1'=> isset($request->childDetail1Dob) ? $request->childDetail1Dob : null,
'child2'=> isset($request->childDetail2Dob) ? $request->childDetail2Dob : null,
'child3'=> isset($request->childDetail3Dob) ? $request->childDetail3Dob : null,
'child4'=> isset($request->childDetail4Dob) ? $request->childDetail4Dob : null,
'child5'=> isset($request->childDetail5Dob) ? $request->childDetail5Dob : null,
];
Filter array to eliminate fields with null values
$b = array_filter($a, function($k) use ($a) { return $k!=null; });
Data am trying to post
$data = array(
'DobPrincipalTraveller' => "1992-02-05",
'TravelStartDate' => "2018-11-23",
'TravelEndDate' => "2018-11-30",
'CoverOption' => "Grade 4",
'DobOfSpouse' => "0000-00-00",
'Children' => $b,
'WithSpouse' => $request->spouse == null ? '0' : '1'
);
Posts data to the API via POST
$travelplan = $this->global_Curl_Meta(
$data, 'api/travel/get-plans')->data;
Curl function
public function global_Curl_Meta($data, $url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, (env('API_ENDPOINT_NGINX_IP') . '/' . $url));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
//Post and convert array to JSON
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
$response = json_decode(curl_exec($ch));
curl_close($ch);
return $response;
}
Sample data required by the API
{"DobPrincipalTraveller":"1978-01-22",
"TravelStartDate":"2018-11-22",
"TravelEndDate":"2018-11-25",
"CoverOption":"Standard",
"WithSpouse":"1",
"DobOfSpouse":"02-08-1979",
"Children":[
{"DateOfBirth":"2015-05-23"},
{"DateOfBirth":"2016-09-13"}
]
}
php api arraylist
php api arraylist
edited Nov 22 at 5:50
asked Nov 22 at 5:40
Patweb
666
666
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
instead of $b = array_filter($a, function($k) use ($a) { return $k!=null; });
use
$b = ;
foreach($a as $val){
if($val){
$b['DateOfBirth'] = $val;
}
}
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
instead of $b = array_filter($a, function($k) use ($a) { return $k!=null; });
use
$b = ;
foreach($a as $val){
if($val){
$b['DateOfBirth'] = $val;
}
}
add a comment |
up vote
0
down vote
accepted
instead of $b = array_filter($a, function($k) use ($a) { return $k!=null; });
use
$b = ;
foreach($a as $val){
if($val){
$b['DateOfBirth'] = $val;
}
}
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
instead of $b = array_filter($a, function($k) use ($a) { return $k!=null; });
use
$b = ;
foreach($a as $val){
if($val){
$b['DateOfBirth'] = $val;
}
}
instead of $b = array_filter($a, function($k) use ($a) { return $k!=null; });
use
$b = ;
foreach($a as $val){
if($val){
$b['DateOfBirth'] = $val;
}
}
answered Nov 22 at 5:55
suresh bambhaniya
834113
834113
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424519%2fissue-posting-a-php-filtered-array-to-an-api%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown