Skip to content

Commit 4fa194f

Browse files
committed
Clean up examples
1 parent 1ab65aa commit 4fa194f

6 files changed

+78
-125
lines changed

examples/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Hybridauth 3 Examples
33

44
File | Description
55
-------------- | ------------------------------------------------------------------------------
6-
example_01.php | This simple example illustrate how to authenticate users with GitHub. If you're new to Hybridauth, this file is the one you'll like want to check.
6+
example_01.php | This simple example illustrate how to authenticate users with GitHub. If you're new to Hybridauth, this file is the one you'll likely want to check.
77
example_02.php | Details how to use users in a similar fashion to Hybridauth 2. Note that while Hybridauth 3 provides a similar interface to Hybridauth 2, both versions are not fully compatible with each other.
88
example_03.php | An example on how use Access Tokens to access providers APIs, and how to setup custom API endpoints.
99
example_04.php | A simple example that shows how to connect users to providers using OpenID.

examples/example_01.php

+39-45
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,17 @@
1818
*
1919
* If you're already familiar with the process, you can skip the explanation below.
2020
*
21-
* To get started with GitHub authentication, you need to create a new GitHub
22-
* application.
21+
* To get started with GitHub authentication, you need to create a new GitHub application.
2322
*
2423
* First, navigate to https://github.com/settings/developers then click the Register
2524
* new application button at the top right of that page and fill in any required fields
2625
* such as the application name, description and website.
2726
*
2827
* Set the Authorization callback URL to https://path/to/hybridauth/examples/example_01.php.
29-
* Understandably, you need to replace 'path/to/hybridauth' with the real path to this
30-
* script.
28+
* Understandably, you need to replace 'path/to/hybridauth' with the real path to this script.
3129
*
32-
* Note that Hybridauth provides an utility function that can generate the current page url for
33-
* you and can be used for the callback. Exemple: 'callback' => Hybridauth\HttpClient\Util::getCurrentUrl()
30+
* Note that Hybridauth provides an utility function that can generate the current page url for you
31+
* and can be used for the callback. Example: 'callback' => Hybridauth\HttpClient\Util::getCurrentUrl()
3432
*
3533
* After configuring your GitHub application, simple replace 'your-app-id' and 'your-app-secret'
3634
* with your application credentials (Client ID and Client Secret).
@@ -46,38 +44,29 @@
4644
$config = [
4745
'callback' => 'https://path/to/hybridauth/examples/example_01.php', // or Hybridauth\HttpClient\Util::getCurrentUrl()
4846

49-
'keys' => ['id' => 'your-app-id', 'secret' => 'your-app-secret'],
47+
'keys' => [ 'id' => 'your-app-id', 'secret' => 'your-app-secret' ], // Your Github application credentials
5048

51-
'scope' => 'user:email'
49+
/* optional : set scope
50+
'scope' => 'user:email', */
5251

5352
/* optional : set debug mode
54-
// You can also set it to
55-
// - false To disable logging
56-
// - true To enable logging
57-
// - 'error' To log only error messages. Useful in production
58-
// - 'info' To log info and error messages (ignore debug messages]
5953
'debug_mode' => true,
60-
// 'debug_mode' => 'info',
61-
// 'debug_mode' => 'error',
62-
// Path to file writable by the web server. Required if 'debug_mode' is not false
54+
// Path to file writeable by the web server. Required if 'debug_mode' is not false
6355
'debug_file' => __FILE__ . '.log', */
6456

6557
/* optional : customize Curl settings
6658
// for more information on curl, refer to: http://www.php.net/manual/fr/function.curl-setopt.php
6759
'curl_options' => [
6860
// setting custom certificates
69-
// http://curl.haxx.se/docs/caextract.html
7061
CURLOPT_SSL_VERIFYPEER => true,
7162
CURLOPT_CAINFO => '/path/to/your/certificate.crt',
7263
73-
// setting proxies
74-
# CURLOPT_PROXY => '*.*.*.*:*',
64+
// set a valid proxy ip address
65+
CURLOPT_PROXY => '*.*.*.*:*',
7566
76-
// custom user agent
77-
# CURLOPT_USERAGENT => '',
78-
79-
// etc..
80-
], */
67+
// set a custom user agent
68+
CURLOPT_USERAGENT => ''
69+
] */
8170
];
8271

8372
/**
@@ -104,7 +93,9 @@
10493
/**
10594
* Step 5: Retrieve Users Profiles
10695
*
107-
*
96+
* Calling getUserProfile returns an instance of class Hybridauth\User\Profile which contain the
97+
* connected user's profile in simple and standardized structure across all the social APIs supported
98+
* by HybridAuth.
10899
*/
109100

110101
$userProfile = $github->getUserProfile();
@@ -120,10 +111,10 @@
120111
* As an example we list the authenticated user's public gists.
121112
*/
122113

123-
$apiResponse = $github->apiRequest('gists/public');
114+
$apiResponse = $github->apiRequest('gists');
124115

125116
/**
126-
* Step 6: Disconnecting from the Provider API
117+
* Step 6: Disconnect the adapter
127118
*
128119
* This will erase the current user authentication data from session, and any further
129120
* attempt to communicate with Github API will result on an authorisation exception.
@@ -143,41 +134,44 @@
143134
* has erased the oauth access token used to sign http requests from the current
144135
* session, thus, any new request we now make will now throw an exception.
145136
*
146-
* It's also important that you don't show Hybridauth exception's messages to the
147-
* user as they may include sensitive data, and that you use your own error messages
148-
* instead.
137+
* It's important that you don't show Hybridauth exception's messages to the end user as
138+
* they may include sensitive data, and that you use your own error messages instead.
149139
*/
150140

151141
try {
152142
$github->getUserProfile();
153143
}
154144

155145
/**
156-
* Catch Curl Errors
157-
*
158-
* This kind of error may happen when:
159-
* - Internet or Network issues.
160-
* - Your server configuration is not setup correctly.
161-
* The full list of curl errors that may happen can be found at http://curl.haxx.se/libcurl/c/libcurl-errors.html
162-
*/
146+
* Catch Curl Errors
147+
*
148+
* This kind of error may happen in case of:
149+
* - Internet or Network issues.
150+
* - Your server configuration is not setup correctly.
151+
*
152+
* The full list of curl errors that may happen can be found at http://curl.haxx.se/libcurl/c/libcurl-errors.html
153+
*/
154+
163155
catch (Hybridauth\Exception\HttpClientFailureException $e) {
164156
echo 'Curl text error message : '.$github->getHttpClient()->getResponseClientError();
165157
}
166158

167159
/**
168-
* Catch API Requests Errors
169-
*
170-
* This usually happens when requesting a:
171-
* - Wrong URI or a mal-formatted http request.
172-
* - Protected resource without providing a valid access token.
173-
*/
160+
* Catch API Requests Errors
161+
*
162+
* This usually happens when requesting a:
163+
* - Wrong URI or a mal-formatted http request.
164+
* - Protected resource without providing a valid access token.
165+
*/
166+
174167
catch (Hybridauth\Exception\HttpRequestFailedException $e) {
175168
echo 'Raw API Response: '.$github->getHttpClient()->getResponseBody();
176169
}
177170

178171
/**
179-
* I catch everything else
180-
*/
172+
* Base PHP's exception that catches everything [else]
173+
*/
174+
181175
catch (\Exception $e) {
182176
echo 'Oops! We ran into an unknown issue: '.$e->getMessage();
183177
}

examples/example_02.php

+18-46
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
/*!
3-
* Details how to use users in a similar fashion to Hybridauth 2. Note that while Hybridauth 3 provides a similar interface to Hybridauth 2, both versions are not fully compatible with each other.
3+
* Details how to use users in a similar fashion to Hybridauth 2. Note that while Hybridauth 3 provides
4+
* a similar interface to Hybridauth 2, both versions are not fully compatible with each other.
45
*/
56

67
include 'vendor/autoload.php';
@@ -9,13 +10,9 @@
910
use Hybridauth\HttpClient;
1011

1112
$config = [
12-
'base_url' => HttpClient\Util::getCurrentUrl(),
13+
'callback' => HttpClient\Util::getCurrentUrl(),
1314

1415
'providers' => [
15-
'OpenID' => [
16-
'enabled' => true
17-
],
18-
1916
'GitHub' => [
2017
'enabled' => true,
2118
'keys' => [ 'id' => '', 'secret' => '' ],
@@ -34,71 +31,46 @@
3431
'Twitter' => [
3532
'enabled' => true,
3633
'keys' => [ 'key' => '', 'secret' => '' ],
37-
],
38-
39-
'Reddit' => [
40-
'enabled' => true,
41-
'keys' => [ 'id' => '', 'secret' => '' ],
42-
],
34+
]
4335
],
4436

45-
/* optional : set debgu mode
46-
// You can also set it to
47-
// - false To disable logging
48-
// - true To enable logging
49-
// - 'error' To log only error messages. Useful in production
50-
// - 'info' To log info and error messages (ignore debug messages]
37+
/* optional : set debug mode
5138
'debug_mode' => true,
52-
// 'debug_mode' => 'info',
53-
// 'debug_mode' => 'error',
54-
// Path to file writable by the web server. Required if 'debug_mode' is not false
39+
// Path to file writeable by the web server. Required if 'debug_mode' is not false
5540
'debug_file' => __FILE__ . '.log', */
5641

5742
/* optional : customize Curl settings
5843
// for more information on curl, refer to: http://www.php.net/manual/fr/function.curl-setopt.php
5944
'curl_options' => [
6045
// setting custom certificates
61-
// http://curl.haxx.se/docs/caextract.html
6246
CURLOPT_SSL_VERIFYPEER => true,
6347
CURLOPT_CAINFO => '/path/to/your/certificate.crt',
6448
65-
// setting proxies
66-
# CURLOPT_PROXY => '*.*.*.*:*',
67-
68-
// custom user agent
69-
# CURLOPT_USERAGENT => '',
49+
// set a valid proxy ip address
50+
CURLOPT_PROXY => '*.*.*.*:*',
7051
71-
// etc..
72-
], */
52+
// set a custom user agent
53+
CURLOPT_USERAGENT => ''
54+
] */
7355
];
7456

75-
$hybridauth = new Hybridauth( $config );
57+
try {
58+
$hybridauth = new Hybridauth( $config );
7659

77-
try {
7860
$adapter = $hybridauth->authenticate( 'GitHub' );
7961

8062
// $adapter = $hybridauth->authenticate( 'Google' );
81-
// $adapter = $hybridauth->authenticate( 'Twitter' );
8263
// $adapter = $hybridauth->authenticate( 'Facebook' );
8364
// $adapter = $hybridauth->authenticate( 'Twitter' );
84-
// $adapter = $hybridauth->authenticate( 'Foursquare' );
85-
// $adapter = $hybridauth->authenticate( 'Disqus' );
86-
// $adapter = $hybridauth->authenticate( 'Reddit' );
87-
// $adapter = $hybridauth->authenticate( 'WordPress' );
88-
// $adapter = $hybridauth->authenticate( 'Steam' );
89-
// etc.
9065

91-
$userProfile = $adapter->getUserProfile();
9266
$tokens = $adapter->getAccessToken();
67+
$userProfile = $adapter->getUserProfile();
9368

94-
echo '<pre>';
69+
// print_r( $tokens );
70+
// print_r( $userProfile );
9571

96-
print_r( $userProfile );
97-
print_r( $tokens );
98-
print_r( $_SESSION );
72+
$adapter->disconnect();
9973
}
100-
catch( Exception $e ){
74+
catch (\Exception $e) {
10175
echo $e->getMessage();
10276
}
103-
104-
$adapter->disconnect();

examples/example_03.php

+8-15
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,26 @@
88
$config = [
99
'callback' => Hybridauth\HttpClient\Util::getCurrentUrl(),
1010

11-
'keys' => [ 'id' => '', 'secret' => '' ],
12-
13-
'tokens' => [
14-
'access_token' => 'your-facebook-access-token'
15-
],
11+
'keys' => [ 'id' => 'your-facebook-app-id', 'secret' => 'your-facebook-app-secret' ],
1612

1713
'endpoints' => [
18-
'api_base_url' => 'https://graph.facebook.com/v2.2/',
14+
'api_base_url' => 'https://graph.facebook.com/v2.8/',
1915
'authorize_url' => 'https://www.facebook.com/dialog/oauth',
2016
'access_token_url' => 'https://graph.facebook.com/oauth/access_token',
2117
]
2218
];
2319

24-
$adapter = new Hybridauth\Provider\Facebook( $config );
25-
2620
try {
21+
$adapter = new Hybridauth\Provider\Facebook( $config );
22+
23+
$adapter->setAccessToken(['access_token' => 'user-facebook-access-token']);
24+
2725
$userProfile = $adapter->getUserProfile();
28-
$tokens = $adapter->getAccessToken();
2926

30-
echo '<pre>';
27+
// print_r( $userProfile );
3128

32-
print_r( $userProfile );
33-
print_r( $tokens );
34-
print_r( $_SESSION );
29+
$adapter->disconnect();
3530
}
3631
catch( Exception $e ){
3732
echo $e->getMessage();
3833
}
39-
40-
$adapter->disconnect();

examples/example_04.php

+6-9
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,19 @@
1414
// etc.
1515
];
1616

17-
$adapter = new Hybridauth\Provider\OpenID( $config );
18-
1917
try {
18+
$adapter = new Hybridauth\Provider\OpenID( $config );
19+
2020
$adapter->authenticate();
2121

22-
$userProfile = $adapter->getUserProfile();
2322
$tokens = $adapter->getAccessToken();
23+
$userProfile = $adapter->getUserProfile();
2424

25-
echo '<pre>';
25+
// print_r( $tokens );
26+
// print_r( $userProfile );
2627

27-
print_r( $userProfile );
28-
print_r( $tokens );
29-
print_r( $_SESSION );
28+
$adapter->disconnect();
3029
}
3130
catch( Exception $e ){
3231
echo $e->getMessage();
3332
}
34-
35-
$adapter->disconnect();

examples/example_05.php

+6-9
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,19 @@
1515
// 'verify' => true, # Set to false to disable SSL certificate verification
1616
]);
1717

18-
$adapter = new Hybridauth\Provider\Github( $config, $guzzle );
19-
2018
try {
19+
$adapter = new Hybridauth\Provider\Github($config, $guzzle);
20+
2121
$adapter->authenticate();
2222

23-
$userProfile = $adapter->getUserProfile();
2423
$tokens = $adapter->getAccessToken();
24+
$userProfile = $adapter->getUserProfile();
2525

26-
echo '<pre>';
26+
// print_r( $tokens );
27+
// print_r( $userProfile );
2728

28-
print_r( $userProfile );
29-
print_r( $tokens );
30-
print_r( $_SESSION );
29+
$adapter->disconnect();
3130
}
3231
catch( Exception $e ){
3332
echo $e->getMessage();
3433
}
35-
36-
$adapter->disconnect();

0 commit comments

Comments
 (0)