简体   繁体   中英

Cannot upload image by PHP CURL to Cloudflare

When I run the curl command below, I receive success response and the image can upload as normal:

curl -X POST -F file=@/myFolder/pic.png -H "Authorization: Bearer {tokenX}" https://api.cloudflare.com/client/v4/accounts/{accountX}/images/v1

But when I use PHP to exec curl by the code below:

// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://api.cloudflare.com/client/v4/accounts/{accountX}/images/v1');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'file' => '@' .realpath('/myFolder/pic.png'));

$headers = array();
$headers[] = 'Authorization: Bearer {tokenX}';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

Cloudflare return an error: ERROR 9422: Decode error: image failed to be decoded: Uploaded image must have image/jpeg or image/png content type

I run curl command and PHP code on the same local server.

What did I do wrong?

echo json_decode(shell_exec('curl -X POST -F file=@./Rectangle36.png -H "Authorization: Bearer {{token}}" https://api.cloudflare.com/client/v4/accounts/{{account}}/images/v1' ))->result->variants[0];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM