Wistia a professional video hosting with great analytics and video marketing tools. It has tons of features along with prompt customer support. What it lacks though is a great developer community. And surprisingly, Wistia does not officially support PHP developers for API integrations.
It does not even have a PHP team for it? I mean, who does that! And they don’t plan to have one in near future if my emails with their support rep is to be trusted.
But, with that being said, they do have a great product and i will today be focusing on uploading videos to Wistia using PHP and Wistia Upload API.
So, like with many other APIs, we willuse PHP cURL. But first, we will needtokeep these points touse the Upload API.
- Use POST method
- Use SSL-based API endpoint (https://upload.wistia.com/)
- Upload will only be successful if the data is multipart-form encoded
Required post parameters
Two parameters are required:
- api_password or access_token
- file (If uploading from local source or a newfile) or url (From an external source)
Apart from these, you can optionally pass in project_id, name, description and contact_id.
Now, to upload a file you will need to create a PHP based cURL request as below.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
$params['file'] = array();
/*Encode it using CurlFile*/
$params['file'] = new CurlFile('somefile.mp4', 'video/mp4', 'somefile.mp4);
$params['api_password'] = 'Your Wistia API password';
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$resultJson = json_decode($result);
The $resultJson here is the object representation of json result returned from Wistia after successful upload. Notice i used the CurlFile class instead of trying to encode the upload content explicitly. But beware that CurlFile is only native to PHP version > 5.5.
Here is a sample json response return from Wistia:
Here, hashed_id is the field you might wanna save in DB since it is also required to update and delete media from Wistia.
If there is an error, you will see something like below in json response:
Wistia resturns HTTP 200 header upon successful upload and 400 for errors. Also, it returns 401 when there is an authentication error.
Latest posts by alexey (see all)
- Zend Soap NULL Reponse - August 29, 2018
- Quick Facts About WooCommerce - April 25, 2018
- WordPress stuck on a “Too many redirects” error loop when using SSL - January 17, 2018