4

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?

5
  • I have exactly the same problem. Did you found the solution yet? Commented Jun 28, 2016 at 11:42
  • @WouterdenOuden: Its not possible to upload file with curl for remote URL. Source here: stackoverflow.com/questions/22736756/… Commented Jun 29, 2016 at 12:48
  • After much trial and error I came to the same conclusion. Commented Jun 29, 2016 at 12:48
  • @WouterdenOuden: Same here. Actually my PHP app was deployed on Heroku which does not has persistent file storage so finding a way to directly upload to storage drive. But I guess I might have to find another option. Commented Jun 29, 2016 at 12:53
  • I just realized that i'm able to get the files locally, because there on the same server. But I will remember this :D Commented Jun 29, 2016 at 13:02

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Try to specify mimeType and name also, in my case :

'file' => new CURLFile($media->sourceUrl(), $media->mimeType(), $media->name())

sourceUrl points to S3 url, mimeType is video/mp4 and the name is video.mp4

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.