17
AOUT

HybridAuth - Une librairie PHP pour se connecter aux services sociaux

Publié le 17/08/11 à 10h21 par DJo

HybridAuth est une librairie PHP open source permettant de gérer l'authentification auprès des services sociaux les plus populaires.

HybridAuth est une librairie PHP open source permettant de gérer l'authentification auprès des services sociaux les plus populaires.

De nombreux réseaux sociaux et services web proposent des systèmes d'authentification centralisés intégrables directement dans nos propres applications web:

  • Google Account
  • Facebook et son fameux Connect with Facebook
  • Twitter avec OAuth
  • Foursquare
  • vimeo
  • LinkedIn
  • Flickr
  • Tumblr
  • OpenID
  • ...

Utiliser un service tiers pour faciliter l'inscription et identification d'un membre est très intéressant et vous permettra de gagner en nombre de membre (Oui, l'internaute est fainéant ! Il préférera cliquer sur son bouton Connect with Twitter, plutôt que de remplir un formulaire ...).

HybridAuth va vous permettre de gagner pas mal de temps en proposant une librairie prête à emploi sur tous ces services. Elle permettra donc de vous connecter via ces services et vous retournera les infos de l'utilisateur une fois connecté.

Voici un exemple basique d'utilisation du script pour s'identifier sur twitter:

<?php

    # First, we need to start a new PHP session
    session_start();


    # include HybridAuth main file
    include "/path/to/hybridauth/hybridauth.php"; 


    # create a new Hybrid_Auth instance
    $hybridauth = new Hybrid_Auth();


    # and let check for errors from the start!
    # if Hybrid_Auth::hasError() return true, then we display the error message 
    if( $hybridauth->hasError() )
    { 

        # Hybrid_Auth::getErrorMessage() will return the last error message
        echo "Ooops error: " . $hybridauth->getErrorMessage() ; 

        # and dont forget to call Hybrid_Auth::endSession() to rest the current session 
        $hybridauth->endSession();  

        die( "Please try again!" ); 
    } 


    # if the user is NOT logged in with twitter yet, then let try to signin
    if( ! $hybridauth->hasSession() )
    {
        # hybridauth required params
        $params = array();


        // we need to tell the social API where the user need to be redirected after authentification
        // * in our case, let say we need the user to return back to this same page
        // * By default if the hauth_return_to parameter is not given, then HybridAuth will redirect 
        //   back the user to the same page where he come from.
        // Note: we can use the built-in function Hybrid_Auth::getCurrentUrl() to get the current URL
        $params["hauth_return_to"]      = $hybridauth->getCurrentUrl() ; 


        # then let setup a new adapter for twitter with the given parameters
        # (You can change "Twitter" later on to whatever you want : Google, Facebook, etc.)
        $provider_adapter = $hybridauth->setup( "Twitter", $params );  


        # and let start the login process (this step will redirect the user to the Endpoint) 
        $provider_adapter->login();
    }
    # else, if the user is already connected 
    else
    { 
        # then, we resetup the provider adapter
        $provider_adapter = $hybridauth->wakeup(); 

        # and, grab the user data from the provider
        $user_data        = $provider_adapter->user();

        # print something using the user profile 
        echo "User connected with <b>{$user_data->providerId}</b> as <b>{$user_data->profile->displayName}</b> : <hr /><pre>";

        # dump the user data
        print_r( $user_data );

    }

Pratique non ?

 

Site Officiel

Baraguiné par tsunamichups le 17/08/11 à 11h22
tsunamichups sur La Ferme du Web
ah ba oui pour le coups s'est plus qu'utile !!

Ajouter un Commentaire

Pour poster un commentaire, vous devez être identifié. Vous pouvez choisir parmi ces trois méthodes d'identification:

Compte la Ferme du Web

Identifiez-vous
Inscrivez-vous

Compte Facebook

Compte Twitter

Connexion avec votre compte twitter
Rechercher sur la Ferme du web