Issue posting a PHP filtered array to an API












0















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"}
]
}









share|improve this question





























    0















    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"}
    ]
    }









    share|improve this question



























      0












      0








      0








      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"}
      ]
      }









      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 5:50







      Patweb

















      asked Nov 22 '18 at 5:40









      PatwebPatweb

      716




      716
























          1 Answer
          1






          active

          oldest

          votes


















          0














          instead of $b = array_filter($a, function($k) use ($a) { return $k!=null; });



          use



          $b = ;
          foreach($a as $val){
          if($val){
          $b['DateOfBirth'] = $val;
          }
          }





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            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

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            instead of $b = array_filter($a, function($k) use ($a) { return $k!=null; });



            use



            $b = ;
            foreach($a as $val){
            if($val){
            $b['DateOfBirth'] = $val;
            }
            }





            share|improve this answer




























              0














              instead of $b = array_filter($a, function($k) use ($a) { return $k!=null; });



              use



              $b = ;
              foreach($a as $val){
              if($val){
              $b['DateOfBirth'] = $val;
              }
              }





              share|improve this answer


























                0












                0








                0







                instead of $b = array_filter($a, function($k) use ($a) { return $k!=null; });



                use



                $b = ;
                foreach($a as $val){
                if($val){
                $b['DateOfBirth'] = $val;
                }
                }





                share|improve this answer













                instead of $b = array_filter($a, function($k) use ($a) { return $k!=null; });



                use



                $b = ;
                foreach($a as $val){
                if($val){
                $b['DateOfBirth'] = $val;
                }
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 22 '18 at 5:55









                suresh bambhaniyasuresh bambhaniya

                873113




                873113






























                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    Wiesbaden

                    Marschland

                    Dieringhausen