关键跟踪器 API 文件上传

Pivotal Tracker API File Upload

我有这张表格可以上传文件。我希望将其作为 API 的评论上传到 Pivotal Tracker 中。我将在代码中写什么?我是 Curl 的新手。

编辑: 在 Pivotal Tracker 中,您可以在评论中上传文件 .jpg、.xls 等,它会创建一个可下载的 icon/thumbnail。

评论文档:https://www.pivotaltracker.com/help/api/rest/v5#projects_project_id_stories_story_id_comments_post

文件附件文档: 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;
}

这可能不是正确的方法,但它确实有效!

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