-
Notifications
You must be signed in to change notification settings - Fork 376
Expand file tree
/
Copy pathfunctions.php
More file actions
104 lines (90 loc) · 3.17 KB
/
functions.php
File metadata and controls
104 lines (90 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
/**
* Make a a curl call to an endpoint with params
*
* @param string $endpoint we are hitting
* @param string $type of request
* @param array $params to send along with the request
*
* @return array with the api response
*/
function makeApiCall( $endpoint, $type, $params ) {
// initialize curl
$ch = curl_init();
// create endpoint with params
$apiEndpoint = $endpoint . '?' . http_build_query( $params );
// set other curl options
curl_setopt( $ch, CURLOPT_URL, $apiEndpoint );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
// get response
$response = curl_exec( $ch );
// close curl
curl_close( $ch );
return array( // return data
'type' => $type,
'endpoint' => $endpoint,
'params' => $params,
'api_endpoint' => $apiEndpoint,
'data' => json_decode( $response, true )
);
}
/**
* Get facebook api login url that will take the user to facebook and present them with login dialog.
*
* Endpoint: https://www.facebook.com/{fb-graph-api-version}/dialog/oauth?client_id={app-id}&redirect_uri={redirect-uri}&state={state}&scope={scope}&auth_type={auth-type}
*
* @param string $scope comma separated list of permissions being requested from the user..
* @param string $state random generated to verify request is from facebook.
* @return string
*/
function getFacebookLoginUrl( $permissions, $state ) {
// endpoint for facebook login dialog
$endpoint = 'https://www.facebook.com/' . FB_GRAPH_VERSION . '/dialog/oauth';
$params = array( // login url params required to direct user to facebook and promt them with a login dialog
'client_id' => FB_APP_ID,
'redirect_uri' => FB_REDIRECT_URI,
'state' => $state,
'scope' => $permissions,
'auth_type' => 'rerequest'
);
// return login url
return $endpoint . '?' . http_build_query( $params );
}
/**
* Get an access token with the code from facebook.
*
* Endpoint https://graph.facebook.com/{fb-graph-version}/oauth/access_token?client_id{app-id}&client_secret={app-secret}&redirect_uri={redirect_uri}&code={code}
*
* @param string $code code returned from facebook, exchange for access token
* @return array
*/
function getAccessTokenWithCode( $code ) {
// endpoint for getting an access token with code
$endpoint = FB_GRAPH_DOMAIN . FB_GRAPH_VERSION . '/oauth/access_token';
$params = array( // params for the endpoint
'client_id' => FB_APP_ID,
'client_secret' => FB_APP_SECRET,
'redirect_uri' => FB_REDIRECT_URI,
'code' => $code
);
// make the api call
return makeApiCall( $endpoint, 'GET', $params );
}
/**
* Get an access token with the code from facebook.
*
* Endpoint https://graph.facebook.com/{fb-graph-version}/{endpoint-path}
*
* @param array $params Params for fb endpoint.
* @return array
*/
function getFacebookUserInfo( $params ) {
// endpoint for getting an access token with code
$endpoint = FB_GRAPH_DOMAIN . FB_GRAPH_VERSION . '/' . $params['endpoint_path'];
$endpointParams = array( // params for the endpoint
'fields' => $params['fields'],
'access_token' => $params['access_token']
);
// make the api call
return makeApiCall( $endpoint, $params['request_type'], $endpointParams );
}