PHP API throwing some sort of validation error for sandbox and production environment

Here is the code I’m using to trim the video:

<?php
require 'vendor/autoload-2.php';
include("config.php");

use Shotstack\Client\Api\EditApi;
use Shotstack\Client\Configuration;
use Shotstack\Client\Model\Edit;
use Shotstack\Client\Model\Output;
use Shotstack\Client\Model\Timeline;
use Shotstack\Client\Model\Track;
use Shotstack\Client\Model\Clip;
use Shotstack\Client\Model\VideoAsset;

$stime = $_POST['stime'];
$etime = $_POST['etime'];
$userID = $_POST['userID'];
$videoURL = $_POST['videoURL'];
$videoKey = $_POST['videoKey'];

$source = str_replace(' ', '%20', $videoURL);



$vidLength = $etime - $stime;


class TrimDemo
{
    protected $apiKey;
    protected $apiUrl = 'https://api.shotstack.io/stage';

    public function __construct()
    {

        $this->apiKey = 'MY API KEY';
    }

    public function render()
    {
        $config = Configuration::getDefaultConfiguration()
            ->setHost($this->apiUrl)
            ->setApiKey('x-api-key', $this->apiKey);

            echo $this->apiUrl.'<br>';
            echo $this->apiKey.'<br><br>';

        $client = new EditApi(null, $config);

        $videoAsset = new VideoAsset();
        $videoAsset
            ->setSrc($GLOBALS['source'])
            ->setTrim($GLOBALS['stime']);

        $videoClip = new Clip();
        $videoClip
            ->setAsset($videoAsset)
            ->setLength($GLOBALS['vidLength'])
            ->setStart(0);

        $track = new Track();
        $track->setClips([$videoClip]);

        $timeline = new Timeline();
        $timeline->setTracks([$track]);

        $output = new Output();
        $output
            ->setFormat('mp4')
            ->setResolution('hd');

        $edit = new Edit();
        $edit
            ->setTimeline($timeline)
            ->setOutput($output);

        try {
            $response = $client->postRender($edit)->getResponse();
        } catch (Exception $e) {
            die('Request failed: ' . $e->getMessage());
        }

        $video = $client->getRender($response->getId())->getResponse();
        if ($video->getStatus() === 'done') {
            echo $video->getUrl().'<br>';
        }


        
        global $videoID;
        $videoID = $response->getId();
    }
}

$editor = new TrimDemo();
$editor->render();

$finalID = $videoID;

?>

I’m getting the following error thrown at me:
Request failed: [400] Client error: POST https://api.shotstack.io/stage/render resulted in a 400 Bad Request response: {“success”:true,“message”:“Bad Request”,“response”:{“error”:{“name”:“ValidationError”,“details”:[{“message”:"“trim” mu (truncated…)

Can anyone please help?

Hi @soohaibster,

There appears to be an issue with one of the variables you’re passing to the API, specifically in relation to the trim property. Are you using the right type? The trim method only accepts a float.

If you can share what you are sending through to your PHP script and their types this may help solve the problem.

1 Like

Thanks a lot @dazzatron . That was the real problem there. I was sending the value as a string. I have changed it now and everything is working properly.

Thanks again!

1 Like