簡體   English   中英

無法使用PHP和AJAX張貼到Wufoo API

[英]Unable to POST to Wufoo API with PHP and AJAX

我對PHP相當陌生,正在嘗試將信息發送到當前包含以下字段的Wufoo表單。

在此處輸入圖片說明

我正在嘗試向其發布信息,但收到500:內部服務器錯誤和“未捕獲的SyntaxError:JSON中位置2處的意外令牌<”。 在從其他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>

您可以嘗試以下代碼嗎?

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

            ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM