Open Source | Technology | Web

eZ Publish Social Media Integration: Feeds

In the previous blog post we announced the  nxc_social_networks extension and demonstrated how to implement Sign-on buttons. This blog post describes how to fetch feeds from social networks (Facebook, Twitter and Google+) and use them within eZ Publish templates.

How does it work?

nxc_social_networks extension provides several eZ Publish template fetch functions that return various feeds from Facebook, Twitter and Google+. eZ Publish interacts with social networks via OAuth 2.0 protocol. Each fetch function result is cached for 1 minute.

In order to enabled social network fetch functions, you need to do the following:

  • Create social networks applications. If you did it for Sign-on buttons, you should use the same ones, no need to do it again.
  • Generate access token for each social network: after installing nxc_social_networks extension, there should be “Social Network Tokens Management” tab in the ‘Setup’ section of eZ Publish back-end. To generate a token, click on each social network icon. You will be redirected to a confirmation page, where you should approve the permissions. Afterwards you will be redirected back to eZ Publish back-end, and access token will be saved to eZ Publish database. You should generate token for each social network just once. After this, fetch functions are ready to use:

Using template fetch functions

The following fetch function are available in the extension:

facebook_timeline

This fetch function fetches user`s or page`s timeline content (https://developers.facebook.com/docs/reference/api/user/#feed). Available variables are:

  • page_id – if this option is empty, the page of user`s timeline will be fetched, using the account that was used to generate OAuth 2.0 access token
  • limit – the maximum number of items to include into response
Usage examples:
Source code    
  1. <h3>Wall timeline</h3>
  2. {def $timeline = fetch(
  3.     'nxc_social_network_feed',
  4.     'facebook_timeline',
  5.     hash(
  6.         'limit', 10
  7.     )
  8. )}
  9. <ul>
  10.     {foreach $timeline as $message}
  11.         <li>
  12.             <h5>{$message.story}</h5>
  13.  
  14.             {if eq( $message.type, 'photo' )}
  15.             <a href="{$message.link}"><img src="{$message.picture}" alt="{$message.name}" /></a>
  16.             {elseif eq( $message.type, 'video' )}
  17.             <a href="{$message.link}"><img src="{$message.picture}" alt="{$message.name}" /></a>
  18.             {elseif eq( $message.type, 'link' )}
  19.             <a href="{$message.link}">{$message.name}</a>
  20.             {$message.description}
  21.             {else}
  22.             {$message.message}
  23.             {/if}
  24.  
  25.             <p>{$message.created_ago}</p>
  26.         </li>
  27.     {/foreach}
  28. </ul>
  29. <h3>Page timeline</h3>
  30. {set $timeline = fetch(
  31.     'nxc_social_network_feed',
  32.     'facebook_timeline',
  33.     hash(
  34.         'page_id', '503169393046429',
  35.         'limit', 10
  36.     )
  37. )}
  38. <ul>
  39.     {foreach $timeline as $message}
  40.         <li>
  41.             <h5>{$message.story}</h5>
  42.  
  43.             {if eq( $message.type, 'photo' )}
  44.             <a href="{$message.link}"><img src="{$message.picture}" alt="{$message.name}" /></a>
  45.             {elseif eq( $message.type, 'video' )}
  46.             <a href="{$message.link}"><img src="{$message.picture}" alt="{$message.name}" /></a>
  47.             {elseif eq( $message.type, 'link' )}
  48.             <a href="{$message.link}">{$message.name}</a>
  49.             {$message.description}
  50.             {else}
  51.             {$message.message}
  52.             {/if}
  53.  
  54.             <p>{$message.created_ago}</p>
  55.         </li>
  56.     {/foreach}
  57. </ul>
  58. {undef $timeline}

twitter_timeline

This fetch function returns various twitter feeds. Available variables are:

Usage examples:
Source code    
  1. {def $timeline = false()}
  2. <h1>Twitter feeds:</h1>
  3. <h3>Home timeline</h3>
  4. {set $timeline = fetch(
  5.     'nxc_social_network_feed',
  6.     'twitter_timeline',
  7.     hash(
  8.         'type', 'home',
  9.         'parameters', hash(
  10.             'count', 5
  11.         )
  12.     )
  13. )}
  14. <ul>
  15.     {foreach $timeline as $message}
  16.         <li>{$message.text}, {$message.created_ago}</li>
  17.     {/foreach}
  18. </ul>
  19. <h3>User timeline including retweets</h3>
  20. {set $timeline = fetch(
  21.     'nxc_social_network_feed',
  22.     'twitter_timeline',
  23.     hash(
  24.         'type', 'user',
  25.         'parameters', hash(
  26.             'screen_name', 'ezsystems',
  27.             'count', 10,
  28.             'include_rts', true()
  29.         )
  30.     )
  31. )}
  32. <ul>
  33.     {foreach $timeline as $message}
  34.         <li>{$message.text}, {$message.created_ago}</li>
  35.     {/foreach}
  36. </ul>
  37. <h3>Mentions timeline</h3>
  38. {set $timeline = fetch(
  39.     'nxc_social_network_feed',
  40.     'twitter_timeline',
  41.     hash(
  42.         'type', 'mentions',
  43.         'parameters', hash(
  44.             'count', 10
  45.         )
  46.     )
  47. )}
  48. <ul>
  49.     {foreach $timeline as $message}
  50.         <li>{$message.text}, {$message.created_ago}</li>
  51.     {/foreach}
  52. </ul>
  53. {undef $timeline}

twitter_user_info

This fetch function returns information about user account, which was used to generate OAuth 2.0 access token (https://dev.twitter.com/docs/api/1.1/get/users/show).

Usage example:

Source code    
  1. <h3>User info</h3>
  2. {def $twitter_info = fetch( 'nxc_social_network_feed', 'twitter_user_info' )}
  3. <p>{$twitter_info.name}</p>
  4. <p>{$twitter_info.location}</p>
  5. <p>{$twitter_info.followers_count} followers</p>
  6. {undef $twitter_info}

google_activities_list

This fetch function returns the list of activities from Google+ (https://developers.google.com/+/api/latest/activities/list). Available variables are:

  • user_id – if it equals to ‘me’, user activities will be fetched, using the account that was used to generate OAuth 2.0 access token
  • limit – the maximum number of activities to include in the response. Acceptable values are from 1 to 100, inclusive.
Usage example:
Source code    
  1. <h3>Activities list</h3>
  2. {def $activities = fetch(
  3.     'nxc_social_network_feed',
  4.     'google_activities_list',
  5.     hash(
  6.         'user_id', '114649176555802716836',
  7.         'limit', 10
  8.     )
  9. )}
  10. <ul>
  11.     {foreach $activities as $activity}
  12.         <li>
  13.             <h5><a href="{$activity.url}">{$activity.title|wash}</a></h5>
  14.             <div>{$activity.object.content}</div>
  15.             <p>{$activity.created_ago}</p>
  16.         </li>
  17.     {/foreach}
  18. </ul>
  19. {undef $activities}

google_activities_search

This fetch function searches Google+ public activities (https://developers.google.com/+/api/latest/activities/search). Available variables are:

  • query – full-text search query string
  • limit – the maximum number of activities to include in the response. Acceptable values are from 1 to 20, inclusive
  • sorting – possible values are ‘best’ and ‘recent’

Usage example:

Source code    
  1. <h3>Activities search</h3>
  2. {def $activities = fetch(
  3.     'nxc_social_network_feed',
  4.     'google_activities_search',
  5.     hash(
  6.         'query', 'eZ Publish',
  7.         'limit', 5,
  8.         'sorting', 'recent'
  9.     )
  10. )}
  11. <ul>
  12.     {foreach $activities as $activity}
  13.         <li>
  14.             <h5><a href="{$activity.url}">{$activity.title|wash}</a></h5>
  15.             <div>{$activity.object.content}</div>
  16.             <p>{$activity.created_ago}</p>
  17.         </li>
  18.     {/foreach}
  19. </ul>
  20. {undef $activities}

Suggestions are welcome. If you have any questions you can ask them in the comments.

Stay tuned, the next post will describe how to publish to social networks from eZ Publish.

Print this post

0 Responses to "eZ Publish Social Media Integration: Feeds"

Post a Comment:

Get latest news