1

I've got this form to upload a file. I want it to be uploaded as a comment in Pivotal Tracker with the API. What will I write in the code? I'm new to Curl.

Edit: In Pivotal Tracker you can upload file .jpg, .xls, etc in the comments, and it creates an downloadable icon/thumbnail.

Comments Doc: https://www.pivotaltracker.com/help/api/rest/v5#projects_project_id_stories_story_id_comments_post

File attachment Doc: https://www.pivotaltracker.com/help/api/rest/v5#file_attachment_resource

upload.html:

<form action="pt.php" method="post" enctype="multipart/form-data">
  <fieldset>
    <input type="file" name="pt_xls" />
    <input type="submit" name="upload" value="Upload" />
  </fieldset>
</form>

pt.php:

$response = pivotalTracker("https://www.pivotaltracker.com/services/v5/projects/XXXXXXXX/stories/XXXXXXXX/comments");

$resArr = array();
$resArr = json_decode($response);

echo "<pre>"; print_r($resArr); echo "</pre>";

function pivotalTracker($url) {


    $pivotalAPIToken = 'XXXXXXXXXX'; 
    $curlHeader = array("X-TrackerToken: ".$pivotalAPIToken, "Content-type: application/json");

    $options = array(
        CURLOPT_HTTPHEADER => $curlHeader,
        CURLOPT_RETURNTRANSFER => true,  
        CURLOPT_POST => TRUE,
        CURLOPT_POSTFIELDS => json_encode($data),
    ); 

    $ch = curl_init($url);
    curl_setopt_array($ch, $options);

    $content  = curl_exec($ch);

    curl_close($ch);

    return $content;
}
Sight
  • 11
  • 5
  • You should specify what you want as the comment. Is it something within the file? If so, you will need something to read the xls file since it is encoded with Microsoft sauce. – pcnate Mar 08 '17 at 15:57
  • In Pivotal Tracker you can upload file .jpg, .xls, etc in the comments, and it creates an downloadable icon/thumbnail. Comments: https://www.pivotaltracker.com/help/api/rest/v5#projects_project_id_stories_story_id_comments_post File attachment: https://www.pivotaltracker.com/help/api/rest/v5#file_attachment_resource – Sight Mar 09 '17 at 08:14
  • According to the links you provided you need to upload the file separately. When you upload a file, you get an attachment object back. When you create a new comment, you need to attach that object to the data you want to send. – Douwe de Haan Mar 09 '17 at 10:46
  • Thanks! What if I upload the file to my server, then I send it over via Curl, and then I delete it on my server. How will the $data array in my script look like? – Sight Mar 09 '17 at 14:41

1 Answers1

0

This may not be the right way, but it works!

file_in.php

    if(isset($_FILES['file'])){
          $errors= array();
          $theFile = $_FILES['file'];
          $file_name = $_FILES['file']['name'];
          $file_size = $_FILES['file']['size'];
          $file_tmp = $_FILES['file']['tmp_name'];
          $file_type = $_FILES['file']['type'];

          move_uploaded_file($file_tmp, $_SERVER['DOCUMENT_ROOT']."/pt_uploads/".$file_name);

       }

        $target_file = $_SERVER['DOCUMENT_ROOT']."/pt_uploads/".$file_name;
        $response = pivotalTracker("https://www.pivotaltracker.com/services/v5/projects/*ID*/uploads", $file_name, $target_file, $file_type);


        function pivotalTracker($url, $file_name, $target_file, $file_type) {

            $pivotalAPIToken = 'XXXXXXXX';
            $curlHeader = array("X-TrackerToken: ".$pivotalAPIToken, "Content-type: multipart/form-data", "Content-Disposition: form-data;'");


            $cfile = curl_file_create('../pt_uploads/'.$file_name,$file_type,$file_name);

            $data = array('file' => $cfile);

            $options = array(
                CURLOPT_HTTPHEADER => $curlHeader,
                CURLOPT_RETURNTRANSFER => true,  
                CURLOPT_BINARYTRANSFER => true,
                CURLOPT_POST => TRUE,
                CURLOPT_POSTFIELDS => $data,
            ); 

            $ch = curl_init($url);
            curl_setopt_array($ch, $options);

            $content  = curl_exec($ch);

            curl_close($ch);

            unlink($target_file);

            $resArr = array();
            $resArr = json_decode($content);
            $theId = $resArr->id;
            $theName = $resArr->filename;


            header('Location: pt_file_comment.php?pt_file_id='.$theId.'&filename='.$theName);

    }

pt_file_comment.php

$pt_file_id = $_GET['pt_file_id'];
    $pt_file_name = $_GET['filename'];

    $response = pivotalTracker("https://www.pivotaltracker.com/services/v5/projects/*ID*/stories/*ID*/comments", $pt_file_id, $pt_file_name);
    $resArr = array();
    $resArr = json_decode($response);

    echo "<pre>"; print_r($resArr); echo "</pre>";

    function pivotalTracker($url, $pt_file_id, $pt_file_name) {

    $file_data[] = array('id' => $pt_file_id,);
    $fileObject = (object) $file_data;


        $data = array(
            'text'              =>    'file_attachments/'.$pt_file_id.'/'.$pt_file_name,
            'file_attachments'  => $file_data,
        );

        $pivotalAPIToken = 'XXXX';
        $curlHeader = array("X-TrackerToken: ".$pivotalAPIToken, "Content-type: application/json");

        $options = array(
            CURLOPT_HTTPHEADER => $curlHeader,
            CURLOPT_RETURNTRANSFER => true,  
            CURLOPT_POST => TRUE,
            CURLOPT_POSTFIELDS => json_encode($data),
        ); 

        $ch = curl_init($url);
        curl_setopt_array($ch, $options);

        $content  = curl_exec($ch);

        curl_close($ch);
        return $content;
    }
Sight
  • 11
  • 5