Php 服务器对服务器实时通信
我有两个基于LAMP(使用Drupal CMS)的网站,我想在服务器之间进行通信。例如,网站1上的客户机执行一些活动,数据和内容被传送到网站2,网站2处理数据/请求并回复到网站1客户机。我该怎么做?是否有任何库或模块可以实现这一点?我从哪里开始构建这样的功能?您想要做的事情可以通过HTTP协议使用POST查询通过套接字完成 例如,有一个从服务器a到服务器B的通信。注意:您可以使其双向 HTTP查询(客户端)Php 服务器对服务器实时通信,php,apache,drupal,p2p,Php,Apache,Drupal,P2p,我有两个基于LAMP(使用Drupal CMS)的网站,我想在服务器之间进行通信。例如,网站1上的客户机执行一些活动,数据和内容被传送到网站2,网站2处理数据/请求并回复到网站1客户机。我该怎么做?是否有任何库或模块可以实现这一点?我从哪里开始构建这样的功能?您想要做的事情可以通过HTTP协议使用POST查询通过套接字完成 例如,有一个从服务器a到服务器B的通信。注意:您可以使其双向 HTTP查询(客户端) 听起来你想写一个api。 # the target url (without http:
听起来你想写一个api。
# the target url (without http://) or address of the remote host
# if the remote address is an ipv6 she must start and end with [] like this [::1].
$http_host = "website1.com"; # api.website1.com or localhost or 13.33.33.37
# The address of the script who's give answer from the root directory "/".
$script_path = "/answer.php";
# The parameters.
$http_params = "cost=156&product=" . urlencode("string must be url encoded");
$http_query = "POST " . $script_path . " HTTP/1.0" . "\r\n";
$http_query .= "Host: " . $http_host . "\r\n";
$http_query .= "Content-Type: application/x-www-form-urlencoded;" . "\r\n";
$http_query .= "Content-Length: ".strlen($http_params) . "\r\n";
$http_query .= "User-Agent: Drupal/PHP" . "\r\n\r\n";
$http_query .= $http_params;
$http_answer = NULL;
if ($socket = @fsockopen($http_host, 80, $errno, $errstr, 10))
{
fwrite($socket, $http_query);
while (!feof($socket))
$http_answer .= fgets($socket, 1024);
fclose($socket);
}
$http_answer = explode("\r\n", $http_answer);
if (is_array($http_answer))
{
echo "<pre>";
print_r($http_answer);
echo "</pre>";
}
# if the parameters are matched.
if (isset($_POST['cost'], $_POST['product']))
{
# some treatement on the data
if (is_numeric($_POST['cost']))
echo "The cost were defined to $_POST[cost]" . "\r\n";
else
echo "The cost attribute must be a numerical value." . "\r\n";
if (!is_numeric($_POST['product']))
echo "The product were correctly registered." . "\r\n";
else
echo "The product attribute must be different than a numerical value." . "\r\n";
}
# otherwise the parameters are wrong.
else echo "Something went wrong.";