【问题标题】:Unable to POST to Wufoo API with PHP and AJAX无法使用 PHP 和 AJAX 发布到 Wufoo API
【发布时间】:2016-07-14 14:58:53
【问题描述】:

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

我正在尝试向其发布信息,但收到 500:内部服务器错误和“未捕获的语法错误:位置 2 的 JSON 中的意外令牌

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

【问题讨论】:

    标签: javascript php jquery ajax wufoo


    【解决方案1】:

    你能试试下面的代码吗?

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

    【讨论】:

    • 嘿!刚刚尝试过 - 仍然得到内部服务器错误 - 唯一的区别是'Uncaught SyntaxError:Unexpected token
    • 只是改进答案。你能检查一下吗?
    • 嗨 Bhavin - 现在没有出现错误,但 Wufoo 中没有出现条目。在 JSON 中,entry 数组为空。
    • 打印$responseStatus结果
    • 嗨!我在“$responseStatus = curl_getinfo($ref);”下面使用它- " echo json_encode(array('error' => '内部服务器错误' . var_dump($responseStatus)));"这不起作用。
    猜你喜欢
    • 1970-01-01
    • 2016-04-08
    • 2015-01-21
    • 1970-01-01
    • 1970-01-01
    • 2018-02-12
    • 2012-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多