PHP:远程函数调用并返回结果? [英] PHP: Remote Function Call and returning the result?

查看:190
本文介绍了PHP:远程函数调用并返回结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对PHP不是很熟练.我想知道如何在2台Web服务器之间进行通信.为了方便起见,(从第一台服务器)在远程服务器上运行一个函数(查询).并将结果返回到第一台服务器.

I'm not very expert to PHP. I want to know how to communicate between 2 web servers. For clearance, (from 1st Server) run a function (querying) on remote server. And return the result to 1st server.

实际上,主题将是:
Web Server (1) ----------------> Web Server (2) ---------------> Database Server
Web Server (1)< ---------------- Web Server (2)< --------------- Database Server

Actually the theme will be:
Web Server (1) ----------------> Web Server (2) ---------------> Database Server
Web Server (1) <---------------- Web Server (2) <--------------- Database Server

Query Function()仅位于Web Server (2)上.然后我需要从Web Server (1)远程运行query function().

Query Function() will be only located on Web Server (2). Then i need to run that query function() remotely from Web Server (1).

这叫什么?而且有可能吗?

What is it call? And Is it possible?

推荐答案

是.

我能想到的一种不错的方法是通过URL将请求发送到第二台服务器.在GET(或POST)参数中,指定您要调用的方法,以及(出于安全性考虑)某种随时间变化的哈希值.其中的哈希确保没有第三方可以在第二台服务器上任意运行该功能.

A nice way I can think of doing would be to send a request to the 2nd server via a URL. In the GET (or POST) parameters, specify which method you'd like to call, and (for security) some sort of hash that changes with time. The hash in there to ensure no third-party can run the function arbitrarily on the 2nd server.

要发送请求,您可以使用cURL:

To send the request, you could use cURL:

function get_url($request_url) {
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $request_url);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  $response = curl_exec($ch);
  curl_close($ch);

  return $response;
}

这发送一个GET请求.然后,您可以使用:

This sends a GET request. You can then use:

$request_url = 'http://second-server-address/listening_page.php?function=somefunction&securityhash=HASH';
$response = get_url($request_url);

在第二台服务器上,设置listening_page.php(当然,使用您喜欢的任何文件名)以检查GET请求并验证请求的完整性(即哈希,正确和有效的参数).

On your second server, set up the listening_page.php (with whatever filename you like, of course) that checks for GET requests and verifies the integrity of the request (i.e. the hash, correct & valid params).

这篇关于PHP:远程函数调用并返回结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆