Submitted form details into JSON via PHP and sent to server












-1














I've created a few PHP API files that takes submitted form information and compile it as a string and sent the information to a web server via cURL e.g.:



$FirstName = $_GET['firstname'];
$LastName = $_GET['lastname'];
$PhoneNumber = $_GET['phone'];
$EmailAddress = $_GET['email'];

$array = array(
'FirstName' => $FirstName,
'LastName' => $LastName,
'PhoneNumber' => $PhoneNumber,
'EmailAddress' => $EmailAddress
);

.... etc.


I have a client that wants the information to be sent as a JSON file



{
"Client": {
"Firstname" : "####",
"LastName" : "####",
"Contact" : {
"Phonenumber" : "####",
"Emailaddress" : "####"
}
}
}


My questions is:




  1. How do I compile JSON in a PHP file (if possible)?


Can it work like this...?



{
"Client": {
"Firstname" : $FirstName,
"LastName" : $LastName,
"Contact" : {
"Phonenumber" : $PhoneNumber,
"Emailaddress" : $EmailAddress
}
}
}



  1. How do I take the information and sent it to the server in JSON format via PHP?


Any help, or a link to resource will be wonderful.



I've never worked with JSON before so this is quite new to me.










share|improve this question


















  • 1




    return your array with json_encode($array); so it look like your expected result
    – Sachin
    Nov 13 '18 at 7:45
















-1














I've created a few PHP API files that takes submitted form information and compile it as a string and sent the information to a web server via cURL e.g.:



$FirstName = $_GET['firstname'];
$LastName = $_GET['lastname'];
$PhoneNumber = $_GET['phone'];
$EmailAddress = $_GET['email'];

$array = array(
'FirstName' => $FirstName,
'LastName' => $LastName,
'PhoneNumber' => $PhoneNumber,
'EmailAddress' => $EmailAddress
);

.... etc.


I have a client that wants the information to be sent as a JSON file



{
"Client": {
"Firstname" : "####",
"LastName" : "####",
"Contact" : {
"Phonenumber" : "####",
"Emailaddress" : "####"
}
}
}


My questions is:




  1. How do I compile JSON in a PHP file (if possible)?


Can it work like this...?



{
"Client": {
"Firstname" : $FirstName,
"LastName" : $LastName,
"Contact" : {
"Phonenumber" : $PhoneNumber,
"Emailaddress" : $EmailAddress
}
}
}



  1. How do I take the information and sent it to the server in JSON format via PHP?


Any help, or a link to resource will be wonderful.



I've never worked with JSON before so this is quite new to me.










share|improve this question


















  • 1




    return your array with json_encode($array); so it look like your expected result
    – Sachin
    Nov 13 '18 at 7:45














-1












-1








-1







I've created a few PHP API files that takes submitted form information and compile it as a string and sent the information to a web server via cURL e.g.:



$FirstName = $_GET['firstname'];
$LastName = $_GET['lastname'];
$PhoneNumber = $_GET['phone'];
$EmailAddress = $_GET['email'];

$array = array(
'FirstName' => $FirstName,
'LastName' => $LastName,
'PhoneNumber' => $PhoneNumber,
'EmailAddress' => $EmailAddress
);

.... etc.


I have a client that wants the information to be sent as a JSON file



{
"Client": {
"Firstname" : "####",
"LastName" : "####",
"Contact" : {
"Phonenumber" : "####",
"Emailaddress" : "####"
}
}
}


My questions is:




  1. How do I compile JSON in a PHP file (if possible)?


Can it work like this...?



{
"Client": {
"Firstname" : $FirstName,
"LastName" : $LastName,
"Contact" : {
"Phonenumber" : $PhoneNumber,
"Emailaddress" : $EmailAddress
}
}
}



  1. How do I take the information and sent it to the server in JSON format via PHP?


Any help, or a link to resource will be wonderful.



I've never worked with JSON before so this is quite new to me.










share|improve this question













I've created a few PHP API files that takes submitted form information and compile it as a string and sent the information to a web server via cURL e.g.:



$FirstName = $_GET['firstname'];
$LastName = $_GET['lastname'];
$PhoneNumber = $_GET['phone'];
$EmailAddress = $_GET['email'];

$array = array(
'FirstName' => $FirstName,
'LastName' => $LastName,
'PhoneNumber' => $PhoneNumber,
'EmailAddress' => $EmailAddress
);

.... etc.


I have a client that wants the information to be sent as a JSON file



{
"Client": {
"Firstname" : "####",
"LastName" : "####",
"Contact" : {
"Phonenumber" : "####",
"Emailaddress" : "####"
}
}
}


My questions is:




  1. How do I compile JSON in a PHP file (if possible)?


Can it work like this...?



{
"Client": {
"Firstname" : $FirstName,
"LastName" : $LastName,
"Contact" : {
"Phonenumber" : $PhoneNumber,
"Emailaddress" : $EmailAddress
}
}
}



  1. How do I take the information and sent it to the server in JSON format via PHP?


Any help, or a link to resource will be wonderful.



I've never worked with JSON before so this is quite new to me.







php json






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 7:38









cgaybbacgaybba

457




457








  • 1




    return your array with json_encode($array); so it look like your expected result
    – Sachin
    Nov 13 '18 at 7:45














  • 1




    return your array with json_encode($array); so it look like your expected result
    – Sachin
    Nov 13 '18 at 7:45








1




1




return your array with json_encode($array); so it look like your expected result
– Sachin
Nov 13 '18 at 7:45




return your array with json_encode($array); so it look like your expected result
– Sachin
Nov 13 '18 at 7:45












2 Answers
2






active

oldest

votes


















1














use json_encode() like below'



$array = array(
'Client'=>[
'FirstName' => $FirstName,
'LastName' => $LastName,
'Contact' => ['PhoneNumber' => $PhoneNumber,'EmailAddress' => $EmailAddress]
]);
$data = json_encode($array);


and pass this $data to cURL






share|improve this answer





























    1














    Copy your $_GET to a new array, add a new Contact entry to this array, passing it an array with Phonenumber & Emailaddress a values, and assign them from your original $_GET, then json_encode($newArray) to return a JSON representation of the array that can be sent to the server.



    $payload = $_GET; //If this only contains the data you want to send to the server, otherwise create new array using required values.
    $payload['Contact'] = ["Phonenumber" => $payload["Phonenumber"], "Emailaddress" => $payload["Emailaddress"]];
    unset($payload["Phonenumber"]);
    unset($payload["Emailaddress"]);
    //Array is converted to JSON below
    $payload = json_encode(["Client" => $payload]);





    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%2f53276045%2fsubmitted-form-details-into-json-via-php-and-sent-to-server%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      use json_encode() like below'



      $array = array(
      'Client'=>[
      'FirstName' => $FirstName,
      'LastName' => $LastName,
      'Contact' => ['PhoneNumber' => $PhoneNumber,'EmailAddress' => $EmailAddress]
      ]);
      $data = json_encode($array);


      and pass this $data to cURL






      share|improve this answer


























        1














        use json_encode() like below'



        $array = array(
        'Client'=>[
        'FirstName' => $FirstName,
        'LastName' => $LastName,
        'Contact' => ['PhoneNumber' => $PhoneNumber,'EmailAddress' => $EmailAddress]
        ]);
        $data = json_encode($array);


        and pass this $data to cURL






        share|improve this answer
























          1












          1








          1






          use json_encode() like below'



          $array = array(
          'Client'=>[
          'FirstName' => $FirstName,
          'LastName' => $LastName,
          'Contact' => ['PhoneNumber' => $PhoneNumber,'EmailAddress' => $EmailAddress]
          ]);
          $data = json_encode($array);


          and pass this $data to cURL






          share|improve this answer












          use json_encode() like below'



          $array = array(
          'Client'=>[
          'FirstName' => $FirstName,
          'LastName' => $LastName,
          'Contact' => ['PhoneNumber' => $PhoneNumber,'EmailAddress' => $EmailAddress]
          ]);
          $data = json_encode($array);


          and pass this $data to cURL







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 7:46









          suresh bambhaniyasuresh bambhaniya

          863113




          863113

























              1














              Copy your $_GET to a new array, add a new Contact entry to this array, passing it an array with Phonenumber & Emailaddress a values, and assign them from your original $_GET, then json_encode($newArray) to return a JSON representation of the array that can be sent to the server.



              $payload = $_GET; //If this only contains the data you want to send to the server, otherwise create new array using required values.
              $payload['Contact'] = ["Phonenumber" => $payload["Phonenumber"], "Emailaddress" => $payload["Emailaddress"]];
              unset($payload["Phonenumber"]);
              unset($payload["Emailaddress"]);
              //Array is converted to JSON below
              $payload = json_encode(["Client" => $payload]);





              share|improve this answer




























                1














                Copy your $_GET to a new array, add a new Contact entry to this array, passing it an array with Phonenumber & Emailaddress a values, and assign them from your original $_GET, then json_encode($newArray) to return a JSON representation of the array that can be sent to the server.



                $payload = $_GET; //If this only contains the data you want to send to the server, otherwise create new array using required values.
                $payload['Contact'] = ["Phonenumber" => $payload["Phonenumber"], "Emailaddress" => $payload["Emailaddress"]];
                unset($payload["Phonenumber"]);
                unset($payload["Emailaddress"]);
                //Array is converted to JSON below
                $payload = json_encode(["Client" => $payload]);





                share|improve this answer


























                  1












                  1








                  1






                  Copy your $_GET to a new array, add a new Contact entry to this array, passing it an array with Phonenumber & Emailaddress a values, and assign them from your original $_GET, then json_encode($newArray) to return a JSON representation of the array that can be sent to the server.



                  $payload = $_GET; //If this only contains the data you want to send to the server, otherwise create new array using required values.
                  $payload['Contact'] = ["Phonenumber" => $payload["Phonenumber"], "Emailaddress" => $payload["Emailaddress"]];
                  unset($payload["Phonenumber"]);
                  unset($payload["Emailaddress"]);
                  //Array is converted to JSON below
                  $payload = json_encode(["Client" => $payload]);





                  share|improve this answer














                  Copy your $_GET to a new array, add a new Contact entry to this array, passing it an array with Phonenumber & Emailaddress a values, and assign them from your original $_GET, then json_encode($newArray) to return a JSON representation of the array that can be sent to the server.



                  $payload = $_GET; //If this only contains the data you want to send to the server, otherwise create new array using required values.
                  $payload['Contact'] = ["Phonenumber" => $payload["Phonenumber"], "Emailaddress" => $payload["Emailaddress"]];
                  unset($payload["Phonenumber"]);
                  unset($payload["Emailaddress"]);
                  //Array is converted to JSON below
                  $payload = json_encode(["Client" => $payload]);






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 13 '18 at 7:55

























                  answered Nov 13 '18 at 7:46









                  Ikenna Anthony OkaforIkenna Anthony Okafor

                  324310




                  324310






























                      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.





                      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.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53276045%2fsubmitted-form-details-into-json-via-php-and-sent-to-server%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

                      Xamarin.iOS Cant Deploy on Iphone

                      Glorious Revolution

                      Dulmage-Mendelsohn matrix decomposition in Python