George George - 3 months ago 31
PHP Question

dailymotion API PHP question

The following code works for Vimeo API:

function getTitle($id){
$title = unserialize(file_get_contents("http://vimeo.com/api/v2/video/$id.php"));
$theTitle=$title[0]['title'];
echo $theTitle;
}


If for Dailymotion I use:

$id2 = 'xks75n';
function dailyMotionTitle($id2){
$dm = unserialize(file_get_contents("http://www.dailymotion.com/embed/video/".$id2));
echo $dm[0]['title'];
}


I get Error at offset 0 of 1374 bytes. I know I can use embed.ly or JSON parsing but I prefer PHP. Any help concerning repair of Dailymotion PHP parsing is appreciated.

Answer

You can't just change URLs and expect this to work.

Read through the Dailymotion APIs documentation pages to learn how to access video information programatically.

As one example, using the REST API, to get the title of your video in a JSON formatted response, fetch:

https://api.dailymotion.com/video/xks75n?fields=title

Which returns

{
    "title": "The Farmer and His Sons- Aesop's fables"
}

Another option is to use the oEmbed API on the URL you have in the question.

http://www.dailymotion.com/services/oembed?format=json&url=http://www.dailymotion.com/embed/video/xks75n

Which returns

{
    "type": "video",
    "version": "1.0",
    "provider_name": "Dailymotion",
    "provider_url": "http:\/\/www.dailymotion.com",
    "title": "The Farmer and His Sons- Aesop's fables",
    "author_name": "hooplakidz",
    "author_url": "http:\/\/www.dailymotion.com\/hooplakidz",
    "width": 480,
    "height": 269,
    "html": "<iframe src=\"http:\/\/www.dailymotion.com\/embed\/video\/xks75n\" width=\"480\" height=\"269\" frameborder=\"0\"></iframe>",
    "thumbnail_url": "http:\/\/static2.dmcdn.net\/static\/video\/369\/709\/34907963:jpeg_preview_large.jpg?20110830044159",
    "thumbnail_width": 426.666669846,
    "thumbnail_height": 240
}

Hint: you can use json_decode() to "decode" that into a PHP object or array to access the value(s).