📜 ⬆️ ⬇️

PHPixie Social - easy integration with social networks

image
Authorization through social networks is one of the most frequent tasks faced by developers of entertainment sites. It would seem there is nothing to do, because for each API there is a PHP library from the vendor itself. But what if you need to connect several? I do not want to pull a bunch of libraries into the project that implement the same OAuth protocol, and I would also like to have some kind of a single interface. PHPixie Social is a small library with only one dependency, which makes it easy to work immediately with Facebook, Twitter, Google and Vkontakte, and if you use the PHPixie framework, you also immediately get authorization in just a few lines of code.
But first, consider the component itself.



Configuration
')
<?php $config = array( //     , //          API 'facebook' => array( 'type' => 'facebook', 'appId' => '', 'appSecret' => '', //      , 'scope' => array(), //   API 'apiVersion' => '2.3' ), 'twitter' => array( 'type' => 'twitter', // Twitter   OAuth 1.0a //    'consumerKey' => '', 'consumerSecret' => '' ), 'google' => array( 'type' => 'google', 'appId' => '', 'appSecret' => '', //  'scope' => array(), 'apiVersion' => '2.3' ), 'vk' => array( 'type' => 'vk', 'appId' => '', 'appSecret' => '', //  'scope' => array(), 'apiVersion' => '2.3' ), ); 


Initialization

 //    $slice = new \PHPixie\Slice(); $config = $slice->arrayData($config); $social = new \PHPixie\Social($config); 


User Authorization

So the first thing we need to do is ask the user for authorization, for this we need to come up with some kind of link where the API will send us an access token. Then we redirect the user to the login page. Here is a simple example:

 $callbackUrl = 'http://localhost.com/callback=1'; if(!isset($_GET['callback'])) { //  ,   $loginUrl = $social->get('facebook')->loginUrl($callbackUrl); header('Location: '.$loginUrl); } else { //   ,        API, //        Social  . // ,   $callbackUrl    $socialUser = $social->get('faceebook')->handleCallback($callbackUrl, $_GET); if($socialUser === null) { //    echo "You didn't authorize our app"; }else{ //   API    var_dump($socialUser->get('me')); } } 


User object

 $socialUser->id(); // ID    //      $socialUser->loginData(); // GET  $socialUser->get('some/endpoint', $queryParams = array()); // POST  $socialUser->post('some/endpoint', $data = array(), $queryParams = array()); //   $socialUser->api('PUT', 'some/endpoint', $queryParams = array(), $data = array()); //  , //      //      $token = $social->token(); //     $socialUser = $social->get('facebook')->user($token); //        $social->get('facebook')->get($token, 'some/endpoint', $queryParams = array()); $social->get('facebook')->post($token, 'some/endpoint', $data = array(), $queryParams = array()); $social->get('facebook')->api($token, 'PUT', 'some/endpoint', $queryParams = array(), $data = array()); 


Integration with the framework

In the framework, by default, a plugin is available to the authorization module, which handles user login and is easily included in its configuration. Those who are already working with the framework will not find anything difficult in this, so this time, instead of a description, I will leave a link to the demo project: https://github.com/phpixie/demo-socialauth

In it, the user chooses which network to authenticate through. If he logs in for the first time, he will immediately create an entry in the Users tab and his login will be remembered through the session. At the subsequent login, the entity from the base will already be used.

To try Social, you just need the composer require phpixie / social , and as always, if you have any questions, please contact us directly in the chat.

Source: https://habr.com/ru/post/303108/


All Articles