Features

Developers

Upload a video with the api.video PHP client

October 26, 2021 - Erikka Innes in Video upload, PHP

api.video offers a PHP client to make it easier for you to upload your videos. Today we'll walk through how to do this. You can always hop on over to the GitHub repository if you'd prefer to learn about the client that way too - api.video PHP client.

Prerequisites

For this project, you'll need these things:

  • Composer - if you don't have it set up, go here to learn about it and get it installed - getcomposer.org
  • An api.video account - you can sign up here.

Installation

You'll need to install a few things using Composer:

  • The api.video PHP client - composer require api-video/php-api-client
  • Symfony HTTP client - composer require symfony/http-client
  • nyholm/psr7 - composer require nyholm/psr7

You can type composer update to make sure everything is pulled into your project and configured.

Code sample

Here is the code sample for uploading a file. You need to change 'yourAPIkey' to your key that you retrieve from the api.video dashboard after you create an account. Place the video you want to upload in the same folder as your code sample. For the sake of simplicity, this code sample hardcodes the title of the video - change VIDEO_TO_UPLOAD.mp4 to the title of your video file.

<?php
require __DIR__ .'/vendor/autoload.php';

use Symfony\Component\HttpClient\Psr18Client;
use ApiVideo\Client\Client;
use ApiVideo\Client\Model\VideoCreationPayload;

$apiKey = 'yourAPIkey';
$apiVideoEndpoint = 'https://ws.api.video';

$httpClient = new \Symfony\Component\HttpClient\Psr18Client();
$client = new ApiVideo\Client\Client(
    $apiVideoEndpoint,
    $apiKey,
    $httpClient
);

$payload = (new VideoCreationPayload())
    ->setTitle('My cool video');

$video = $client->videos()->create($payload);
echo($payload);

$response = $client->videos()->upload(
    $video->getVideoId(),
    new SplFileObject(__DIR__.'/VIDEO_TO_UPLOAD.mp4')
);
echo($response);

Import the client

In the first part of the code, you import the parts of the api.video PHP client you'll need. The client is more efficient when you import the endpoints you need for your code, instead of everything. You also need the HttpClient from Symfony for this implementation. So it will look like this:

<?php
require __DIR__ .'/vendor/autoload.php';

use Symfony\Component\HttpClient\Psr18Client;
use ApiVideo\Client\Client;
use ApiVideo\Client\Model\VideoCreationPayload;

Initialize the client

Next you initialize the client for use in your API calls.

$apiKey = 'yourAPIkey';
$apiVideoEndpoint = 'https://ws.api.video';

$httpClient = new \Symfony\Component\HttpClient\Psr18Client();
$client = new ApiVideo\Client\Client(
    $apiVideoEndpoint,
    $apiKey,
    $httpClient
);

Create your video payload

The next part of the code includes a video creation payload. This is a cool part of the PHP client. You can add all the metadata you'd like for your video here. Later, you're able to update your code as well. Consult the Create a video endpoint in our API reference documentation for available parameters. You capitalize them when adding them into your payload. So title becomes setTitle as shown in the code sample. Any parameters in camel case that you want to use would change a bit. For example, the docs have mp4Support, but if you are using the PHP client, you would write it as setMp4support.

When you send multiple parameters, the code would look like this:

$payload = (new VideoCreationPayload()) ->setTitle('My cool video'); $payload->setTags(['tag 1', 'tag 2']);

When you're done setting up your payload, then you use the client to send this information to api.video servers and create your video container.

$payload = (new VideoCreationPayload())
    ->setTitle('My cool video');

$video = $client->videos()->create($payload);
echo($payload);

Upload the video

In the last part of the video, you retrieve the video ID for the container you made and upload your video into the container. You can only upload into a container once, but you can change its metadata later if you wish. You can see whether it worked by reading the echoed response in the terminal.

$response = $client->videos()->upload(
    $video->getVideoId(),
    new SplFileObject(__DIR__.'/VIDEO_TO_UPLOAD.mp4')
);
echo($response);

Conclusion

Now you know the basics of uploading a video with the api.video PHP client! You can check whether your upload worked by going into your dashboard and looking in the videos section. You should see it there. If you get a note that it's still processing, give it a bit of time to get the video ready, especially for big videos.

If you have any questions or suggestion, please share them on our community forum. If you haven't created your api.video account yet, you can do that in just a few moments by follwoing this link. Happy building!

Erikka Innes

Developer Evangelist

Create your free account

Start building with video now