简体   繁体   中英

Unable to POST to Wufoo API with PHP and AJAX

I am fairly new to PHP and am trying to send information to a Wufoo Form that currently contains the following fields.

在此处输入图片说明

I am trying to POST information to it but am getting a 500: Internal Server Error and 'Uncaught SyntaxError: Unexpected token < in JSON at position 2'. I am unsure what I am doing wrong as I typed this up after researching from other Stack Overflow questions. My PHP file is names send.php. Here is the PHP Code I have placed in my server :

<?php

$First = isset($_POST['First']) ? $_POST['First'] : null;
$Last = isset($_POST['Last']) ? $_POST['Last'] : null;
//$comment = isset($_POST['comment']) ? $_POST['comment']: null;

$ref = curl_init('https://shsabhlok.wufoo.com/api/v3/forms/happy-go-lucky/entries.json'); 
curl_setopt($ref, CURLOPT_HTTPHEADER, array('Content-type: multipart/form-data'));
curl_setopt($ref, CURLOPT_POST, true);
curl_setopt($ref, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ref, CURLOPT_POSTFIELDS, array('Field1' => $First, 'Field2' => $Last));     
curl_setopt($ref, CURLOPT_USERPWD, '2222-2222-2222-2222');
curl_setopt($ref, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ref, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ref, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ref, CURLOPT_FOLLOWLOCATION, true);

$response = curl_exec($ref);
$responseStatus = curl_getinfo($ref);

if ($responseStatus['http_code'] == 201)
{
    echo json_encode(array('error' => 'Sent'));
}
else
{
    // http_response_code(500);
    header('X-PHP-Response-Code: 500', true, 500);
    echo json_encode(array('error' => 'Internal server error' . var_dump($responseStatus)));
}

?>

Here is the JS and HTML I am using :

<body>
      <div class="input-group">
        <label class="col-sm-6 control-label">First Name :</label> 
        <div class="col-sm-10">
          <input id="firstName"type="text" class="form-control" placeholder="First Name" aria-describedby="mi-fn">
        </div>
      </div>


      <div class="input-group">
        <label class="col-sm-6 control-label">Last Name :</label> 
        <div class="col-sm-10">
          <input id="lastName"type="text" class="form-control" placeholder="Last Name" aria-describedby="mi-ln">
        </div>
      </div>
      <div>
       <button type="submit" class="btn btn-primary" id="send-email" data-loading-text="Sending...">Submit</button>
      </div>
      </body>


<script>

      // email modal
      $('#send-email').click(function(e) {

        $('#send-email').button('loading');

        $.ajax({
          url: 'send.php',
          type: 'post',
          data: {'First': $('#firstName').val(), 'Last': $('#lastName').val()},
          success: function(result) {
            $('#send-email').button('reset');
            console.log("hello");
          },
          error: function(xhr) {
            var error = JSON.parse(xhr.responseText);

            var errorString = typeof(error.error) != "undefined" ? error.error : "Sorry, there was an error. Please try again later.";

            alert(errorString);
            $('#send-email').button('reset');
          }
        });
      });
  </script>

Can you please try with below code?

    <?php

            $First = isset($_POST['First']) ? $_POST['First'] : null;
            $Last = isset($_POST['Last']) ? $_POST['Last'] : null;
            //$comment = isset($_POST['comment']) ? $_POST['comment']: null;

            $ref = curl_init('https://shsabhlok.wufoo.com/api/v3/forms/happy-go-lucky/entries.json'); 
            curl_setopt($ref, CURLOPT_HTTPHEADER, array('Content-type: multipart/form-data'));
            // Add http header parameter
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                         
                         'Content-Type:application/json'                                                                       
            ); 
            curl_setopt($ref, CURLOPT_POST, true);
            curl_setopt($ref, CURLOPT_RETURNTRANSFER, 1);
            //changes here , we need to pass data into json format.
            curl_setopt($ref, CURLOPT_POSTFIELDS, json_encode(array('Field1' => $First, 'Field2' => $Last)));     
            curl_setopt($ref, CURLOPT_USERPWD, '2222-2222-2222-2222');
            curl_setopt($ref, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
            curl_setopt($ref, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ref, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($ref, CURLOPT_FOLLOWLOCATION, true);

            $response = curl_exec($ref);
            $responseStatus = curl_getinfo($ref);

            if ($responseStatus['http_code'] == 201)
            {
                echo json_encode(array('error' => 'Sent'));
            }
            else
            {
                // http_response_code(500);
                header('X-PHP-Response-Code: 500', true, 500);
                echo json_encode(array('error' => 'Internal server error' . var_dump($responseStatus)));
            }

            ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM