我有两台服务器,服务器1(主机)向服务器2(客户端)提供内容。
在服务器2上我正在使用
file_get_contents('http://domain1.com/shared/portal.php?file=testfile');
检索所请求的文件内容。但在此内容可以在服务器2上检索之前,我想实现某种身份验证(用户名/密码)。我想在flc的url中包含用户名和密码查询,并在发回内容之前检查服务器1上的db,但是在某些情况下需要通过循环多次打印fgc内容,所以如果fgc被称为10次,我不想在服务器1上点击10次。
所以基本上我希望能够在服务器一或类似的东西上调用一个auth文件来访问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>
答案 0 :(得分:1)
您可以使用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);