2014-09-26 37 views
2

我想從我的其他網站調用PHP函數外調用函數在PHP ...它是可能 php文件是這個樣子我的服務器上 - daplonline.in/short.php如何從mysite的

<?php 
function writeMsg() { 
    echo "Hello world!"; 
} 
?> 

而我想從我的其他網站調用此功能是否有可能?

<?php 
    include("http://daplonline.in/short.php"); //it is 777 
    writeMsg(); 
?> 
+0

的輸出有你在處理有任何錯誤這個腳本? – DeDevelopers 2014-09-26 11:01:45

+0

Impossible: http://stackoverflow.com/questions/3101327/using-includes-cross-domain-that-c​​ontain-php-code-failing – 2014-09-26 11:02:23

+0

這是不可能的沒有...即使這個網站是在相同的生產服務器ü會得到一個base_restriction對此包括 – DarkBee 2014-09-26 11:02:27

回答

1

您要找的內容在Java中稱爲RMI。但據我所知,PHP中沒有什麼可以做「遠程方法調用」。所以我會說這是不可能的。

但是你可以編寫一個腳本,它根據請求的URI調用正確的函數。

所以我們說在$_GET放慢參數:

http://daplonline.in/rmi.php?method=short.writeMessage

然後在你的rmi.php你必須調用基礎上,method放慢參數正確的函數。

<?php 

list($scriptName, $methodName) = explode('.', $_GET['method']); 

require $scriptName . '.php'; 

echo serialize(call_user_func($methodName)); 

要快速和骯髒地摘要它。你可以unserialize()的迴應rmi.php並得到返回的數據。

+0

[它在PHP中,但由於其他原因未提出](http://stackoverflow.com/questions/1158348/including-a-remote-file -in-php):) – NoobEditor 2014-09-26 11:04:46

0

是的,你可以。儘管這是一個巨大的安全風險。

allow_url_include布爾

此選項允許有以下功能使用URL的fopen封裝的:包括,include_once,要求,require_once。

ini_set('allow_url_include', 1); 

延伸閱讀:including a remote file in PHP

+0

應該提到的是,對於包括HTTP在內的URL,目標服務器不能解釋請求的腳本,而是按原樣返回。 – lafor 2014-09-26 11:07:22

+0

哪裏喊我ste? ini_set('allow_url_include',1); – 2014-09-26 11:13:25

0

你可以連接到使用CURL文件,並指定一個參數。

該文件可以檢查該參數並調用特定功能。

這不完全是你想要的,但它應該工作。最好將它製作成API,而不是包含外部PHP文件,然後從它們調用函數。您可以通過這種方式添加更多安全性,例如只接受來自某些URL的連接。

short.php將包含類似

<?php 
function writeMsg() { 
    echo "Hello world!"; 
} 
writeMsg(); 
?> 

來自其他服務器的文件可以包含

<?php 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "http://daplonline.in/short.php"); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); 
$result = curl_exec($ch); 
curl_close($ch); 
echo $result; 

$result將是short.php腳本

+0

我想從我的其他網站調用函數writeMsg()只有 – 2014-09-26 11:09:00

+0

然後可能使用'allow_url_include'方法會更好。但是,如果'writeMsg()'不帶任何參數,並且只想調用該函數,那麼也可以使用CURL。該外部文件將調用該函數並返回輸出。所以你只需在主網站的頁面上顯示它。 – cornelb 2014-09-26 11:11:21

+0

allow_url_include是安全的? – 2014-09-26 11:21:39

相關問題