我有两台服务器,服务器1(主机)是向服务器2(客户端)提供内容。
在服务器2上我使用
file_get_contents('http://domain1.com/shared/portal.php?file=testfile');
检索所请求的文件内容。但在此内容可以在服务器2上检索之前,我想实现某种身份验证(用户名/密码)。我正在考虑在fgc的url中包含一个用户名和密码查询,并在内容发送回来之前检查服务器1上的db,但在某些情况下,fgc内容需要通过循环打印多次,所以如果fgc被调用10次,我不想在服务器1上打db 10次。
所以基本上我希望能够在服务器1上调用一个授权文件或类似的东西,这将访问fgc获取的内容。也许是JSON之类的?但是我从来没有在php中使用过JSON,所以我不确定这将如何工作。
任何帮助都将非常感激。提前谢谢。
更新到更具体的显然:我想要一些关于我可以使用的一些方法的指针,这些方法将赋予服务器2授权从服务器1检索所请求的文件。如果没有通过用户名/密码或访问密钥授予授权,那么使用fgc请求的文件内容将返回空或拒绝消息。
所以现在我在每个服务器上的设置是这样的,如果可能的话,我想在检索fgc内容之前执行一次授权尝试。
(Server 1 Portal.php)
<link rel="stylesheet" type="text/css" href="http://domain1.com/shared/css/style.css">
<?php
$fileName = $_GET["file"];
/* allow authorization through access key or username/password in this file or separate file preferably */
if ($fileName) {
include ($_SERVER["DOCUMENT_ROOT"]."/shared/content/".$fileName.".php");
} else {
echo "Missing file name.";
}
?>
<script src="http://domain1.com/shared/js/functions.js"></script>
在服务器2上,我有这样的内容
(Server 2 index.php)
<?php
/* request authorization from server 1 to ensure content is allowed to be sent */
// once authorization completes get content from server 1
$test = file_get_contents('http://domain1.com/shared/portal.php?file=testfile');
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<?php echo $test; ?>
</body>
</html>
您可以使用stream_context_create
(或CURL)来发出请求。但是服务器1 必须实现认证过程以允许从服务器2下载。
<?php
$context = stream_context_create(array(
'http' => array(
'method' => 'GET',
'header' => sprintf('Authorization: Basic %s', base64_encode('username:password'))
)
));
$fileUrl = 'http://domain1.com/shared/portal.php?file=testfile'
$contents = file_get_contents($fileUrl, false, $context);