我试图通过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);
?>
感谢。
答案 0 :(得分:2)
对于PHP v5.5或更高版本,这是一个从LOCALLY STORED文件上传到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留空时会发生什么。
答案 1 :(得分:1)
您的问题(以及命令行和PHP实现之间的区别)可能是您在PHP中对JSON进行数据编码,您应该使用http_build_query()
代替:
CURLOPT_POSTFIELDS => http_build_query($data)
为清楚起见,Wistia API说它returns JSON,但在请求中并不期望它。