Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2013-02-20 16:10:27

robhert
Member
From: Perú
Registered: 2007-04-27
Posts: 208
Website

Vimeo thumbnails and Open Graph

Hello!

I found this script (works)

<script type="text/javascript">
    $.ajax({
        type:'GET',
        url: 'http://vimeo.com/api/v2/video/' + <txp:custom_field name='videovimeo' /> + '.json',
        jsonp: 'callback',
        dataType: 'jsonp',
        success: function(data){
            var thumbnail_src = data[0].thumbnail_large;
            $('#thumb_wrapper').append('<img src="' + thumbnail_src + '"/>');
        }
    });
</script>
<div id="thumb_wrapper"></div>

As you can see, the script appends the <img src.... to <div id="thumb_wrapper">here</div>

But what I need is to append the image thumbnail url TO: <meta property="og:image" content="HERE" />

Any ideas?

(I’ve tried to find a lot of solution, including PHP, but with no success).

Last edited by robhert (2013-02-20 16:29:00)

Offline

#2 2013-02-20 16:40:25

MattD
Plugin Author
From: Monterey, California
Registered: 2008-03-21
Posts: 1,254
Website

Re: Vimeo thumbnails and Open Graph

Does whatever you hope to use the meta tag have javascript enabled? Might be pointless trying to do it this way.


My Plugins

Piwik Dashboard, Google Analytics Dashboard, Minibar, Article Image Colorpicker, Admin Datepicker, Admin Google Map, Admin Colorpicker

Offline

#3 2013-02-20 16:49:27

robhert
Member
From: Perú
Registered: 2007-04-27
Posts: 208
Website

Re: Vimeo thumbnails and Open Graph

Hi Matt! Thank you for answer.

What I propose is to use Open Graph (To send the articles to Facebook with its respective image by “Like”). Then, as the article is a video from Vimeo, what I do is use the URL IMAGE <meta property="og:image" content="IMAGE URL HERE" /> and it has the direction of the Vimeo video image.

You see, for the user to click “Like”, you must have javascript enabled, so we understand that use of JavaScript is ​​valid.

Now, I don’t use PHP (though I know that it is possible) because I could not implement it successfully. But if someone gives me a help with PHP, great!

Thanks in advance!

Offline

#4 2013-02-20 17:27:46

springworks
Member
Registered: 2005-01-06
Posts: 172
Website

Re: Vimeo thumbnails and Open Graph

It would seem from this question and answer on Stack Overflow that trying to add Facebook Open Graph properties post page load will not work as intended. The meta data needs to be in the original source of the page so you need some way of doing that before the page is sent to the browser.

The only way I can think of doing that is to add some way of getting the URL of the thumbnail from Vimeo using PHP and inserting it into the document <head> in Textpattern. Unless there is a way of hacking the URL of the thumbnail based on the video ID (which I guess is what you are storing in the videovimeo custom field).

Offline

#5 2013-02-20 17:38:18

robhert
Member
From: Perú
Registered: 2007-04-27
Posts: 208
Website

Re: Vimeo thumbnails and Open Graph

Now I understand it a little more. I found a lot of PHP ways, but none of them worked for me, maybe I don’t know how to implement it.

Here are some:

<txp:php>
    $video_url = "http://vimeo.com/<txp:custom_field name='videovimeo' />"  
    $file = fopen($video_url, "r");
    $filedata = stream_get_contents($file);
    $html_content = strpos($filedata,"<link rel=\"videothumbnail");
    $link_string = substr($filedata, $html_content, 128);
    $video_id_array = explode("\"", $link_string);
    $thumbnail_url = $video_id_array[3];
    echo $thumbnail_url;
</txp:php>

But how should I show it?

HERE are another one: http://ilikekillnerds.com/2012/03/a-php-function-to-get-vimeo-video-thumbnails-easily/

Any ideas?

Offline

#6 2013-02-20 17:58:54

etc
Developer
Registered: 2010-11-11
Posts: 5,471
Website GitHub

Re: Vimeo thumbnails and Open Graph

It will be easier to help you if you give an example of vimeo feed you are trying to import.

Offline

#7 2013-02-20 18:07:16

robhert
Member
From: Perú
Registered: 2007-04-27
Posts: 208
Website

Re: Vimeo thumbnails and Open Graph

Hi Oleg!

Ok. I want to add the thumbnail of a Vimeo video to the following: <meta property="og:image" content="URL.jpg" /> nothing else. So I could show it like this:

<head>
  <title>Title</title>
  <meta property="og:title" content="<txp:page_title />" /> 
  <meta property="og:image" content="http://www.IMAGE-URL.com/image.jpg" /> 
  <meta property="og:description" content="<txp:excerpt />" /> 
  <meta property="og:url" content="<txp:permlink />" />
</head>

Thank you in advance!

Offline

#8 2013-02-20 18:15:08

etc
Developer
Registered: 2010-11-11
Posts: 5,471
Website GitHub

Re: Vimeo thumbnails and Open Graph

Yes, but from where would you like to extract IMAGE-URL: json feed, xml, html? Could you provide an example, I don’t know vimeo very well.

Offline

#9 2013-02-20 18:33:22

etc
Developer
Registered: 2010-11-11
Posts: 5,471
Website GitHub

Re: Vimeo thumbnails and Open Graph

Well, in case you need to extract it from the video page itself, try

<meta property="og:image"<txp:etc_query url="http://vimeo.com/58729480" query="//meta[@property='og:image']/@content" />>

Mind, however, that connecting to vimeo will take some time. If your php is <5.3.8 (required for etc_query), you can try to do it with smd_xml.

Offline

#10 2013-02-20 19:21:47

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: Vimeo thumbnails and Open Graph

Vimeo has a public API. Therefor, there is really no need to parse the pages themselves. It’s not every elegant way of doing it.

Anyways, getting the video’s information from Vimeo is no brainer. You can either do it with the help of any XML parser plugin there is, or even do it without any plugins — it all just requires just few lines of very simple PHP.

Caching the data is pretty much no-brainer too. Since you are using articles, you could use custom fields store the values or a preference field even.

So how? Well, for example like this. The following solution is rather modular and allows you to fetch any meta value from any video. Requires just little bit of PHP and creating a form template.

Create a form template called vimeo, and populate it with the following:

<txp:php>

	/**
	 * Gets Vimeo video id from the contained statement and fetches the meta data for it.
	 *
	 * After loading the meta data, it populates variables containing each and every
	 * value. Each of these variables is prefixed with 'vimeo:'.
	 *
	 * All meta files are cached to Textpattern's preferences table.
	 */

	$video_id = trim(yield(array()));

	if ($video_id)
	{
		$data = get_pref('abc_vimeo_v_'.$video_id);

		if (!$data)
		{
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, 'http://vimeo.com/api/v2/video/'.urlencode($video_id).'.json');
			curl_setopt($ch, CURLOPT_FAILONERROR, 1);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_TIMEOUT, 10);
			$file = curl_exec($ch);
			$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
			curl_close($ch);

			if ($file && $status == 200)
			{
				$data = $file;
				set_pref('abc_vimeo_v_'.$video_id, $data, 'abc_vimeo', PREF_HIDDEN);
			}
		}

		if ($data && $json = @json_decode($data))
		{
			foreach ($json[0] as $name => $value)
			{
				variable(array(
					'name'  => 'vimeo:'.txpspecialchars($name),
					'value' => txpspecialchars((string) $value),
				));
			}
		}
	}
</txp:php>

After that, you can get any information out of any Vimeo video by simply calling the form which you named vimeo. The snippet creates variables with each containing a value fetched from the API. Usage is rather simple, for instance:

<txp:output_form form="vimeo">
	58729480
</txp:output_form>

<txp:variable name="vimeo:thumbnail_large" />

Since you are storing the Video ID in a custom field, the above would become:

<txp:output_form form="vimeo">
	<txp:custom_field name="videovimeo" />
</txp:output_form>

<txp:variable name="vimeo:thumbnail_large" />
<txp:variable name="vimeo:title" />
<txp:variable name="vimeo:id" />
<txp:variable name="vimeo:description" />

Which would output thumbnail, title, id and description for the video specified in the custom field.

Requires PHP 5.2, cURL and Textpattern 4.5.0 or newer.

Last edited by Gocom (2013-02-20 19:26:01)

Offline

#11 2013-02-20 20:06:43

robhert
Member
From: Perú
Registered: 2007-04-27
Posts: 208
Website

Re: Vimeo thumbnails and Open Graph

Thank you very much Oleg and Jukka for your answer and your time.

Jukka , I’ve made exactly what you wrote and guess what!? It worked! Now when I debug https://developers.facebook.com/tools/debug I have no errors. Your code is a-must-to-have. Thank you again!

Robhert

Offline

Board footer

Powered by FluxBB