-1

我对 PHP 相当陌生,正在尝试将信息发送到当前包含以下字段的 Wufoo 表单。

在此处输入图像描述

我正在尝试向其发布信息,但收到 500:内部服务器错误和“未捕获的语法错误:位置 2 的 JSON 中的意外令牌 <”。在研究了其他 Stack Overflow 问题后,我不确定自己做错了什么。我的 PHP 文件名为 send.php。这是我放置在服务器中的 PHP 代码:

<?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)));
}

?>

这是我正在使用的 JS 和 HTML:

<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>
4

1 回答 1

0

你能试试下面的代码吗?

    <?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)));
            }

            ?>
于 2016-07-14T06:44:44.130 回答