Search code examples
phpcurlibm-watsonphp-curl

PHP curl POST for Watson Video Enrichment


Am trying to use PHP 7.2 to submit a new job to the Watson Video Enrichment API.
Here's my code:

//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;

But I can't get it to work, even with varying CURLOPTs, like:

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');

I keep getting the response: Bad Request.

Here's the API docs.

Am I setting up the POST CURL all wrong? Is my $data array wrong? Any idea how to fix this?


Solution

  • Ok, I figured it out. Thanks to @TheGentleman for pointing the way.

    My data array should look like:

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