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

时间:2013-04-07 14:04:14

标签: php json

我有一个数组,用于在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"]
];

2 个答案:

答案 0 :(得分:0)

您可以创建一个表单,其中的操作指向您要重定向用户的网址。在表单输入中包含要发送的数据。最后发送表格。这可以使用javascript完成,无需任何用户工作。

答案 1 :(得分:0)

First Server(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
    }
?>

Second Server(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;
?>