CloudFlare Stream PHP Curl not working to get token?

(Reposting because my code tags were bad.)

Hey folks,
Having some trouble trying to secure a stream video. Or rather get a token back.
Following this doc: https://developers.cloudflare.com/stream/viewing-videos/securing-your-stream#customizing-default-restrictions

The curl command example, on the page, works from “curl” on the command line:

curl \
-X POST \
-H "Authorization: Bearer $TOKEN" \
https://api.cloudflare.com/client/v4/accounts/$ACCOUNT/stream/$VIDEOID/token

But not from PHP:

<?php

$url = "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT/stream/$VIDEOID/token";

$ch = curl_init();

$headers = array(
  "Authorization: Bearer $TOKEN"
);

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, false);

$result = curl_exec($ch);

print $result . '\n';
?>

The above gives a “400 Bad Request” error.

The 400 error is not specific to Cloudflare. It is because no POST vars are being passed with the POST request.

I added some POST vars with this (one of the suggested POST vars further down in the page):

curl_setopt($ch, CURLOPT_POSTFIELDS, "uid=$VIDEOID");

Now it gives me a weird error:

{
  "result": null,
  "success": false,
  "errors": [
    {
      "code": 10005,
      "message": "Bad Request"
    }
  ],
  "messages": [
    {
      "code": 10005,
      "message": "Failed to parse body"
    }
  ]
}

I could not find anything else from duckduckgo. Anyone have any suggestions, or even better, working php curl code examples?

Try using below:

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://api.cloudflare.com/client/v4/accounts/YOUR_ACCOUNT_HAS_ID/stream/VIDEO_HASH_ID/token',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_HTTPHEADER => array(
    'Authorization: Bearer $TOKEN_HASH_HERE',
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

Else, might have to send it as a JSON :thinking:

That works. High five! Thank you.

I did have to change the single quotes to double quotes to allow my variables to be variables. But that is otherwise a working example.

My exact code was:

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT/stream/$VIDEOID/token",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_HTTPHEADER => array(
    "Authorization: Bearer $TOKEN",
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;
1 Like

:+1: I am happy to assist you :wink:

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