2014-09-02 64 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留空時會發生什麼。