Cloudflare Images and PHP Curl Request Not Working

Hello.

I am trying to implement Cloudflare Images but I am getting an error that I can’t seem to trouble shoot.

“ERROR 5400: Bad request: invalid number at line 1 column 2”

Here is the code that I am using:

$url = ‘https://api.cloudflare.com/client/v4/accounts/:account-YES-im-using-my-account-number-here/images/v1/direct_upload’;

$curl = curl_init();

if (function_exists(‘curl_file_create’)) {
$fileAttachment = curl_file_create(’:absolute-file-path’);
} else {
$fileAttachment = ‘@’ . realpath(’:absolute-file-path’);
}

$fields = array(
‘uploaded_file’ => $fileAttachment
);
$customHeaders = array(
“Content-Type: application/json”,
“Authorization: Bearer :myImagesToken”
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $customHeaders);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);

$data = curl_exec($curl);
echo $data;

curl_close($curl);

This script runs. I get past the authorization. Then it returns “ERROR 5400: Bad request: invalid number at line 1 column 2”

What should I do?

It would really be nice if you could include a canonical example of how to use this service via PHP in the documentation.

The current docs which include only instructions from the shell are not very useful for direct creator content uploads.

1 Like

Try changing this to 'file' => $fileAttachment. You need to use the file key in the form.

1 Like

Yeah, thanks for trying to help.

I am still getting “ERROR 5400: Bad request: invalid number at line 1 column 2”

Again, if the documentation would include a canonical example of how to use these services on our servers, I would appreciate it and others might too.

Oh sorry, I just realized you’re trying to use direct_upload. There’s no need to upload a file here then, this endpoint will return a URL that a user can upload to (docs here: Cloudflare API v4 Documentation)

Just to confirm, are you wanting to use direct_upload which returns an upload URL (no auth needed), or do you wish to just upload an image yourself?


For direct upload:
You need to do a POST to /accounts/:account_identifier/images/v1/direct_upload with a JSON body:
{"expiry": "TIMESTAMP"} - example: {"expiry":"2021-01-02T02:20:00Z"}

For uploading yourself:
You need to do a POST to /accounts/:account_identifier/images/v1 with a form upload (like you’re doing right now) with the file key

Ok, I think I got it working by removing the image FILE entirely.

I still think that this documentation could be improved but maybe me stumbling around with this will help others.

Here is what I used to get a JSON response using PHP cURL:

$url = "https://api.cloudflare.com/client/v4/accounts/:accountNumber/images/v1/direct_upload";
	$curl = curl_init();

	$customHeaders = array(
		"Content-Type: application/json",
		"Authorization: Bearer :secretToken"
	);

	curl_setopt($curl, CURLOPT_HTTPHEADER, $customHeaders);
	curl_setopt($curl, CURLOPT_URL, $url);
	curl_setopt($curl, CURLOPT_POST, TRUE);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

	$data = curl_exec($curl);
	echo "<div id='curl-data'>" . $data . "</div>";

	curl_close($curl);

I think I’m going to move on with my project now.

This topic was automatically closed after 15 days. New replies are no longer allowed.