У меня есть PHP код в файле 'index. php', который включает HTML. Цель состоит в том, чтобы пройти аутентификацию с помощью Google Directory API. Это настройка кода:
<?php
// Admin Google API settings
// Portal url:
define("CALLBACK_URL", "http://localhost/los-api/google/index.php"); //Callback URL
define("AUTH_URL", "https://accounts.google.com/o/oauth2/v2/auth"); //Used to get CODE (not Token!)
define("CLIENT_ID", "***.apps.googleusercontent.com"); // Personal
define("CLIENT_SECRET", "***"); // Personal
define("SCOPE", "https://www.googleapis.com/auth/admin.directory.device.chromeos
https://www.googleapis.com/auth/admin.directory.user
https://www.googleapis.com/auth/admin.directory.orgunit"); // Depends on what you want to do.
define("APIURL_DIRECTORY","https://www.googleapis.com/admin/directory/v1/customer/"); // For Google
Directory actions
define("CUSTOMER_ID","***"); // Personal, see: ....? voorbeeld
define("TOKEN_URL","https://oauth2.googleapis.com/token"); // URL to get Token (not code).
// Initiate code for access token
if(isset($_GET["code"])){
//DEBUG: echo "Code: ".$_GET["code"];
$url = TOKEN_URL."?";
$url .= "code=".$_GET["code"];
$url .= "&grant_type=authorization_code";
$url .= "&client_id=". urlencode(CLIENT_ID);
$url .= "&redirect_uri=". urlencode(CALLBACK_URL);
$url .= "&client_secret=". urlencode(CLIENT_SECRET);
$response = json_decode(exeCurl($url,"POST"), true);
if(isset($response)){
if(array_key_exists("access_token", $response)) {
$access_token = $response;
setcookie("LOStoken", $response['access_token'], time() + (86400 * 30), "/"); // 86400 = 1 day
}
}
} else {
if(isset($_POST['gettoken'])){
$url = AUTH_URL."?";
$url .= "response_type=code";
$url .= "&client_id=". urlencode(CLIENT_ID);
$url .= "&scope=". urlencode(SCOPE);
$url .= "&redirect_uri=". urlencode(CALLBACK_URL);
echo exeCurl ($ url, "GET"); // здесь я хочу выполнить функцию 'exeCurl', которая существует в файле 'curl. php' в той же папке?>
curl.php
<?php
namespace CURL;
class cURL
{
// general curl method
function exeCurl($url,$method,$body="") {
$curl = curl_init(); // initiate curl
if(isset($_COOKIE["LOStoken"])){
$headers = array(
"Accept: */*",
"Accept-Encoding: gzip, deflate",
"Authorization: Bearer ". $_COOKIE["LOStoken"],
"Connection: keep-alive",
"Content-Length: ". strlen($body),
"Content-Type: application/json",
"cache-control: no-cache"
);
} else {
$headers = array(
"Accept: */*",
"Accept-Encoding: gzip, deflate",
"Cache-Control: no-cache",
"Content-Length: ". strlen($body),
"Connection: keep-alive",
"cache-control: no-cache"
);
}
// Set parameters for curl
$params = array(
CURLOPT_URL => $url, // API URL
CURLOPT_RETURNTRANSFER => true, // Return answer
CURLOPT_SSL_VERIFYPEER => false, // SSL, enable in production
//CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10, // Max redirect
CURLOPT_FOLLOWLOCATION => true, // If 301, follow redirect
CURLOPT_TIMEOUT => 30, // Max timeout
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, // HTTP version used
CURLOPT_CUSTOMREQUEST => $method, // HTTP method used
CURLOPT_HTTPHEADER => $headers); // HTTP headers
// Combine
curl_setopt_array($curl, $params);
// Curl ans
$response = curl_exec($curl);
$err = curl_error($curl); // fill with errors
curl_close($curl); // close
if ($err) {
echo "cURL Error #:" . $err; // if errors
}
if(array_key_exists("error", $response)) echo $response["error_description"];
return $response; // return ans
}
}
Как мне достичь этого? Через наследство? Или импортировать 'curl. php', застрявший здесь