2014-09-02 76 views
2

我想通过CURL上传一部电影到Wistia API(http://wistia.com/doc/upload-api)。PHP Curl - 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

1.你打开'display_errors'? 2. php.ini中的上传文件大小和文章大小限制是什么? 3.你甚至不检查'curl_exec'结果。 – Raptor 2014-09-02 03:30:29

回答

1

您的问题(在命令行和PHP执行之间的差额)可能是,你是JSON编码在PHP中的数据,你应该使用http_build_query()代替:

CURLOPT_POSTFIELDS => http_build_query($data) 

为了清楚起见,Wistia API说它returns JSON,但不要求在请求中。

2

对于PHP v5.5.0或更高版本,这是一个从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); 
    } 
} 

如果你没有一个项目上载到,留下$项目留空将不会明显迫使Wistia创建一个。它会失败。因此,如果您没有要上传的项目,则可能必须从$ params数组中删除此项。我没有试验看看当你将$ name留空时会发生什么。