我有一个数组,用于存储 www.website1.com 上的用户登录信息。当用户登录 www.website1.com 时,我需要将用户连同数组中的信息(用户名、密码等)一起发送到 www.website2.com。
这是在 www.website1.com 上设置的数组
$user = [
"user_name"=>$_POST["user_name"],
"user_pass"=>$_POST["user_pass"],
"from"=>$result["domain_id"]
];
您可以创建一个表单,其动作指向您要重定向用户的 url。在表单输入中包含您要发送的数据。最后发送表格。这可以使用 javascript 完成,无需任何用户操作。
第一个服务器(www.website1.com):index.php
<?php
session_start();
$api_key = "ChowKiKo";
//First Server: index.php
if(isset($_SESSION['username'])) {
//-----------Server2 Validation--------------
//Validate if the Server2's api_key is the same as my api_key
if(isset($_POST['api_key']) && $_POST['api_key'] == $api_key) {
//lets use methoding
if(isset($_POST['method']) && $_POST['method'] == 'getUserInfo') {
switch($_POST['method']) {
case 'getUserInfo':
$arr = array('username' => $_POST['username'], 'password' => $_POST['password'], 'from' => "http://www.website2.com");
echo json_encode($arr);
break;
case 'getUserPost':
//mysql_query()...[code here and echo the value]
break;
case 'getUserFriends':
//the same thing here
break;
}
}
}
}else {
?>
<html>
<head>Sample jSON with cURL</head>
<body>
<!-- Login Options -->
<form method="post" action="index.php">
<input type="text" name="username" />[...]
</form>
</body>
</html>
<?php
}
?>
第二服务器(www.website2.com):index.php
<?php
//Second Server: index.php
$api_key = 'ChowKiKo'; // Your API key.
$server1 = 'http://www.website1.com'
//$sessid = '9999your9999sess9999id'; // Your session ID.
//starting cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $server1);
//prepare the field values being posted to the service
$data = array(
'method' => '"getUserInfo"',
'api_key' => '"'. $api_key .'"',
//'sessid' => '"'. $sessid .'"',
//'arg1' => '"some value"',
//'arg2' => '"somevalue"',
//'argN'=>'"another value"',
);
// POSTFIELD like GET process but processing internally with www.website1.com/index.php?method=getUserInfo&api_key=ChowKiko
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
//make the request
$result = curl_exec($ch);
//print the jSON of the Server1, else it will return false.
echo $result;
?>