我如何使用DailyMotion API?

时间:2011-10-06 16:08:35

标签: php api

这里的任何人都可以帮我集成DailyMotion API吗?我正在使用他们的SDK,但无济于事,论坛也没有多大帮助。

1 个答案:

答案 0 :(得分:3)

我尝试使用DailyMotion SDK。它工作得很好但后来由于某种原因我被告知不要使用SDK。所以这是使用cURL使用API​​的PHP代码。

define("DAILYMOTION_API_KEY", "xyzzz");
define("DAILYMOTION_API_SECRET_KEY", "abcc");
$testUser = "username";
$testPassword = "pwd";
$url = 'https://api.dailymotion.com/oauth/token';
$testVideoFile = "<file location>";
$vidName = "testing video";
$vidDesc = "this is a test";
/* GET ACCESS TOKEN */
try {
$data = "grant_type=password&client_id=" . DAILYMOTION_API_KEY . "&client_secret=" .      DAILYMOTION_API_SECRET_KEY . "&username=abs&password=pwd&scope=read+write";
$curlInit = curl_init($url);
curl_setopt($curlInit, CURLOPT_POST, 1);
curl_setopt($curlInit, CURLOPT_POSTFIELDS, $data);
curl_setopt($curlInit, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curlInit);
curl_close($curlInit);
$res = json_decode($output);
$accessToken = $res->access_token;
$getUploadUrl = "curl -d 'access_token=$accessToken' -G  https://api.dailymotion.com/file/upload/";
$uploadUrl = json_decode(system($getUploadUrl));
$postFileCmd = "curl -F 'file=@$testVideoFile'" . ' "' . $uploadUrl->upload_url . '"';
$postFileResponse = json_decode(system($postFileCmd));

$postVideoCmd = "curl -d 'access_token=$accessToken&url=$postFileResponse->url' https://api.dailymotion.com/me/videos";

$postVideoResponse = json_decode(system($postVideoCmd));
$videoId = $postVideoResponse->id;

$publishCmd = "curl -F 'access_token=$accessToken' \
 -F 'title=$vidName' \
 -F 'published=true' \
 -F 'description=this is a test' \
 https://api.dailymotion.com/video/$videoId";

 $publishres = system($publishCmd);
 print_r($publishres);
 echo "Video is posted & published Successfully";
 } catch (Exception $e) {
 print_r($e);
 }