1 | <?php |
---|
2 | session_start(); |
---|
3 | |
---|
4 | // include hybridauth lib |
---|
5 | $config = dirname(__FILE__) . '/../../hybridauth/config.php'; |
---|
6 | require_once( "../../hybridauth/Hybrid/Auth.php" ); |
---|
7 | |
---|
8 | // start login with facebook? |
---|
9 | if( isset( $_GET["login"] ) ){ |
---|
10 | try{ |
---|
11 | $hybridauth = new Hybrid_Auth( $config ); |
---|
12 | |
---|
13 | $adapter = $hybridauth->authenticate( "facebook" ); |
---|
14 | |
---|
15 | $user_profile = $adapter->getUserProfile(); |
---|
16 | } |
---|
17 | catch( Exception $e ){ |
---|
18 | die( "<b>got an error!</b> " . $e->getMessage() ); |
---|
19 | } |
---|
20 | } |
---|
21 | |
---|
22 | // logged in ? |
---|
23 | if( ! isset( $user_profile ) ){ |
---|
24 | ?> |
---|
25 | <p> |
---|
26 | A VERY basic example showing how to integrate Facebook Javascript SDK side by side with HybridAuth. Click the "Sign in" link to start. |
---|
27 | </p> |
---|
28 | |
---|
29 | <h2><a href ="index.php?login=1">Sign in with facebook</a></h2> |
---|
30 | <?php |
---|
31 | } |
---|
32 | |
---|
33 | // user signed in with facebook |
---|
34 | else{ |
---|
35 | ?> |
---|
36 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
---|
37 | <html lang="en"> |
---|
38 | <head> |
---|
39 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
---|
40 | <link rel="stylesheet" href="../social_hub/public/css.css" type="text/css"> |
---|
41 | <script src="https://raw.github.com/douglascrockford/JSON-js/master/json2.js"></script> |
---|
42 | <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script> |
---|
43 | <style> |
---|
44 | pre{width:450px;overflow:auto;} |
---|
45 | </style> |
---|
46 | </head> |
---|
47 | <body> |
---|
48 | |
---|
49 | <table width="95%" border="0" cellpadding="2" cellspacing="2"> |
---|
50 | <tr> |
---|
51 | <td width="50%" valign="top"> |
---|
52 | |
---|
53 | <fieldset> |
---|
54 | <legend>Hybridauth library</legend> |
---|
55 | <b>Hi <?php echo $user_profile->displayName; ?></b>, <br />your profile url is: <?php echo $user_profile->profileURL; ?> |
---|
56 | <hr /> |
---|
57 | <b>Hybridauth access tokens for Facebook:</b> |
---|
58 | <pre><?php print_r( $adapter->getAccessToken() ); ?></pre> |
---|
59 | </fieldset> |
---|
60 | |
---|
61 | </td> |
---|
62 | <td width="50%" valign="top"> |
---|
63 | |
---|
64 | <fieldset> |
---|
65 | <legend>Facebook JavaScript SDK</legend> |
---|
66 | You are logged into this site with Facebook and the <a href='https://developers.facebook.com/docs/reference/javascript/'>Javascript SDK</a> and you should be happy about that. |
---|
67 | <br /> |
---|
68 | <br /> |
---|
69 | <span id="hellomessage"></span> |
---|
70 | <hr /> |
---|
71 | <input value="Click on me to share this page on Facebook" style="height:30px;" type="submit" onclick="share_link()" /><br /> |
---|
72 | <input value="Click on me to publish a random story your Facebook wall" style="height:30px;" type="submit" onclick="post_to_wall()" /><br /> |
---|
73 | <input value="Click on me to invite friends" style="height:30px;" type="submit" onclick="invite_friends()" /> |
---|
74 | |
---|
75 | <hr /> |
---|
76 | |
---|
77 | <br /> |
---|
78 | Note: Inviting friends <b>may require some advanced</b> Facebook application configuration your side. To know more about FB.ui visit <a href='https://developers.facebook.com/docs/reference/javascript/FB.ui/'>https://developers.facebook.com/docs/reference/javascript/FB.ui/</a> |
---|
79 | |
---|
80 | <div id="fb-root"></div> |
---|
81 | <script src="http://connect.facebook.net/en_US/all.js"></script> |
---|
82 | |
---|
83 | <script> |
---|
84 | $(function(){ |
---|
85 | FB.init({ |
---|
86 | appId:'<?php echo $adapter->config["keys"]["id"]; ?>', // or simply set your appid hard coded |
---|
87 | cookie:true, |
---|
88 | status : true, |
---|
89 | xfbml:true |
---|
90 | }); |
---|
91 | |
---|
92 | FB.getLoginStatus(function(response) { |
---|
93 | console.log( response ); |
---|
94 | $("#hellomessage").after( "<br /><hr /><b>Facebook JavaScript SDK Login Status :</b>:<pre>" + JSON.stringify( response ) + "</pre>" ); |
---|
95 | |
---|
96 | FB.api('/me', function(response) { |
---|
97 | console.log( response ); |
---|
98 | $("#hellomessage").html( "<b>Hi " + response.name + "</b>,<br />your profile url is: " + response.link ); |
---|
99 | }); |
---|
100 | }); |
---|
101 | }); |
---|
102 | |
---|
103 | // https://developers.facebook.com/docs/reference/dialogs/send/ |
---|
104 | function share_link() { |
---|
105 | FB.ui({ |
---|
106 | method: 'send', |
---|
107 | name: 'HybridAuth, open source social sign on php library', |
---|
108 | link: 'http://hybridauth.sourceforge.net/', |
---|
109 | }); |
---|
110 | } |
---|
111 | |
---|
112 | // https://developers.facebook.com/docs/reference/dialogs/requests/ |
---|
113 | function invite_friends() { |
---|
114 | FB.ui({ |
---|
115 | method: 'apprequests', message: '<?php echo $user_profile->displayName; ?> want you to join him at mywebsite.com.' |
---|
116 | }); |
---|
117 | } |
---|
118 | |
---|
119 | // https://developers.facebook.com/docs/reference/dialogs/feed/ |
---|
120 | function post_to_wall() { |
---|
121 | var obj = { |
---|
122 | method: 'feed', |
---|
123 | link: 'http://hybridauth.sourceforge.net/', |
---|
124 | picture: 'http://fbrell.com/f8.jpg', |
---|
125 | name: 'HybridAuth', |
---|
126 | caption: 'HybridAuth, open source social sign on php library', |
---|
127 | description: 'HybridAuth, open source social sign on php library.' |
---|
128 | }; |
---|
129 | |
---|
130 | function callback(response) { |
---|
131 | document.getElementById('msg').innerHTML = "Post ID: " + response['post_id']; |
---|
132 | } |
---|
133 | |
---|
134 | FB.ui(obj, callback); |
---|
135 | } |
---|
136 | |
---|
137 | function o2s (obj) { |
---|
138 | var str = ''; |
---|
139 | for (var p in obj) { |
---|
140 | if (obj.hasOwnProperty(p)) { |
---|
141 | str += p + '::' + obj[p] + '\n'; |
---|
142 | } |
---|
143 | } |
---|
144 | return str; |
---|
145 | } |
---|
146 | </script> |
---|
147 | |
---|
148 | |
---|
149 | </fieldset> |
---|
150 | |
---|
151 | </tr> |
---|
152 | </table> |
---|
153 | </body> |
---|
154 | </html> |
---|
155 | <?php |
---|
156 | } |
---|