【问题标题】:Active Collab file upload using PHP SDK使用 PHP SDK 上传 Active Collab 文件
【发布时间】:2016-03-17 01:45:45
【问题描述】:

我刚开始使用 this 文档使用 API,但在上传文件时遇到了问题。我没有从对上传文件的 api 调用中得到任何响应。我尝试了多种文件类型,但没有成功。这是我的 API 调用:

$client = new \ActiveCollab\SDK\Client($token);
try {
    $response = $client->post('upload-files',[
        [
            'test-file.txt',
            'text\/plain'
        ]
    ]);

    echo $response->getBody();
} catch(AppException $e) {
    print $e->getMessage() . '<br><br>';
}

根据文档,我应该收到包含 文件上传代码,但我没有收到任何响应,甚至没有收到验证错误。它也不会抛出异常。我对任何其他请求都没有问题,所以我认为这不是身份验证问题。谁能帮我弄清楚我做错了什么?

【问题讨论】:

    标签: php api activecollab


    【解决方案1】:

    为了上传文件,您应该传递一个文件路径数组或路径 - MIME 类型对作为客户端 post 方法的第三个参数:

    $response = $client->post('upload-files', [], ['/path/to/file.png']);
    $response = $client->post('upload-files', [], ['/path/to/file.png' => 'image/png']);
    

    仅当给定路径(本例中为/path/to/file.png)上的文件存在且可读时才有效。

    【讨论】:

    • 感谢您的回复!不过,我仍然遇到一些麻烦,当我尝试第一种方式$response = $client-&gt;post('upload-files', [], ['images/test-image']); 时,我得到了异常ErrorException in Client.php line 222: File 'images/test-image' is not readable。当我尝试第二种方式$response = $client-&gt;post('upload-files', [], ['images/test-image.jpg' =&gt; 'image/jpeg']); 我得到回复ErrorException in Client.php line 222: File 'image/jpeg' is not readable,知道我做错了什么吗?我认为该文件很好,在我们的整个应用程序中使用它没有问题。
    • 您没有提供有效的文件路径。我建议您在 sile 系统上使用文件的绝对路径:/tmp/file-that-i-want-to-upload.jpgc:\MyFolder\MyFile.jpg
    • 我已经更新了答案,以明确您尝试上传的文件需要存在于文件系统上。
    【解决方案2】:

    遇到了同样的问题,我是这样解决的:

    从 SDK 中的 post 方法内部:

    if (is_array($file)) {
        list($path, $mime_type) = $file;
    }
    

    来自 php.net:

    In PHP 5, list() assigns the values starting with the right-most parameter.
    In PHP 7, list() starts with the left-most parameter.
    

    我使用的是 php 5.6,所以我换了:

    ['/path/to/file.png' => 'image/png']
    

    收件人:

    ['image/png' => '/path/to/file.png']
    

    现在按预期工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-26
      • 1970-01-01
      • 2016-06-06
      • 2016-05-17
      相关资源
      最近更新 更多