Skip to content
This repository was archived by the owner on Jan 13, 2022. It is now read-only.

Commit ebde5ad

Browse files
author
Fosco Marotto
committed
Added developer site doc content, needs updating for 4.0.9
1 parent a20dc24 commit ebde5ad

13 files changed

+826
-0
lines changed

docs/FacebookCanvasLoginHelper.fbmd

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<card>
2+
# FacebookCanvasLoginHelper for the Facebook SDK for PHP
3+
4+
A helper class for getting a FacebookSession in a Canvas app
5+
</card>
6+
7+
<card>
8+
## Facebook\FacebookCanvasLoginHelper {#overview}
9+
10+
If your app is loaded through Canvas, Facebook sends a POST request with a signed request. This helper class will handle processing and validating that information with Facebook, and returns a `FacebookSession`.
11+
12+
Usage:
13+
14+
~~~~
15+
16+
$helper = new FacebookCanvasLoginHelper();
17+
try {
18+
$session = $helper->getSession();
19+
} catch (FacebookRequestException $ex) {
20+
// When Facebook returns an error
21+
} catch (\Exception $ex) {
22+
// When validation fails or other local issues
23+
}
24+
if ($session) {
25+
// Logged in.
26+
}
27+
28+
~~~~
29+
</card>
30+
31+
<card>
32+
## Instance Methods {#instance-methods}
33+
34+
### getSession {#getsession}
35+
`getSession()`
36+
Processes the POST request from Facebook, if present. Returns a `FacebookSession` or `null`.
37+
</card>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<card>
2+
# FacebookJavaScriptLoginHelper for the Facebook SDK for PHP
3+
4+
A helper class for getting a FacebookSession using the session from the Facebook SDK for JavaScript.
5+
</card>
6+
7+
<card>
8+
## Facebook\FacebookJavaScriptLoginHelper {#overview}
9+
10+
If your web app uses the Facebook SDK for JavaScript, you can access that in your PHP code as well. This helper class will process and validate the cookie data used by the Facebook SDK for JavaScript, returning a `FacebookSession` on success.
11+
12+
Usage:
13+
14+
~~~~
15+
16+
$helper = new FacebookJavaScriptLoginHelper();
17+
try {
18+
$session = $helper->getSession();
19+
} catch(FacebookRequestException $ex) {
20+
// When Facebook returns an error
21+
} catch(\Exception $ex) {
22+
// When validation fails or other local issues
23+
}
24+
if ($session) {
25+
// Logged in.
26+
}
27+
28+
~~~~
29+
30+
It's important to note that on first access, or if a session has since expired, these methods will operate on data that is one request-cycle stale. You will likely want to make an Ajax request when the login state changes in the Facebook SDK for JavaScript. Information about that here: (FB.event.subscribe)[https://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/#events]
31+
</card>
32+
33+
<card>
34+
## Instance Methods {#instance-methods}
35+
36+
### getSession {#getsession}
37+
`getSession()`
38+
Processes the data available from the Facebook SDK for JavaScript, if present. Returns a `FacebookSession` or `null`.
39+
</card>

docs/FacebookRedirectLoginHelper.fbmd

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<card>
2+
# FacebookRedirectLoginHelper for the Facebook SDK for PHP
3+
4+
A helper class for getting a FacebookSession using the OAuth protocol.
5+
</card>
6+
7+
<card>
8+
## Facebook\FacebookRedirectLoginHelper {#overview}
9+
10+
If your web app uses Facebook Login on the back-end, you'll need to redirect your visitors to a URL at Facebook to initiate a login request. Facebook then redirects the user to your apps callback URL, providing session data. This helper class will generate the login URL for you, and can process and validate the data from Facebook, returning a `FacebookSession` on success.
11+
12+
This class can be extended, and the `storeState($state)` and `loadState()` methods overridden, to store the state check using another method besides the default `$_SESSION`.
13+
14+
Usage:
15+
16+
~~~~
17+
18+
$helper = new FacebookRedirectLoginHelper($redirect_url, $appId = NULL, $appSecret = NULL);
19+
echo '<a href="' . $helper->getLoginUrl() . '">Login with Facebook</a>';
20+
21+
~~~~
22+
23+
Then, in your callback page (at the redirect url) when Facebook sends the user back:
24+
25+
~~~~
26+
27+
$helper = new FacebookRedirectLoginHelper($redirect_url);
28+
try {
29+
$session = $helper->getSessionFromRedirect();
30+
} catch(FacebookRequestException $ex) {
31+
// When Facebook returns an error
32+
} catch(\Exception $ex) {
33+
// When validation fails or other local issues
34+
}
35+
if ($session) {
36+
// Logged in.
37+
}
38+
39+
~~~~
40+
</card>
41+
42+
<card>
43+
## Instance Methods {#instance-methods}
44+
45+
### getLoginUrl {#getloginurl}
46+
`getLoginUrl()`
47+
Generates the URL to redirect a web visitor to Facebook to login to your app.
48+
### getLogoutUrl {#getlogouturl}
49+
`getLogoutUrl($next_url)`
50+
Generates the URL to redirect a web visitor to Facebook to logout, with a url to redirect to after.
51+
### getSessionFromRedirect {#getsessionfromredirect}
52+
`getSessionFromRedirect()`
53+
Processes the redirect data from Facebook, if present. Returns a `FacebookSession` or `null`.
54+
</card>

docs/FacebookRequest.fbmd

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<card>
2+
# FacebookRequest for the Facebook SDK for PHP
3+
4+
Represents a request that will be made against the Graph API.
5+
</card>
6+
7+
<card>
8+
## Facebook\FacebookRequest {#overview}
9+
10+
Constructor:
11+
12+
~~~~
13+
$request = new FacebookRequest(
14+
FacebookSession $session,
15+
string $httpMethod,
16+
string $path,
17+
array $params = NULL,
18+
string $version = NULL
19+
);
20+
~~~~
21+
22+
Usage:
23+
24+
~~~~
25+
// Make a new request and execute it.
26+
try {
27+
$response = (new FacebookRequest($session, 'GET', '/me'))->execute();
28+
$object = $response->getGraphObject();
29+
echo $object->getProperty('name');
30+
} catch (FacebookRequestException $ex) {
31+
echo $ex->getMessage();
32+
} catch (\Exception $ex) {
33+
echo $ex->getMessage();
34+
}
35+
36+
// You can chain methods together and get a strongly typed GraphUser
37+
$me = (new FacebookRequest(
38+
$session, 'GET', '/me'
39+
))->execute()->getGraphObject(GraphUser::className);
40+
echo $me->getName();
41+
~~~~
42+
</card>
43+
44+
<card>
45+
## Instance Methods {#instance-methods}
46+
47+
### execute {#execute}
48+
`execute()`
49+
Returns a `Facebook\FacebookResponse` from this request, from which a strongly-typed result can be retrieved. Throws an exception if the request fails. If the error is returned from Facebook, as opposed to a networking issue, a `Facebook\FacebookRequestException` is thrown.
50+
### getPath {#getpath}
51+
`getPath()`
52+
Returns a copy of the path for the request, not including the version.
53+
### getParameters {#getparams}
54+
`getParameters()`
55+
Returns a copy of the parameters array for the request.
56+
### getSession {#getsession}
57+
`getSession()`
58+
Returns the `Facebook\FacebookSession` object associated with this request.
59+
</card>

docs/FacebookRequestException.fbmd

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<card>
2+
# FacebookRequestException for the Facebook SDK for PHP
3+
4+
Represents an exception thrown by executing a Facebook request.
5+
</card>
6+
7+
<card>
8+
## Facebook\FacebookRequestException {#overview}
9+
10+
This base class has several subclasses:
11+
12+
`FacebookAuthorizationException`
13+
`FacebookClientException`
14+
`FacebookPermissionException`
15+
`FacebookServerException`
16+
`FacebookThrottleException`
17+
`FacebookOtherException`
18+
19+
Whenever a FacebookRequestException is thrown, it will be one of these types.
20+
They are derived from the error information here: https://developers.facebook.com/docs/graph-api/using-graph-api/#errors
21+
</card>
22+
23+
<card>
24+
## Instance Methods {#instance-methods}
25+
26+
`FacebookRequestException` extends from the base `\Exception` class, so `getCode()` and `getMessage()` are available by default.
27+
28+
### getHttpStatusCode {#gethttpstatus}
29+
`getHttpStatusCode()`
30+
Returns the HTTP status code returned with this exception.
31+
### getSubErrorCode {#getsuberrorcode}
32+
`getSubErrorCode()`
33+
Returns the numeric sub-error code returned from Facebook.
34+
### getErrorType {#geterrortype}
35+
`getErrorType()`
36+
Returns the type of error as a string.
37+
### getResponse {#getresponse}
38+
`getResponse()`
39+
Returns the decoded response used to create the exception.
40+
### getRawResponse {#getrawresponse}
41+
`getRawResponse()`
42+
Returns the raw response used to create the exception.
43+
</card>

docs/FacebookResponse.fbmd

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<card>
2+
# FacebookResponse for the Facebook SDK for PHP
3+
4+
Represents a response from the Graph API.
5+
</card>
6+
7+
<card>
8+
## Facebook\FacebookResponse {#overview}
9+
10+
Usage:
11+
12+
~~~~
13+
// A FacebookResponse is returned from an executed FacebookRequest
14+
try {
15+
$response = (new FacebookRequest($session, 'GET', '/me'))->execute();
16+
// You can get the request back:
17+
$request = $response->getRequest();
18+
// You can get the response as a GraphObject:
19+
$object = $response->getGraphObject();
20+
// You can get the response as a subclass of GraphObject:
21+
$me = $response->getGraphObject(GraphUser::className());
22+
// If this response has multiple pages, you can get a request for the next or previous pages:
23+
$nextPageRequest = $response->getRequestForNextPage();
24+
$previousPageRequest = $response->getRequestForPreviousPage();
25+
} catch (FacebookRequestException $ex) {
26+
echo $ex->getMessage();
27+
} catch (\Exception $ex) {
28+
echo $ex->getMessage();
29+
}
30+
31+
// You can also chain the methods together:
32+
$me = (new FacebookRequest(
33+
$session, 'GET', '/me'
34+
))->execute()->getGraphObject(GraphUser::className);
35+
echo $me->getName();
36+
~~~~
37+
</card>
38+
39+
<card>
40+
## Instance Methods {#instance-methods}
41+
42+
### getGraphObject {#getgraphobject}
43+
`getGraphObject(string $type = 'Facebook\GraphObject')`
44+
Returns the result as a `GraphObject`. If specified, a strongly-typed subclass of `GraphObject` is returned.
45+
### getGraphObjectList {#getgraphobjectlist}
46+
`getGraphObjectList(string $type = 'Facebook\GraphObject')`
47+
Returns an array of `GraphObject` returned by this request. If specified, a strongly-typed subclass of `GraphObject` is returned.
48+
### getRequest {#getrequest}
49+
`getRequest()`
50+
Returns the `FacebookRequest` that produced this response.
51+
### getRequestForNextPage {#getnextpage}
52+
`getRequestForNextPage()`
53+
If the response has paginated data, produces a `FacebookRequest` for the next pge of data.
54+
### getRequestForPreviousPage {#getpreviouspage}
55+
`getRequestForPreviousPage()`
56+
If the response has paginated data, produces a `FacebookRequest` for the previous page of data.
57+
</card>

docs/FacebookSession.fbmd

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<card>
2+
# FacebookSession for the Facebook SDK for PHP
3+
4+
Represents a Facebook Session, which is used when making requests to the Graph API.
5+
</card>
6+
7+
<card>
8+
## Facebook\FacebookSession {#overview}
9+
10+
Usage:
11+
12+
~~~~
13+
use Facebook\FacebookSession;
14+
15+
FacebookSession::setDefaultApplication('app-id', 'app-secret');
16+
17+
// If you already have a valid access token:
18+
$session = new FacebookSession('access-token');
19+
20+
// If you're making app-level requests:
21+
$session = FacebookSession::newAppSession();
22+
23+
// To validate the session:
24+
try {
25+
$session->validate();
26+
} catch (FacebookRequestException $ex) {
27+
// Session not valid, Graph API returned an exception with the reason.
28+
echo $ex->getMessage();
29+
} catch (\Exception $ex) {
30+
// Graph API returned info, but it may mismatch the current app or have expired.
31+
echo $ex->getMessage();
32+
}
33+
~~~~
34+
</card>
35+
36+
<card>
37+
## Static Methods {#static-methods}
38+
39+
### setDefaultApplication {#setdefaultapp}
40+
`setDefaultApplication(string $appId, string $appSecret)`
41+
Configures and app ID and secret that will be used by default throughout the SDK (but can be overridden whenever necessary using parameters to other methods.
42+
### validate {#validate}
43+
`validate(Facebook\GraphSessionInfo $sessionInfo, string $appId = NULL, string $appSecret = NULL)`
44+
Ensures that the provided GraphSessionInfo is valid, throwing an exception if not. It does this by ensuring the app ID in the token info matches the given (or default) app ID, ensuring the token itself is valid, and ensuring that the expiration time has not passed.
45+
### newAppSession {#newappsession}
46+
`newAppSession(string $appId = NULL, string $appSecret = NULL)`
47+
Returns a `Facebook\FacebookSession` configured with a token for the app which can be used for publishing and for requesting app-level information.
48+
### newSessionFromSignedRequest {#newsessionfromsr}
49+
`newSessionFromSignedRequest(string $signedRequest)`
50+
Returns a `Facebook\FacebookSession` for the given signed request.
51+
</card>
52+
53+
<card>
54+
## Instance Methods {#instance-methods}
55+
56+
### getToken {#gettoken}
57+
`getToken()`
58+
Returns the token string for the session.
59+
### getSessionInfo {#getsessioninfo}
60+
`getSessionInfo(string $appId = NULL, string $appSecret = NULL)`
61+
Equivalent to calling the /debug_token endpoint of the Graph API to get the details for the access token for this session. Returns a `Facebook\GraphSessionInfo` object.
62+
### getLongLivedSession {#getlonglivedsession}
63+
`getLongLivedSession(string $appId = NULL, string $appSecret = NULL)`
64+
Returns a new `Facebook\FacebookSession` resulting from extending a short-lived access token. This method will make a network request. If you know you already have a long-lived session, you do not need to call this. The only time you get a short-lived session as of March 2014 is from the Facebook SDK for JavaScript. If this session is not short-lived, this method will return `$this`. A long-lived session is on the order of months. A short-lived session is on the order of hours. You can figure out whether a session is short-lived or long-lived by checking the expiration date in the session info, but it's not a precise thing.
65+
### getExchangeToken {#getexchangetoken}
66+
`getExchangeToken(string $appId = NULL, string $appSecret = NULL)`
67+
Returns an exchange token string which can be sent back to clients and exchanged for a device-linked access token. You need this when your user did not log in on a particular device, but you want to be able to make Graph API calls from that device as this user.
68+
### validate {#validatei}
69+
`validate(string $appId = NULL, string $appSecret = NULL)`
70+
Ensures that a session is valid, throwing an exception if not. It does this by fetching the token info, ensuring the app ID in the token info matches the given (or default) app ID, ensuring the token itself is valid, and ensuring that the expiration time has not passed.
71+
</card>

0 commit comments

Comments
 (0)