facebook twitter hatena line email

Php/HybridAuth

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

twitterやfacebookの認証のマッパーライブラリ

ダウンロード

http://hybridauth.sourceforge.net/userguide.html

サンプルコード(twitter

https://dev.twitter.com/apps/でread&writeにする

<?php
session_start();
$config = dirname(__FILE__) . '/hybridauth/config.php';
require_once(dirname(__FILE__) . '/hybridauth/Hybrid/Auth.php');
// create an instance for Hybridauth with the configuration file path as parameter
$hybridauth = new Hybrid_Auth($config);
$adapter = $hybridauth->authenticate("Twitter");
$user_profile = $adapter->getUserProfile();
echo "Ohai there! U are connected with: <b>{$adapter->id}</b><br />";
echo "As: <b>{$user_profile->displayName}</b><br />";
echo "And your provider user identifier is: <b>{$user_profile->identifier}</b><br />";
print_r($user_profile);
$adapter->logout();

ユーザデータ

すべてのプロバイダーで、マッピングされたユーザデータが取得できる

$user_profile = array(
[identifier] => 219000000
[webSiteURL] => 
[profileURL] => http://twitter.com/hogehoge
[photoURL] => http://a3.twimg.com/profile_images/1271919150/hogehoge_normal.png
[displayName] => hogehge
[description] => hogehoge
[firstName] => hoge
[lastName] => 
[gender] => male
[language] => 
[age] => 
[birthDay] => 01
[birthMonth] => 01
[birthYear] => 1981
[email] => hogehoge@gmail.com
[phone] => 
[address] => 
[country] => 
[region] => 
[city] => 
[zip] => 
)

ユーザ認証テーブル

CREATE TABLE `authentications` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL COMMENT 'refer to users.id',
  `provider` varchar(100) NOT NULL,
  `provider_uid` varchar(255) NOT NULL,
  `email` varchar(200),
  `display_name` varchar(150) NOT NULL,
  `first_name` varchar(100),
  `last_name` varchar(100),
  `profile_url` varchar(300),
  `website_url` varchar(300),
  `created` datetime NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `provider_uid` (`provider_uid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ユーザテーブルL

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `email` varchar(200) NOT NULL,
  `password` varchar(200) NOT NULL,
  `first_name` varchar(200) NOT NULL,
  `last_name` varchar(200) NOT NULL,
  `created_at` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 ;

getUserProfileとtableの対応

$provider_uid  = $user_profile->identifier;
$email         = $user_profile->email;
$first_name    = $user_profile->firstName;
$last_name     = $user_profile->lastName;
$display_name  = $user_profile->displayName;
$website_url   = $user_profile->webSiteURL;
$profile_url   = $user_profile->profileURL;

その他

facebook

  1. https://developers.facebook.com/appsからkeyとsecret取得
  2. 設定/編集/websiteにアクセス元ドメインを入れないと,"Error Message: Invalid redirect_uri: 指定されたURLは、アプリケーションの設定で許可されていません。"エラーが出る
<?php
$config = dirname(__FILE__) . '/hybridauth/config.php';
require_once("hybridauth/Hybrid/Auth.php");
$hybridAuth = new Hybrid_Auth($config);
$adapter = $hybridAuth->authenticate("Facebook");
$user_profile = $adapter->getUserProfile();
print_r($user_profile);
$adapter->logout();

参照

  • TwitterやFacebookへのログイン機能をこれ1個で実装できるPHPライブラリ「HybridAuth」

http://phpspot.org/blog/archives/2011/08/twitterfacebook_3.html

YahooJapanを追加

  • yahootest.php
<?php
$config = dirname(__FILE__) . '/hybridauth/config.php';
require_once("hybridauth/Hybrid/Auth.php");
$hybridauth = new Hybrid_Auth($config);
$adapter = $hybridauth->authenticate("YahooJapan");
$user_profile = $adapter->getUserProfile();
print_r($user_profile);
$adapter->logout();
  • hybridauth/Hybrid/Providers/YahooJapan.php
<?php
class Hybrid_Providers_YahooJapan extends Hybrid_Provider_Model_OpenID
{
  var $openidIdentifier = "yahoo.co.jp";
}
  • hybridauth/config.php
return 
  array(
   "providers" => array ( 
      "YahooJapan" => array ( 
        "enabled" => true 
      ),
    ),
  );

Mixiを追加

  • mixitest.php
<?php
$config = dirname(__FILE__) . '/hybridauth/config.php';
require_once("hybridauth/Hybrid/Auth.php");
$hybridAuth = new Hybrid_Auth($config);
$adapter = $hybridAuth->authenticate("Mixi");
$user_profile = $adapter->getUserProfile();
print_r($user_profile);
$adapter->logout();
  • hybridauth/Hybrid/Providers/Mixi.php
<?php
class Hybrid_Providers_Mixi extends Hybrid_Provider_Model_OpenID
{
  var $openidIdentifier = "https://mixi.jp";
}
  • hybridauth/config.php
return 
  array(
   "providers" => array ( 
      "Mixi" => array ( 
        "enabled" => true 
      ),
    ),
  );

Livedoorを追加

  • livedoortest.php
<?php
$config = dirname(__FILE__) . '/hybridauth/config.php';
require_once("hybridauth/Hybrid/Auth.php");
$hybridAuth = new Hybrid_Auth($config);
$adapter = $hybridAuth->authenticate("Livedoor");
$user_profile = $adapter->getUserProfile();
print_r($user_profile);
$adapter->logout();
  • hybridauth/Hybrid/Providers/Livedoor.php
<?php
class Hybrid_Providers_Livedoor extends Hybrid_Provider_Model_OpenID
{
  var $openidIdentifier = "http://livedoor.com";
}
  • hybridauth/config.php
return 
  array(
   "providers" => array ( 
      "Livedoor" => array ( 
        "enabled" => true 
      ),
    ),
  );