QA@IT
«回答へ戻る

回答を投稿

<?php

session_start();
//ここは各phpファイルをおいた場所を参照
require_once( 'vendor/facebook/php-sdk-v4/src/Facebook/FacebookSession.php' );
require_once( 'vendor/facebook/php-sdk-v4/src/Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'vendor/facebook/php-sdk-v4/src/Facebook/FacebookRequest.php' );
require_once( 'vendor/facebook/php-sdk-v4/src/Facebook/FacebookResponse.php' );
require_once( 'vendor/facebook/php-sdk-v4/src/Facebook/FacebookSDKException.php' );
require_once( 'vendor/facebook/php-sdk-v4/src/Facebook/FacebookRequestException.php' );
require_once( 'vendor/facebook/php-sdk-v4/src/Facebook/FacebookAuthorizationException.php' );
require_once( 'vendor/facebook/php-sdk-v4/src/Facebook/GraphObject.php' );
require_once( 'vendor/facebook/php-sdk-v4/src/Facebook/FacebookSession.php' );
require_once( 'vendor/autoload.php' );

//ここはこのままでOK
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
// init app with app id (APPID) and secret (SECRET)
FacebookSession::setDefaultApplication('xxxxxxxxxxxxxxxxx','xxxxxxxxxxxxxxxxxxxxx');
$helper = new FacebookRedirectLoginHelper('http://localhost:8887/logbal/login.php');
try {
$session = $helper->getSessionFromRedirect();
} catch(FacebookRequestException $ex) {
// When Facebook returns an error
} catch(\Exception $ex) {
// When validation fails or other local issues
}
if ($session) {
// ログイン済み
try {
$request = new FacebookRequest( $session, 'GET', '/me');
$response = $request->execute();
$user_profile = $graphObject;
$graphObject = $response->getGraphObject();

echo "";
echo print_r( $graphObject );

$con = mysql_connect('localhost', 'root', 'root');
if (!$con) {
exit('データベースに接続できませんでした。');
}

$result = mysql_select_db('DEMO', $con);
if (!$result) {
exit('DEMOデータベースを選択できませんでした。');
}

$user_profile['id']     = $_REQUEST['id'];
$user_profile['gender'] = $_REQUEST['gender'];
$user_profile['name']   = $_REQUEST['name'];
$user_profile['link']   = $_REQUEST['link'];

$result = mysql_query("INSERT INTO users(id, gender, name, link) VALUES('$id', '$gender', '$name', '$link')", $con);
if (!$result) {
exit('データを登録できませんでした。');
}

echo "";
} catch(FacebookRequestException $e) {
echo "Exception occured, code: " . $e->getCode();
echo " with message: " . $e->getMessage();
}
}else{
$loginUrl = $helper->getLoginUrl();
echo'Login With Facebook';
}
?>

<html>
<meta charset="UTF-8" content="text/html">
<?php

session_start();
//ここは各phpファイルをおいた場所を参照
require_once( 'vendor/facebook/php-sdk-v4/src/Facebook/FacebookSession.php' );
require_once( 'vendor/facebook/php-sdk-v4/src/Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'vendor/facebook/php-sdk-v4/src/Facebook/FacebookRequest.php' );
require_once( 'vendor/facebook/php-sdk-v4/src/Facebook/FacebookResponse.php' );
require_once( 'vendor/facebook/php-sdk-v4/src/Facebook/FacebookSDKException.php' );
require_once( 'vendor/facebook/php-sdk-v4/src/Facebook/FacebookRequestException.php' );
require_once( 'vendor/facebook/php-sdk-v4/src/Facebook/FacebookAuthorizationException.php' );
require_once( 'vendor/facebook/php-sdk-v4/src/Facebook/GraphObject.php' );
require_once( 'vendor/facebook/php-sdk-v4/src/Facebook/FacebookSession.php' );
require_once( 'vendor/autoload.php' );
 
//ここはこのままでOK
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
// init app with app id (APPID) and secret (SECRET)
FacebookSession::setDefaultApplication('xxxxxxxxxxxxxxxxx','xxxxxxxxxxxxxxxxxxxxx');
$helper = new FacebookRedirectLoginHelper('http://localhost:8887/logbal/login.php');
try {
  $session = $helper->getSessionFromRedirect();
} catch(FacebookRequestException $ex) {
  // When Facebook returns an error
} catch(\Exception $ex) {
  // When validation fails or other local issues
}
if ($session) {
  // ログイン済み
  try {
    $request = new FacebookRequest( $session, 'GET', '/me');
    $response = $request->execute();
    $user_profile = $graphObject;
    $graphObject = $response->getGraphObject();

echo "<pre>";
    echo  print_r( $graphObject );

    $con = mysql_connect('localhost', 'root', 'root');
    if (!$con) {
    exit('データベースに接続できませんでした。');
    }

    $result = mysql_select_db('DEMO', $con);
    if (!$result) {
    exit('DEMOデータベースを選択できませんでした。');
    }

    $user_profile['id']     = $_REQUEST['id'];
    $user_profile['gender'] = $_REQUEST['gender'];
    $user_profile['name']   = $_REQUEST['name'];
    $user_profile['link']   = $_REQUEST['link'];

    $result = mysql_query("INSERT INTO users(id, gender, name, link) VALUES('$id', '$gender', '$name', '$link')", $con);
    if (!$result) {
    exit('データを登録できませんでした。');
    }

echo "</pre>";
    } catch(FacebookRequestException $e) {
    echo "Exception occured, code: " . $e->getCode();
    echo " with message: " . $e->getMessage();
    }
  }else{
    $loginUrl = $helper->getLoginUrl();
    echo'<a href="' . $loginUrl .'">Login With Facebook</a>';
    }
?>
</html>