Decoding Error while uploading a local Video file to CloudFlare Streams with PHP

Type

Video

What is the error number?

10004

What is the error message?

Decoding Error: A por (truncated…)

What is the issue you’re encountering

When I try to upload a video, I always get this error. I’m using PHP with Guzzle

What steps have you taken to resolve the issue?

Here is the source code:

$client = new Client();

$file = fopen($path, ‘rb’);

$response = $client->post(
https://api.cloudflare.com/client/v4/accounts/$cloudFlareZone/stream/”,
[
‘headers’ => [
‘Authorization’ => 'Bearer ’ . $cloudFlareApiKey,
‘Content-Type’ => ‘application/json’,
],
‘body’ => $file
]
);

The $path has correct path, and the dotenv variables are also correct

Hi, I believe at least one of the issues is the content-type and body. Could you try this function? Note that you need account_id and not zone_id; and token instead of key.

function uploadVideoToCloudflare($cf_account_id, $cf_token, $video_file_path) {
    $client = new Client();

    try {
        $response = $client->request('POST', "https://api.cloudflare.com/client/v4/accounts/$cf_account_id/stream/", [
            'headers' => [
                'Authorization' => "Bearer $cf_token"
            ],
            'multipart' => [
                [
                    'name'     => 'file',
                    'contents' => fopen($video_file_path, 'r'),
                    'filename' => basename($video_file_path)
                ]
            ]
        ]);

        return $response->getBody()->getContents();
    } catch (GuzzleException $e) {
        return "Error: " . $e->getMessage();
    }
}

// Usage example:
$cf_account_id = 'YOUR_ACCOUNT_ID';
$cf_token = 'YOUR_TOKEN';
$video_file_path = '/path/to/your/clip.mp4';

$result = uploadVideoToCloudflare($cf_account_id, $cf_token, $video_file_path);
echo $result;

1 Like

Thank you for the fast reply, i could made it working with curl, so i don’t need Guzzle for this, but thank you again!

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