[英]how do I call a php method in another Class in another file?
我在包含 HTML 的文件“index.php”中有 PHP 代碼。 目的是使用 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' ,很卡在這里
需要文件、活動類(具有命名空間,感謝@IncredibleHat 提供建議)並按照您的需要使用它。
require 'curl.php'; $curlclass= new \CURL\cURL; echo $curlclass->exeCurl($url,"GET");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.