简体   繁体   English

如何从另一台服务器调用PHP函数?

[英]How can I call a PHP function from another server?

Is there a way that I can call a PHP function from another server? 有没有办法可以从另一台服务器调用PHP函数?

I have access to both these 2 servers. 我可以访问这两个服务器。 I want to call a PHP function from server 1 and use it in server 2. 我想从服务器1调用PHP函数,并在服务器2中使用它。

What do I need to change on both servers to make it possible? 我需要在两台服务器上进行哪些更改才能实现? Thanks! 谢谢!

You could use an API to do this. 您可以使用API​​来执行此操作。 On Server 1 using Curl or GuzzleHttp you want to make an API call to Server 2 在使用Curl或GuzzleHttp的服务器1上,您要对服务器2进行API调用

When Server 2 receives the request from Server 1 you can have the function run. 服务器2收到服务器1的请求后,便可以运行该功能。

One thing to consider here is security, you will need a way of verifying that only Server 1 is making requests and not somebody who's figured out your API. 这里要考虑的一件事是安全性,您将需要一种方法来验证只有Server 1在发出请求,而不是在弄清楚您的API的人。 Usually an API Key is passed when making the request and it's checked on the API. 通常,发出请求时会传递API密钥,并在API上进行检查。

Essentially what you're asking is how to do a remote procedure call (RPC) between two PHP applications. 本质上,您要问的是如何在两个PHP应用程序之间进行远程过程调用(RPC) To communicate between two applications (PHP applications in your case) that are hosted on different servers you need to choose some kind of communication protocol . 要在不同服务器上托管的两个应用程序(在您的情况下为PHP应用程序)之间进行通信,您需要选择某种通信协议

The protocol you choose would depend on many things, including what your applications do, how often they would communicate, what kind of data you want to send between them, etc. 您选择的协议取决于很多事情,包括您的应用程序执行的操作,它们之间进行通信的频率,要在它们之间发送的数据类型等等。

I'm no expert myself, but from what I know gRPC and REST are two of the more popular API communication protocols. 我自己不是专家,但是据我所知, gRPCREST是两种比较流行的API通信协议。 See these articles for comparisons between the two. 请参阅这些 文章以进行两者之间的比较。 If your servers are going to be communicating back and forth very frequently you might want to consider WebSockets or MQTT . 如果您的服务器将非常频繁地来回通信,则可能需要考虑使用WebSockets或MQTT

For most of these protocols you're going to have to set up a service/server on the one server (the server on which your function is that you want to be called) and a client on the other (the server from which you want to call the function). 对于大多数这些协议,您将不得不在一台服务器(要在其上调用您的功能的服务器)上设置服务/服务器,并在另一台服务器(要从中调用该服务器的服务器)上设置客户端调用该函数)。

To set up gRPC, have a look at this PHP tutorial . 要设置gRPC,请查看此PHP 教程

To set up REST, you would need to expose a HTTP endpoint on your server-host and consume the endpoint from your client-host. 要设置REST,您需要在服务器主机上公开HTTP端点,并从客户端主机使用该端点。 Exposing the endpoint is most easily done with an API framework of which there are many (just Google "PHP REST framework"). 公开端点最容易使用一个API框架来完成(其中只有Google“ PHP REST框架”)。 Consuming the endpoint can be done with a HTTP client, such as Guzzle , like some of the other answers mention. 可以使用HTTP客户端(例如Guzzle)来完成端点的使用,就像提到的其他一些答案一样。

抱歉,如果不是同一服务器会话不会在其他页面上生效,则无法像sesssion一样进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM