2016-07-14 6 views
0

私はPHPをかなり使い慣れており、現在以下のフィールドを含むWufooフォームに情報を送信しようとしています。PHPとAJAXでWufoo APIにPOSTすることができません

enter image description here

私はそれに情報を投稿しようとしていますが、取得しています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> 

答えて

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

      ?> 
+0

Hey!ちょうどそれを試した - まだ内部サーバーのエラーを取得する - 唯一の違いは 'Uncaught SyntaxError:予期しないトークン Shashank

+0

ただ答えをimporve。それを確認していただけますか? –

+0

こんにちはBhavin - 今はエラーにはなりませんが、Wufooではエントリが表示されません。 JSONでは、entries配列は空です。 – Shashank

関連する問題