Upload images using CURL with php

Hi,

I’m trying to send images from my server to cloudflare. I’m using laravel with CURL.
First i request the temp URL, as the docs ask to: https://developers.cloudflare.com/images/upload-images/direct-creator-upload and i get the response, after that i’m trying to make a new CURL request with the image to that url. here is my curl:

 $curl = curl_init();
        curl_setopt_array($curl, [
            CURLOPT_URL            => $url, // tmp url provided by cloudflare
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_TIMEOUT        => 30,
            CURLOPT_POST           => true,
            CURLOPT_POSTFIELDS     => ['file'=>new \CURLFile($photo),'image/jpeg','test_name'],
            CURLOPT_HTTPHEADER     => [
                "Content-Type: multipart/form-data; boundary=".Str::random(32)
            ],
        ]);
        $response = curl_exec($curl);
        curl_close($curl);
        var_dump($response);

but the response i get is: ERROR 5450: Error while receiving upload,

Can you tell me what is the problem?

My code using NodeJS, maybe this helps you:

      const form = new FormData();
      form.append('file', fs.createReadStream(`./public/${picture_url}`));

      let request_config = {
        headers: {
          'Authorization': `Bearer ${accessToken}`,
          ...form.getHeaders()
        },
        maxContentLength: Infinity,
        maxBodyLength: Infinity,
      };
      const result = await axios.post(apiUrl, form, request_config);
      const cdnId = result.data.result.id;

Hi, thanks you help but it doesn’t !
but why are you sending the accessToken? you dont need him when submiting the form.
can you tell me what are the other headers that you send? (the …form.getHeaders() what it returns?)
maybe i’m missing some headers…

Actually i need access token for every request.
Header from ...form.getHeaders():

{
  'content-type': 'multipart/form-data; boundary=--------------------------124169886602312898473468'
}

Url is here https://api.cloudflare.com/client/v4/accounts/${accountId}/images/v1

https://developers.cloudflare.com/images/upload-images/direct-creator-upload
you make this request:
curl --request POST
–url https://api.cloudflare.com/client/v4/accounts/:account_id/images/v1/direct_upload
–header ‘Content-Type: application/json’
–header ‘Authorization: Bearer :token’

and you get:
{
“result”: {
“id”: “2cdc28f0-017a-49c4-9ed7-87056c839c2”,
“uploadURL”: “https://upload.imagedelivery.net/2cdc28f0-017a-49c4-9ed7-87056c839c2
},
“result_info”: null,
“success”: true,
“errors”: ,
“messages”:
}
here we have an ‘uploadURL’ where you can send you image.
then you submit the file to this ‘uploadURL’.
here I have that error. and here you dont need the token

Maybe it will be the Facebook token I recently installed. Can you confirm?

i dont understand what facebook has to do here…
can you be more specific?

So, I created the site with a template that I have now replaced, and when I entered the Disable Click-Right app it worked great. Then I changed the template and also created a custom Token Access app from Facebook Business Developer. So I don’t know if the new template or the Facebook app token I created is the problem. Since I was told that the error probably comes from a code with Token Access, I thought that perhaps the fault lies with the Facebook code. I hope I was clear

I’m not very well versed in neither PHP nor CURL, but your requests seems very off when comparing it to this example: PHP upload file with curl (multipart/form-data) · GitHub

You’re missing content-length, the boundary seems to be invalid, etc.

this was helpful, thanks

you are not posting under the right section/posts. we are here with other kind of issue :slight_smile:
and sorry, I can’t help you :frowning:

Nice man you fixed it!

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.