Search code examples
phprestcurlfile-upload

PHP: CurlFile upload with remote URL


From past few days I have been searching on how to upload file in PHP >=5.5 with Curl. Finally I found the new CurlFile method but cant get it work with remote URLs. Here is the code i am using:

        $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);

When replacing the $remoteUrl with local file path everything works fine.

Q] Is the remote upload possible with CurlFile? If yes what am I doing wrong?


Solution

  • Ok I found the answer: Source

    Its not possible to upload a remote file with curl. One has to download the file locally first and then use CurlFile to upload.