2016-12-06 141 views
0

我試圖使用Laravel和GuzzleHttp將視頻上傳到DailyMotion。以下是我的代碼:使用GuzzleHttp將視頻上傳到DailyMotion

$file = "3.mp4"; 
$fields["file"] = fopen($file, 'rb'); 
$res = $client->post($upload_url, [ 
    'headers' => ['Content-Type' => 'multipart/form-data'], 
    $fields 
]); 

$data3 = $res->getBody(); 
$response_upload_video = json_decode($data3,true); 
echo "<br>Getting dm upload video response: "; 
print_r($response_upload_video); 

$upload_url是由DailyMotion傳遞的動態生成的URL。在執行上面的代碼,我會永遠得到這個錯誤:

Production.ERROR: GuzzleHttp\Exception\ClientException:
Client error: POST http://upload-02.sg1.dailymotion.com/upload?uuid=werewkrewrewrwer&seal=pppppppppppppppp `resulted in a 400 Bad Request response:
{"error":"invalid content range","seal":"yyyyyyyyyyyyyyyyyy"} in /home/vagrant/Code/svc-titus/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:111

但我可以上傳視頻到郵差使用相同的上傳網址,如下面顯示: enter image description here

回答

2

我不認爲你需要指定內容類型的頭文件guzzle會在您提供資源時自動決定它的資源,如果視頻存儲在公共目錄中,則您的視頻的路徑似乎有問題,您需要使用public_path或相應的路徑幫助程序函數來獲取其物理路徑 下面應該在guzzle中工作https 6檢查發送表單文件在這裏 http://docs.guzzlephp.org/en/latest/quickstart.html#uploading-data

$file = "3.mp4"; 
$res = $client->post($upload_url, [ 
    'multipart' => [ 
     [ 
      'name'  => 'file', 
      'contents' => fopen(base_path($file), 'r') // give absolute path using path helper function 
     ] 
    ] 
]); 

$data3 = $res->getBody(); 
$response_upload_video = json_decode($data3,true); 
echo "<br>Getting dm upload video response: "; 
print_r($response_upload_video); 
+0

我試圖運行你的代碼「,當這個錯誤production.ERROR:Symfony的\分量\調試\異常\ FatalThrowableError:類型錯誤:傳遞給GuzzleHttp \ PSR7 \ MultipartStream :: addElement方法(參數2)必須是給出的字符串,在第70行調用/home/vagrant/Code/svc-titus/vendor/guzzlehttp/psr7/src/MultipartStream.php「。基本上引發錯誤的行是'contents'= > fopen(base_path($ file),'r') – imin

+0

對不起,我的工作很糟糕。非常感謝! – imin