将数组从一个服务器传递到另一个服务器


Pass array from one server to another

我有一个数组,用于存储www.website1.com上的用户登录信息。当用户登录www.website1.com时,我需要将用户连同他们在数组中的信息(user_name、密码等)一起发送到www.website2.com。

这是在www.website1.com

得到的数组
$user = [
"user_name"=>$_POST["user_name"],
"user_pass"=>$_POST["user_pass"],
"from"=>$result["domain_id"]
];

您可以创建一个表单,其中的action指向您想要重定向用户的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;
?>
相关文章: