2 つの LAMP (Drupal CMS を使用) ベースの Web サイトがあり、サーバー間の通信を実行したいと考えています。例として、Web サイト 1 のクライアントが何らかのアクティビティを実行し、データとコンテンツが Web サイト 2 に伝達され、Web サイト 2 がデータ/要求を処理し、Web サイト 1 クライアントに応答します。これどうやってするの ?これを達成するためのライブラリやモジュールはありますか? そのような機能を構築するには、どこから始めればよいですか?
1 に答える
1
あなたがしたいことは、POSTクエリを使用してHTTPプロトコルを介してソケットで実行できます。
たとえば、サーバー A からサーバー B への通信があります。注意: 双方向にすることができます。
HTTP クエリ (クライアント)
# 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>";
}
少し想像力を働かせれば、かなり優れたツールを作成できます。Google 自身がこの方法を使用して、reCAPTCHA でチャレンジを生成しています。
HTTP ハンドラー (サーバー)
# 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.";
于 2013-07-08T03:12:10.913 に答える