我正在尝试使用支持 cURL 的 PHP 5.3 中的 cURL 对 API 进行发布请求。以下应该将图像发送到端点的代码不会从服务器产生错误,但似乎也根本不返回任何响应。文档说帖子应该是多部分表单数据,但也说这是自动处理的。另一种可能性是我需要明确告诉它返回 JSON,但我不知道在哪里执行此操作。当我将“Content-Type: application/json”添加到标头时,它会给出 500 错误。谁能指出以下内容有什么问题吗?
(当我从浏览器运行脚本时,没有错误,但也没有显示任何内容。)
$mykey = "*****key*******";
$apiUrl = "https://api.endpoint/v2beta/image-to-video";// this is a valid endpoint
$apiKey = "sk-".$mykey;
$imagePath = "redcar-768.png";
$seed = 0;
$cfgScale = 1.8;
$motionBucketId = 127;
$ch = curl_init($apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
// Above line required for HTTP error codes to be reported
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'image' => new CURLFile($imagePath),
'seed' => $seed,
'cfg_scale' => $cfgScale,
'motion_bucket_id' => $motionBucketId
));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer ' . $apiKey
));
curl_exec($ch);
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
echo $error_msg;
}
else {
echo "success";
}
//for good measure
if (isset($error_msg)) {
echo "there was an error";
}
我有以下建议: