第一臺服務器(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;
?>
爲什麼會這樣下來投票?有人可以告訴我,我還有什麼措辭可以這麼做,所以我可以... – 2013-04-07 14:07:19
「從一臺服務器傳遞到另一臺服務器」是什麼意思? – Dogbert 2013-04-07 14:07:55
我要重新編輯問題。 – 2013-04-07 14:08:40