简体   繁体   English

从不同的域调用PHP函数

[英]Calling PHP functions from different domains

Is it possible to call a PHP function found in a file on another website with a different domain? 是否可以调用在另一个域不同的网站的文件中找到的PHP函数?

For example, I know that to call a PHP function from another file in the same domain (say function aaa() found in aaa.php) I just have to simply do this (with a few simplifying assumptions): 例如,我知道要从同一域中的另一个文件调用PHP函数(例如在aaa.php中找到函数aaa()),我只需要简单地执行此操作(有一些简化的假设):

include_once('aaa.php');
aaa();

I have tried doing something similar, such as: 我尝试做类似的事情,例如:

include_once('http://othersite/aaa.php');
aaa();

I cannot get this to work (the page seems to load fine, with no error messages, but the function does not execute). 我无法使它正常工作(页面似乎可以正常加载,没有错误消息,但是该函数无法执行)。 I have tried require(), which gives me a blank screen. 我试过了require(),这让我空白了。 I have had no success with fopen either. 我也没有用fopen取得成功。

If it is possible to do this, how can I do it? 如果可以这样做,我该怎么办?

The include and require (and their _once variants) take a local filesystem path as their parameter. includerequire (及其_once变体)将本地文件系统路径作为其参数。 Domains have nothing to do with it. 域与它无关。

Yes, you can also put an URL there (if you have the fopen wrappers enabled), but then PHP will just download the file and try to execute it. 是的,您也可以在其中放置一个URL(如果启用了fopen包装器),但是PHP只会下载该文件并尝试执行它。 In other words, for this to work, if you entered http://othersite/aaa.php in your browser, it should show the PHP source, not the results of processing it. 换句话说, http://othersite/aaa.php此方法起作用,如果您在浏览器中输入了http://othersite/aaa.php ,它应该显示PHP源代码,而不是处理它的结果。

When passing an URL to include \\ require , PHP cannot do anything more than your browser. 当传递include \\ require的URL时,PHP只能执行浏览器以外的操作。 It's at the mercy of the webserver at othersite . 它是由其他othersite上的Web服务器othersite If it doesn't return PHP code, there is no way that PHP can get to it. 如果它不返回PHP代码,则PHP无法获得它。

仅当其他服务器设置为将PHP文件作为源提供服务时(即不执行它们),才可以执行此操作。

What you are currently doing is getting the remote server to execute the PHP file and then you're reading the parsed contents -- the same as a browser would. 您当前正在做的是让远程服务器执行PHP文件,然后读取解析的内容-与浏览器相同。 So you get (presumably) HTML, not PHP code. 因此,您大概会得到HTML,而不是PHP代码。

If the remote code does not need to be kept private for any reason (eg security) you can get the remote server to serve you the PHP source code. 如果出于某种原因(例如安全性)不需要将远程代码保密,则可以让远程服务器为您提供PHP源代码。 The easiest way to do that is to rename the file as aaa.txt , so it will not be passed to the PHP interpreter. 最简单的方法是将文件重命名为aaa.txt ,这样就不会将其传递给PHP解释器。

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

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