Scripts

oEmbed client functies

Hulpfunctie om oembed data voor een webpagina te achterhalen. oEmbed wordt door een aantal websites zoals youtube en vimeo ondersteunt als universele manier voor embedden. De webpagina zelf bevat dan een link naar een API waar je de data zoals url en afmetingen uit kan halen. Als de site zelf nog niet oEmbed data aanbiedt probeert dit script embed.ly om alsnog de data te krijgen. Omdat het ophalen van deze info best traag is raad ik aan om het resultaat samen met de oorspronkelijke url op te slaan zodat je het alleen opnieuw hoeft op te halen als de data die je al had opgehaald niet meer klopt. Zie het voorbeeld voor gebruik en voor voorbeeld-data. Mijn ervaring is dat het het makkelijkst is om html te genereren op basis het 'type' attribuut dat je terug krijgt. Of als je de bron vertrouwt en het 'html' attribuut terug krijgt, is dat de meest betrouwbare oplossing.

oembed.php
<?php

function oembed_get_data($video_url, array $params = array(), &$error = null)
{
	$html = @file_get_contents($video_url);
	
	if (!$html)
	{
		$error = 'Could not download video page';
		return false;
	}
	
	$links = oebmed_parse_links($html);
	
	// discover oembed api location using by searching for <link/> elements
	foreach ($links as $link)
	{
		if (!isset($link['type']) || empty($link['href']))
			continue;
		
		$oembed_url = $link['href'];
		
		if (!empty($params))
			$oembed_url .= (strpos($oembed_url, '?') === false ? '?' : '&')
				. http_build_query($params);
		
		if ($link['type'] == 'application/xml+oembed' || $link['type'] == 'text/xml+oembed')
			return oembed_get_xml_data($link['href'], $error);
		
		if ($link['type'] == 'application/json+oembed')
			return oembed_get_json_data($link['href'], $error);
	}
	
	// fallback using embed.ly
	$params = array_merge($params, array('url' => $video_url, 'format' => 'json'));
	$embedly_uri = 'http://api.embed.ly/1/oembed?' . http_build_query($params);
	
	return oembed_get_json_data($embedly_uri, $error);
}

function oebmed_parse_links($html)
{
	$links = array();
	
	// pattern gesplitst voor PHPhulp highlighting
	if (preg_match_all('{<link ([^<>]+)/?'.'>}i', $html, $matches))
		foreach ($matches[1] as $attributes_str)
		{
			preg_match_all('{([a-z]+)=(["\'])(.+?)\2}i', $attributes_str, $amatches, PREG_SET_ORDER);
		
			$attributes = array();
			foreach ($amatches as $amatch)
				$attributes[$amatch[1]] = html_entity_decode($amatch[3]);
		
			$links[] = $attributes;
		}
	
	return $links;
}

function oembed_get_xml_data($oembed_url, &$error = null)
{
	$xml = @simplexml_load_file($oembed_url);
	
	if (!$xml)
	{
		$error = 'Could not parse or load oembed xml data';
		return false;
	}
	
	$data = new stdClass;
	
	foreach ($xml as $property => $value)
		$data->$property = (string) $value;
	
	return $data;
}

function oembed_get_json_data($oembed_url, &$error = null)
{
	$json = @file_get_contents($oembed_url);
	
	if (!$json)
	{
		$error = 'Could not download oembed json data';
		return false;
	}
	
	$data = @json_decode($json);
	
	if (!$data)
	{
		$error = 'Could not parse oembed json data';
		return false;
	}
	
	return (object) $data;
}
voorbeeld.php
<?php

include_once 'oembed.php';

// YouTube.
var_dump(oembed_get_data('http://www.youtube.com/watch?v=tPxgi-PiNFE&hd=1'));
/*
object(stdClass)#1 (13) {
  ["provider_url"]=>
  string(23) "http://www.youtube.com/"
  ["title"]=>
  string(51) "Gaspar NoƩ's Enter The Void - Opening Credits (HD)"
  ["html"]=>
  string(391) "<object width="480" height="295"><param name="movie" value="http://www.youtube.com/e/tPxgi-PiNFE"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/e/tPxgi-PiNFE" type="application/x-shockwave-flash" width="480" height="295" allowscriptaccess="always" allowfullscreen="true"></embed></object>"
  ["author_name"]=>
  string(20) "cinematicallyawkward"
  ["height"]=>
  int(295)
  ["thumbnail_width"]=>
  int(480)
  ["width"]=>
  int(480)
  ["version"]=>
  string(3) "1.0"
  ["author_url"]=>
  string(48) "http://www.youtube.com/user/cinematicallyawkward"
  ["provider_name"]=>
  string(7) "YouTube"
  ["thumbnail_url"]=>
  string(48) "http://i1.ytimg.com/vi/tPxgi-PiNFE/hqdefault.jpg"
  ["type"]=>
  string(5) "video"
  ["thumbnail_height"]=>
  int(360)
}
*/


// Flickr, volgens mij via embed.ly
var_dump(oembed_get_data('http://www.flickr.com/photos/skorj/3404545680/in/set-472500/'));
/*
object(stdClass)#1 (15) {
  ["provider_url"]=>
  string(22) "http://www.flickr.com/"
  ["description"]=>
  string(47) "magnesiumagency.com/2010/01/14/abandoned_japan/"
  ["title"]=>
  string(48) "The drunken karaoke could be heard in the winds."
  ["url"]=>
  string(61) "http://farm4.static.flickr.com/3469/3404545680_69d8788459.jpg"
  ["author_name"]=>
  string(5) "Skorj"
  ["height"]=>
  int(418)
  ["width"]=>
  int(500)
  ["thumbnail_url"]=>
  string(63) "http://farm4.static.flickr.com/3469/3404545680_69d8788459_s.jpg"
  ["thumbnail_width"]=>
  int(75)
  ["version"]=>
  string(3) "1.0"
  ["provider_name"]=>
  string(6) "Flickr"
  ["cache_age"]=>
  int(3600)
  ["type"]=>
  string(5) "photo"
  ["thumbnail_height"]=>
  int(75)
  ["author_url"]=>
  string(35) "http://www.flickr.com/photos/skorj/"
}
*/

Reacties

0
Nog geen reacties.