PHP curl POST 用于 Watson Video Enrichment

PHP curl POST for Watson Video Enrichment

我正在尝试使用 PHP 7.2 向 Watson Video Enrichment 提交新作业 API。
这是我的代码:

//set some vars for all tasks
$apiUrl = 'https://api-dal.watsonmedia.ibm.com/video-enrichment/v2';
$apiKey =  'xxxxxxxx';

//vars for this task
$path = '/jobs';
$name = 'Test1';
$notification_url = 'https://example.com/notification.php';
$url = 'https://example.com/video.mp4';
$data = array(
    "name" => $name, 
    "notification_url" => $notification_url, 
    "preset" => "simple.custom-model",
    "upload" => array(
        "url" => $url
    )
);
$data_string = json_encode($data);

$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_URL, $apiUrl.$path );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_string),
    'Authorization: APIKey '.$apiKey
));
$result = curl_exec($ch);
echo $result;

但我无法让它工作,即使使用不同的 CURLOPT,如:

curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');

我不断收到回复: Bad Request.

这是 API docs.

我设置的 POST CURL 是不是全错了?我的 $data 数组错了吗?知道如何解决这个问题吗?

阅读他们的 API 文档,字段 preset 似乎不是字符串类型。相反,它有一个定义的架构 here。这看起来类似于您使用 upload 架构的方式。

您应该将数据数组更改为如下所示:

$data = array(
    "name" => $name, 
    "notification_url" => $notification_url, 
    "preset" => array(
        "video_url" => "https://example.com/path/to/your/video"
    ),
    "upload" => array(
        "url" => $url
    )
);

好的,我明白了。感谢@TheGentleman 指路。

我的数据数组应该如下所示:

$data = array(
    "name" => $name, 
    "notification_url" => $notification_url, 
    "preset" => array(
        "simple.custom-model" => array(
            "video_url" => $url,
            "language" => "en-US"
        )
    ),
    "upload" => array(
        "url" => $url
    )
);