Plugin Directory

Changeset 1359484


Ignore:
Timestamp:
02/27/2016 10:29:44 AM (10 years ago)
Author:
somatic
Message:

fixed fetch_external_media() to work again with youtube and vimeo oembed calls

File:
1 edited

Legend:

Unmodified
Added
Removed
  • somatic-framework/trunk/inc/somaFunctions.php

    r875746 r1359484  
    13521352        }
    13531353
     1354
    13541355        $media = array();   // init container
    13551356        $width = $width ? $width : "853";           // iframe default
     
    13611362                if (preg_match('%(?:youtube\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match)) {
    13621363                    $id = $match[1];
    1363                     $meta = json_decode(file_get_contents("http://gdata.youtube.com/feeds/api/videos/$id?v=2&alt=json"), true);             // json_decode true returns data as array instead of object
    1364                     $meta = $meta['entry'];                                                                                                 // isolate the single entry we requested
     1364                    ob_start();
     1365                        ini_set('user_agent','Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.56 Safari/537.17');
     1366                        // $meta = file_get_contents("http://gdata.youtube.com/feeds/api/videos/".$id."?v=2&alt=json");  // old, don't work??
     1367                        $meta = file_get_contents("https://www.youtube.com/oembed?url=".$url."&format=json");
     1368                        $meta = json_decode($meta, true);   // json_decode true returns data as array instead of object
     1369                    ob_end_clean();
     1370                   
    13651371                    $media['url']       = "http://www.youtube.com/watch?v=$id";                                                                 // rebuild external url
    13661372                    $media['id']        = $id;
    13671373                    $media['site']      = $site;
    1368                     $media['thumb']     = $meta['media$group']['media$thumbnail'][1]['url'];                                                // grabs url of medium 360x480 image
    1369                     $media['title']     = stripslashes($meta['media$group']['media$title']['$t']);
    1370                     $media['desc']      = stripslashes($meta['media$group']['media$description']['$t']);
    1371                     $media['duration']  = $meta['media$group']['media$content'][0]['duration'];
     1374                    $media['thumb']     = $meta['thumbnail_url'];                                               // grabs url of medium 360x480 image
     1375                    $media['title']     = $meta['title'];
     1376                    // $media['desc']       = stripslashes($meta['media$group']['media$description']['$t']);
     1377                    // $media['duration']   = $meta['media$group']['media$content'][0]['duration'];
    13721378                    $media['mobile']    = "youtube:$id";                                                                                    // launches youtube app, i think...
    13731379                    $media['embed']     = "<iframe width=\"$width\" height=\"$height\" src=\"http://www.youtube.com/embed/$id?rel=0&hd=1\" frameborder=\"0\" allowfullscreen></iframe>";
    13741380                    $media['iframe']    = "http://www.youtube.com/embed/$id?rel=0&hd=1&autoplay=1";                                         // url to pass to iframe renderers
    1375                     $media['api']       = $meta;                                                                                            // we include everything we got from the site API, just in case (each site formats differently)
     1381                    // $media['api']        = $meta;                                                                                            // we include everything we got from the site API, just in case (each site formats differently)
    13761382                } else {
    13771383                    return new WP_Error('missing', "Can't extract ID from youtube URL...");
     
    13791385            break;
    13801386            case "vimeo";
    1381                 if ($oembed = json_decode(file_get_contents("http://vimeo.com/api/oembed.json?url=".urlencode($url)), true)) {              // get oembed metadata
     1387                // try to get around 403 forbidden errors when using file_get_contents
     1388                ob_start();
     1389                    ini_set('user_agent','Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.56 Safari/537.17');
     1390                    $oembed = file_get_contents("https://vimeo.com/api/oembed.json?url=".urlencode($url));
     1391                    $oembed = json_decode($oembed, true);
     1392                ob_end_clean();
     1393                if (!empty($oembed)) {              // get oembed metadata
    13821394                    $id = $oembed['video_id'];                                                                                              // extract id from api response
    1383                     $meta = array_shift(unserialize(file_get_contents("http://vimeo.com/api/v2/video/$id.php")));                           // get more metadata (note: we're making two distinct calls to API for vimeo)
     1395                    ob_start();
     1396                        ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 6.0)');
     1397                        $meta = file_get_contents("https://vimeo.com/api/v2/video/$id.php");
     1398                    ob_end_clean();
     1399                    $meta = array_shift(unserialize($meta));                            // get more metadata (note: we're making two distinct calls to API for vimeo)
    13841400                    $meta = array_merge($oembed, $meta);                                                                                    // combine the two (including some overlapping keys)
    13851401                    $media['url']       = "http://vimeo.com/$id";                                                                           // rebuild external url
Note: See TracChangeset for help on using the changeset viewer.