繁体   English   中英

在 PHP cURL 中设置 CURLOPT_POSTFIELDS 时出现错误请求

[英]Bad Request when setting CURLOPT_POSTFIELDS in PHP cURL

PHP版本:8.1

通过 PHP cURL 使用 Cloudflare Stream 时,我收到 400 Bad Request 错误,但前提是设置了 CURLOPT_POSTFIELDS。

我的代码:

$tokench = curl_init();
curl_setopt($tokench, CURLOPT_URL, "https://api.cloudflare.com/client/v4/accounts/".getenv("CLOUDFLARE_ACCOUNT")."/stream/".urlencode($_GET["video"])."/token");
 curl_setopt($tokench, CURLOPT_POST, 1);
 curl_setopt($tokench, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($tokench, CURLOPT_HTTPHEADER, ["Authorization: Bearer ".getenv("CLOUDFLARE_TOKEN"), "Content-Type: application/json"]);
 curl_setopt($tokench, CURLOPT_POSTFIELDS, "downloadable=true");
 $tokenraw = curl_exec($tokench);
 curl_close($tokench);
 $tokenarr = json_decode($tokenraw, true);

$tokenarr["success"] 是假的。

我自己修好了。 我必须对 POSTFIELDS 进行 JSON 编码,因为内容类型是 application/json。 代码示例:

$tokench = curl_init();
curl_setopt($tokench, CURLOPT_URL, "https://api.cloudflare.com/client/v4/accounts/".getenv("CLOUDFLARE_ACCOUNT")."/stream/".urlencode($_GET["video"])."/token");
curl_setopt($tokench, CURLOPT_POST, 1);
curl_setopt($tokench, CURLOPT_RETURNTRANSFER, true);
curl_setopt($tokench, CURLOPT_HTTPHEADER, ["Authorization: Bearer ".getenv("CLOUDFLARE_TOKEN"), "Content-Type: application/json"]);
curl_setopt($tokench, CURLOPT_POSTFIELDS, json_encode(["downloadable" => true]));
$tokenraw = curl_exec($tokench);
curl_close($tokench);
$tokenarr = json_decode($tokenraw, true);

$tokenarr["success"] 是真的!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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