PHP:使用远程 URL 上传 CurlFile

标签 php rest curl file-upload

从过去几天开始,我一直在寻找如何使用 Curl 在 PHP >=5.5 中上传文件。终于找到新的CurlFile方法,但无法使用远程 URL。这是我正在使用的代码:

        $access_token = 'MY_API_ACCESS_TOKEN';
        $fields = array(
            "name" => $name,
            "parent" => array(
                "id" => $folder_id
            )
        );
        $another = array(
            'attributes' => json_encode($fields),
            'file' => new CurlFile($remoteUrl)
        );
        $header = array (
            "Authorization: Bearer $access_token",
            "Content-Type: multipart/form-data"
        );
        $options = array(
            CURLOPT_URL => $UPLOAD_URL,
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => $another,
            CURLOPT_HTTPHEADER => $header,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_SSL_VERIFYPEER => false
        );
        $ch = curl_init();
        curl_setopt_array($ch, $options);
        $response = curl_exec($ch);
        curl_close($ch);

更换 $remoteUrl 时使用本地文件路径一切正常。

Q] CurlFile 可以远程上传吗?如果是,我做错了什么?

最佳答案

好的,我找到了答案:Source

无法使用 curl 上传远程文件.必须先在本地下载文件,然后使用 CurlFile 上传。

关于PHP:使用远程 URL 上传 CurlFile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37818268/

相关文章:

php - mysql按记录集数量最少的组选择语句

php - 使用 PHP 生成 OpenOffice calc 文件

rest - Shopify:感谢页面上提供的变量?

asp.net - WCF Restful Web 服务返回 JSON

php - 使用file_get_contents()PHP函数的正确方法

php - Flash 媒体服务器/PHP 应用程序

rest - 如何在 jparepository 中创建自定义查询但返回实体以外的对象?

curl - 如何使用 token 身份验证发出 POST/GET curl 请求?

php - 通过curl在youtube视频上发表评论

php - 为什么会返回 "Undefined Index"错误?