2

我正在尝试通过 CURL ( http://wistia.com/doc/upload-api )将电影上传到 Wistia API 。

使用以下命令行可以正常工作,但是当我将其放入 PHP 代码中时,我只是得到一个没有响应的空白屏幕:

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

PHP代码:

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

谢谢。

4

2 回答 2

2

对于 PHP v5.5.0 或更高版本,这是一个用于从本地存储文件上传到 Wistia 的类。

用法:

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

班上:

@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);
    }
}

如果您没有要上传的项目,将 $project 留空显然不会强制 Wistia 创建一个。它只会失败。因此,如果您没有要上传的项目,您可能必须从 $params 数组中删除它。我还没有尝试过将 $name 留空时会发生什么。

于 2015-07-09T13:54:51.883 回答
1

您的问题(以及命令行和 PHP 实现之间的区别)可能是您在 PHP 中对数据进行 JSON 编码,您应该http_build_query()改用:

CURLOPT_POSTFIELDS => http_build_query($data)

为清楚起见,Wistia API 表示它返回 JSON,但并不期望它出现在请求中。

于 2014-09-02T03:30:30.133 回答