PHP file returning 500 Internal Server Error

0

I made a php file that makes some requests in an api, but this one giving me error 500 already tried everything here, but I can not figure out what it could be:

This is giving error in the first if, I could not test the other form step. And when I remove the call from the pipefycall function there is no more 500 error, then it is probably in cURL

<?php   

if (!empty($_POST)){
    // FORM STEP 1 
    if($_POST['formstep'] == 1){
      // get all posts
      $nomecompleto = $_POST['nomecompleto'];
      $email = $_POST['email'];
      $telefone = $_POST['tel'];
      $pipeid = 400624;
      $queryObj = [
          'query' => 
          'mutation {
            createCard(
              input: {
                pipe_id: '.$pipeid.'
                fields_attributes: [{
                    field_id: "nome",
                    field_value: "'.$nomecompleto.'"
                } {
                    field_id: "email",
                    field_value: "'.$email.'"
                } {
                    field_id: "telefone",
                    field_value: "'.$telefone.'"
                }]
               }
            ) {
              card {
                id
              }
            }
          }'
      ];
      $query = json_encode($queryObj);
      $response =  pipefycall($query);
      echo $response;

    // FORM STEP 2 
    }else if ($_POST['formstep'] == 2){
        // get some post data and convert if necessary
        $cardid = $_POST['cardid'];      
        $problema = implode (", ", $_POST["problema"]);
        $detalhes = htmlspecialchars($_POST["detalhes"]);
        $destination_phase_id = 3024092;

        // Array of inputs 
        $inputs = array(
          // field_id => new_value
          "motivo_da_viagem"=>$_POST['motivoviagem'],
          "problema_2"=>$problema,
          "detalhes"=>$detalhes
          );

        // LOOP
        foreach($inputs as $field_id => $new_value) {
          // Update card
          $updated = updateCardField($cardid,$field_id,$new_value);
          if($updated == false){
            echo "erro ao atualizar o card - field ".$field_id;
            break;
          }
          // Check if is last element
          end($inputs);
          if ($field_id === key($inputs)){
            $moved = moveCardToPhase($cardid,$destination_phase_id);
            if($moved == false){
              echo "Atualizado porem erro ao mover";
            }else{
              echo 'movido';
            }
          }
        } 

    // FORM STEP 3
    }else if ($_POST['formstep'] == 3){
        $cardid = $_POST['cardid'];
        //"1977-01-20"
        $datadesaidapost = DateTime::createFromFormat('d/m/Y', $_POST['datadesaida']);
        $datadesaida = $datadesaidapost->format('Y-m-d');
        $horadesaida = $_POST['horadesaida'];
        $destination_phase_id =  3024093;

    }

    // updateCardField
    function updateCardField($cardid,$field_id,$new_value){
      // Create query object 
      $queryobj = [
        "query" => "mutation {
          updateCardField(
            input: {
              card_id: '.$cardid.'
              field_id: '.$field_id.'
              new_value: '.$new_value.'
            }
          ) {
            success
          }
        }"
      ];
      // Convert object to json
      $query = json_encode($queryObj);

      // Call API
      $result = pipefycall($query);
      $resultarray = json_decode($result, true);
      $sucess = $resultarray['data']['updateCardField']['success'];

      // sucesso diferente de true não é necessariamente false
      if($sucess != true){
        return false;
      }else{
        return true;
      }     
    }

    // moveCardToPhase
    function moveCardToPhase($cardid,$destination_phase_id){
      // Create query object 
      $queryobj = [
        "query" => "mutation {
          moveCardToPhase(
            input: {
              card_id: '.$cardid.'
              destination_phase_id: '.$destination_phase_id.'
            }
          ) {
            card {
              current_phase{
                name
              }
            }
          }
        }"
      ];
      // Convert object to json
      $query = json_encode($queryObj);

      // Call API
      $result = pipefycall($query);
      $resultarray = json_decode($result, true);
      $sucess = $resultarray['data']['moveCardToPhase'];
      if($sucess == null){
        return false;
      }else{
        return true;
      }     
    }
    // PIPEFY CALL API
    function pipefycall($query){
      $token = 'xxxx';
      $ch = curl_init();
      // set URL and other appropriate options
      $options = array(
                  CURLOPT_URL => 'xxxx',
                  CURLOPT_RETURNTRANSFER => true,
                  CURLOPT_HEADER => false,
                  CURLOPT_POST => true,
                  CURLOPT_HTTPHEADER => array(
                    "Content-Type: application/json",
                    "Authorization: Bearer $token"
                  ),
                  CURLOPT_POSTFIELDS => $query,
                  CURLOPT_FAILONERROR => true
                );
      curl_setopt_array($ch, $options);
      $response = curl_exec($ch);
      if ($response === FALSE) {
          return "cURL Error: " . curl_error($ch);
      } else {
          return $response;
      }
    }
}
    
asked by anonymous 18.04.2018 / 16:20

0 answers