Coder Social home page Coder Social logo

splashlab / yii-facebook-opengraph Goto Github PK

View Code? Open in Web Editor NEW

This project forked from digitick/yii-faceplugs

44.0 16.0 13.0 339 KB

Yii Extension : A wrapper for the Facebook PHP SDK, Javascript SDK and the Open Graph protocol.

Home Page: http://www.yiiframework.com/extension/facebook-opengraph/

PHP 100.00%

yii-facebook-opengraph's Introduction

The master branch is not stable, please use tagged release branches. The previous stable branch with support for the old API is now under the opengraph-v1.0 branch.

This is a Yii1 application component wrapper for Facebook PHP SDK 5.0

Also included are some helper functions that:

  • Include the Facebook JS SDK on your pages
  • Allow setting Open Graph meta tags
  • Easy rendering of Facebook Social Plugins.

Facebook PHP SDK:

Facebook JS SDK:

Facebook Social Plugins:

Open Graph Protocol:

Upgrading from PHP SDK 4 to PHP SDK 5


INSTALLATION:

It is recommended that you install this via composer. This extension is hosted on packagist.org: https://packagist.org/packages/splashlab/yii-facebook-opengraph

"require": {
    "splashlab/yii-facebook-opengraph": "dev-master"
}

Instead of 'dev-master' you can choose a release tag like '1.0.2-beta'.

Run composer update to get the extension. This will pull down the official Facebook SDK as a dependency.

Configure Yii application component SFacebook in your yii config file:

'components'=>[
    'facebook'=>[
        'class' => '\YiiFacebook\SFacebook',
        'appId'=>'YOUR_FACEBOOK_APP_ID', // needed for JS SDK, Social Plugins and PHP SDK
        'secret'=>'YOUR_FACEBOOK_APP_SECRET', // needed for the PHP SDK
        //'version'=>'v2.5', // Facebook API version to default to i.e. default_graph_version
        //'enable_beta_mode' => null, // optional Facebook\Facebook setting
        //'http_client_handler' => null, // optional Facebook\Facebook setting
        //'persistent_data_handler' => null, // optional Facebook\Facebook setting
        //'url_detection_handler' => null, // optional Facebook\Facebook setting
        //'pseudo_random_string_generator' => null, // optional Facebook\Facebook setting
        //'locale'=>'en_US', // override JS SDK locale setting (defaults to en_US)
        //'jsSdk'=>true, // include JavaScript SDK on all pages
        //'async'=>true, // load JavaScript SDK asynchronously
        //'jsCallback'=>false, // declare if you are going to be inserting any JS callbacks to the async JS SDK loader
        //'callbackScripts'=>'', // default JS SDK init callback JavaScript
        //'status'=>false, // JS SDK - check login status
        //'cookie'=>false, // JS SDK - enable cookies to allow the server to access the session
        //'xfbml'=>false,  // JS SDK - parse XFBML / html5 Social Plugins
        //'frictionlessRequests'=>false, // JS SDK - enable frictionless requests for request dialogs
        //'hideFlashCallback'=>null, // JS SDK - A function that is called whenever it is necessary to hide Adobe Flash objects on a page.
        //'html5'=>true,  // use html5 Social Plugins instead of older XFBML
        //'defaultScope'=>[], // default Facebook Login permissions to request with Login button
        //'redirectUrl'=>null, // default Facebook post-Login redirect URL
        //'userFbidAttribute'=>null, // if using FBAuthRequest, declare Facebook ID attribute on user model here
        //'accountLinkUrl'=>null, // if using FBAuthRequest, declare link to user account page here
        //'ogTags'=>[  // set default OG tags
            //'og:title'=>'MY_WEBSITE_NAME',
            //'og:description'=>'MY_WEBSITE_DESCRIPTION',
            //'og:image'=>'URL_TO_WEBSITE_LOGO',
        //],
        //'authenticationErrorCallback' => function(\Facebook\Exceptions\FacebookAuthenticationException $e) {
            // special logic for expired API accessTokens
            // prompt to re-authenticate?
            // log error?
            // throw $e?
            // return true?
        //},
        //'authorizationErrorCallback' => function(\Facebook\Exceptions\FacebookAuthorizationException $e) {
            // special logic for missing API permissions
            // prompt to authorize additional permissions?
            // log error?
            // throw $e?
            // return true?
        //},
        //'sdkErrorCallback' => function(\Facebook\Exceptions\FacebookSDKException $e) {
            // special logic for generic Facebook SDK errors
            // log error?
            // throw $e?
            // return true?
        //}
    ],
],

Then, to enable the JS SDK and Open Graph meta tag functionality in your base Controller, add this function to override the afterRender() callback:

protected function afterRender($view, &$output) {
    parent::afterRender($view,$output);
    //Yii::app()->facebook->addJsCallback($js); // use this if you are registering any additional $js code you want to run on init()
    Yii::app()->facebook->initJs($output); // this initializes the Facebook JS SDK on all pages
    Yii::app()->facebook->renderOGMetaTags(); // this renders the OG tags
    return true;
}

Installation without Composer

Included is an autoload.php file to assist in installing this extension without Composer. YMMV.


USAGE:

Open Graph meta tags

Some Open Graph meta tags are set by default.

Set custom OG tags on a page (in view or action):

<?php Yii::app()->facebook->ogTags['og:title'] = "My Page Title"; ?>

Social Plugin helper widgets

Render Facebook Social Plugins using helper Yii widgets:

<?php $this->widget('\YiiFacebook\Plugins\LikeButton', [
     //'href' => 'YOUR_URL', // if omitted Facebook will use the current page URL
     'show_faces'=>true,
     'share' => true
]); ?>

You can, of course, just use the XFBML code for social plugins as well (if loading the JS SDK with XFBML = true):

<div class="fb-like" data-share="true" data-show-faces="true"></div>

Run JS after Facebook JS SDK initializes

At any point you can add additional JavaScript code to run after the Facebook JS SDK initializes:

<?php Yii::app()->facebook->addJsCallback($js); ?>

Calling Facebook PHP SDK methods

To use the PHP SDK anywhere in your application, just call it like so (there pass-through the Facebook class):

<?php $accessToken = Yii::app()->facebook->getAccessToken() ?>
<?php $accessToken = Yii::app()->facebook->getLongLivedAccessToken() ?>
<?php $loginUrl = Yii::app()->facebook->getLoginUrl($redirect_url_after_login) ?>
<?php $reRequestUrl = Yii::app()->facebook->getReRequestUrl() ?>
<?php $reAuthenticationUrl = Yii::app()->facebook->getReAuthenticationUrl() ?>
<?php $signedRequest = Yii::app()->facebook->getSignedRequest() ?>
<?php $logoutUrl = Yii::app()->facebook->getLogoutUrl('http://example.com/after-logout') ?>

Graph API calls

Calling API methods directly on the Yii::app()->facebook component will automatically check for and add the proper accessToken for the logged in user. Call them like so:

<?php $graphPageObject = Yii::app()->facebook->get('/me')->getUserNode() ?>
<?php $graphPageObject = Yii::app()->facebook->get('/SOME_PAGE_ID')->getPageNode() ?>
<?php $response = Yii::app()->facebook->post('/me/feed', [
                    'message' => 'User provided message'
                    'link' => 'www.example.com',
                ])->getGraphNode() ?>
<?php Yii::app()->facebook->$fb->delete('/{node-id}') ?>

Facebook Exception Handlers

If you call the SDK methods directly on the Yii::app()->facebook component then some default error handling logic will run. You can override this logic by specifying 3 different global Facebook error handlers:

  • authenticationErrorCallback => function(\Facebook\Exceptions\FacebookAuthenticationException $e)
  • authorizationErrorCallback => function(\Facebook\Exceptions\FacebookAuthorizationException $e)
  • sdkErrorCallback => function(\Facebook\Exceptions\FacebookSDKException $e)

Direct Graph API Calls

If you want to make API calls without the default accessToken or without the global error handlers, call it via the fb property like this (Yii::app()->facebook->fb):

<?php
try {
    $response = Yii::app()->facebook->fb->post('/me/feed', [
        'link' => 'www.example.com',
        'message' => 'User provided message'
    ])->getGraphNode()
    echo "Posted with id: " . $response->getField('id');
} catch (\Facebook\Exceptions\FacebookSDKException $e) {
    // your own error handlers
    echo "Exception occurred, code: " . $e->getCode();
    echo " with message: " . $e->getMessage();
}
?>
<?php Yii::app()->facebook->destroySession() ?>

Convenience Methods

There are a couple of additional convenience methods added the component:

<?php $fb_user_id = Yii::app()->facebook->getUserId() ?>
<?php $graphUserObject = Yii::app()->facebook->getMe() // gets the Graph info of the current user ?>
<?php $graphUserObject = Yii::app()->facebook->getGraphUser($user_id) // gets the Graph object of the current user ?>
<?php $imageUrl = Yii::app()->facebook->getProfilePicture('large') // gets the Facebook picture URL of the current user ?>
<?php $imageUrl = Yii::app()->facebook->getProfilePicture(['height'=>300,'width'=>300]) // $size can also be specific ?>
<?php $imageUrl = Yii::app()->facebook->getProfilePictureById($openGraphId, $size) // gets the Facebook picture URL of a given OG entity ?>

Also included are two helper classes: FBAuthRequest and FBUserIdentity

FBAuthRequest can be used to prompt a user to log in with Facebook if no active Facebook Session is detected. The message is displayed as a notice Flash message. You can optionally pass in Facebook permissions to prompt for.

\YiiFacebook\FBAuthRequest::fbLoginPrompt();
\YiiFacebook\FBAuthRequest::fbLoginPrompt(['manage_pages']);

FBUserIdentity is a base CBaseUserIdentity class that can be extended when adding Facebook authentication to your application.


BREAKING CHANGES:

  • New version 1.x breaks everything from previous version and requires PHP 5.4
  • 2.x version breaks most things, now using the PHP SDK v5 and Graph API v2.5

CHANGE LOG:

  • 1.0.2-beta Updated to PHP SDK 4.0 and Open Graph API 2.2
  • 2.0.0-beta Updated to PHP SDK 5.1 and Open Graph API 2.5

I plan on continuing to update and bugfix this extension as needed.

Please log bugs to the GitHub tracker.

Extension is posted on Yii website also: http://www.yiiframework.com/extension/facebook-opengraph/

The original version with support for Facebook SDK 3.x and Open Graph API 1.x was forked from ianare's faceplugs Yii extension: http://www.yiiframework.com/extension/faceplugs https://github.com/digitick/yii-faceplugs

Updated Feb 19th 2016 by Evan Johnson http://splashlabsocial.com

yii-facebook-opengraph's People

Contributors

brentjanderson avatar ianare avatar kidsil avatar nidgetgod avatar thaddeusmt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

yii-facebook-opengraph's Issues

Cant get the extendend Access Token

Hello,
Maybe im wrong but if i get an extended Accesstoken it is stored nowhere and there is no return value, so i cant use it.

Maybe a bug? Or how i can read it out?

best regards

Yii-facebook-opengraph like button error

I did exactly follow the instructions here :http://www.yiiframework.com/extension/facebook-opengraph
But when I click like button, then the error appears. The error code is: 1404009. While sharing still works fine.
Here is the code:
In main.php :
'components'=>array(
'facebook'=>array(
'class' => 'ext.yii-facebook-opengraph.SFacebook',
'appId'=>'...',
'secret'=>'...', ),),
In SiteController.php
protected function afterRender($view, &$output) {
parent::afterRender($view,$output);
Yii::app()->facebook->initJs($output);
Yii::app()->facebook->renderOGMetaTags();
return true; }
In View:

widget('ext.yii-facebook-opengraph.plugins.LikeButton', array( 'href' => 'https://www.facebook.com/newordersystem', 'show_faces'=>true, 'send' => TRUE )); ?>

Please help me. Thank you very much.
Sorry for my bad english.

Update: I discovered that when I logout Facebook before like the errors will not appear.So, I think one should insert code log out of facebook before like. Can someone help me?

meta tag's full name

setting custom meta tag does not work.
public function renderOGMetaTags() {
$this->ogTags['fb:app_id'] = $this->appId; // set this app ID og tag, for Facebook insights and administration
if (!isset($this->ogTags['type']))
$this->ogTags['og:type'] = 'website'; // set website as the default type
if (!isset($this->ogTags['title']))
$this->ogTags['og:title'] = Yii::app()->name; // default to App name
if (!isset($this->ogTags['url']))
$this->ogTags['og:url'] = $this->getProtocol()."://".Yii::app()->request->serverName.Yii::app()->request->requestUri; // defaults to current URL
foreach ($this->ogTags as $type => $value) { // loop through any other OG tags declared
$this->registerOpenGraph($type, $value);
}
}
here I think should be like this
public function renderOGMetaTags() {
$this->ogTags['fb:app_id'] = $this->appId; // set this app ID og tag, for Facebook insights and administration
if (!isset($this->ogTags['og:type']))
$this->ogTags['og:type'] = 'website'; // set website as the default type
if (!isset($this->ogTags['tog:itle']))
$this->ogTags['og:title'] = Yii::app()->name; // default to App name
if (!isset($this->ogTags['og:url']))
$this->ogTags['og:url'] = $this->getProtocol()."://".Yii::app()->request->serverName.Yii::app()->request->requestUri; // defaults to current URL
foreach ($this->ogTags as $type => $value) { // loop through any other OG tags declared
$this->registerOpenGraph($type, $value);
}
}

Comments.php

in class Comments

public $href is not defined

public $numposts
should be
$num_posts ?

Posting to my page.

I have a FaceBook page that should automatically receive new blog entries from the site and post them there as well. What are the lines of code needed to post to the page whose API key and secret I had supplied?

Kind regards, Ingwie.

Non-composer version

Hi all,

Firstly, thank you for the awesome Yii plugin. I have been using this for years.
May I request to have the non-composer version to download?

Thank you in advance.

Regards,
Ronny

og Tags default Configuration

Hi,
in the Docs and default files is standing:
//'ogTags'=>array( // set default OG tags
//'title'=>'MY_WEBSITE_NAME',
//'description'=>'MY_WEBSITE_DESCRIPTION',
//'image'=>'URL_TO_WEBSITE_LOGO',
//),

this should be:
//'ogTags'=>array( // set default OG tags
//'og:title'=>'MY_WEBSITE_NAME',
//'og:description'=>'MY_WEBSITE_DESCRIPTION',
//'og:image'=>'URL_TO_WEBSITE_LOGO',
//),

Or not?

typo in example

widget('ext.yii-facebook-opengraph.plugins.LikeButton', array( //'href' => 'YOUR_URL', // if omitted Facebook will use the OG meta tag 'show-faces'=>true, 'send' => true )); ?>

show-faces

should be

show_faces

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.