Glenn Harwood
Glenn Harwood

Reputation: 23

PHP Curl - Wistia API Upload

I'm trying to upload a movie to the Wistia API by CURL (http://wistia.com/doc/upload-api).

It works fine using the following command line, but when I put it in PHP code, I just get a blank screen with no response:

$ curl -i -d "api_password=<YOUR_API_PASSWORD>&url=<REMOTE_FILE_PATH>" https://upload.wistia.com/

PHP Code:

<?php
$data = array(
    'api_password' => '<password>',
    'url' => 'http://www.mysayara.com/IMG_2183.MOV'
);



$chss = curl_init('https://upload.wistia.com');
curl_setopt_array($chss, array(
    CURLOPT_POST => TRUE,
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_POSTFIELDS => json_encode($data)
));

// Send the request
$KReresponse = curl_exec($chss);

// Decode the response
$KReresponseData = json_decode($KReresponse, TRUE);

echo("Response:");
print_r($KReresponseData);
?>

Thanks.

Upvotes: 2

Views: 1428

Answers (2)

James Bellamy
James Bellamy

Reputation: 21

For PHP v5.5.0 or later, here's a class for uploading to Wistia, from a LOCALLY STORED file.

Usage:

$result = WistiaUploadApi::uploadVideo("/var/www/mysite.com/tmp_videos/video.mp4","video.mp4","abcdefg123","Test Video", "This is a video upload demonstration");

The class:

@param $file_path Full local path to the file
@param $file_name The name of the file (not sure what Wistia does with this)
@param $project The 10 character project identifier the video will upload to
@param $name The name the video will have on Wistia
@param $description The description the video will have on Wistia

class WistiaUploadApi
{
    const API_KEY            = "<API_KEY_HERE>";
    const WISTIA_UPLOAD_URL  = "https://upload.wistia.com";

    public static function uploadVideo($file_path, $file_name, $project, $name, $description='')
    {
        $url = self::WISTIA_UPLOAD_URL;

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);

        $params = array
        (
            'project_id'   => $project,
            'name'         => $name,
            'description'  => $description,
            'api_password' => self:: API_KEY,
            'file'         => new CurlFile($file_path, 'video/mp4', $file_name)
        );

        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        //JSON result
        $result = curl_exec($ch);

        //Object result
        return json_decode($result);
    }
}

If you don't have a project to upload into, leaving $project blank will NOT apparently force Wistia to create one. It will just fail. So you might have to remove this from the $params array if you don't have a project to upload to. I've not experimented to see what happens when you leave $name blank.

Upvotes: 2

scrowler
scrowler

Reputation: 24406

Your problem (and the difference between the command line and PHP implementation) is probably that you're JSON encoding the data in PHP, you should use http_build_query() instead:

CURLOPT_POSTFIELDS => http_build_query($data)

For clarity, the Wistia API says it returns JSON, but doesn't expect it in the request.

Upvotes: 1

Related Questions