2

I want to call a php function from my other site...it is possible php file is look like this on my server - daplonline.in/short.php

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

And i want to call this function from my other site is it possible ?

<?php
    include("http://daplonline.in/short.php"); //it is 777
    writeMsg();
?>
Savan Paun
  • 1,723
  • 2
  • 16
  • 24
  • have you got any error while processing this script?? – DeDevelopers Sep 26 '14 at 11:01
  • Impossible: http://stackoverflow.com/questions/3101327/using-includes-cross-domain-that-contain-php-code-failing – Mark Sep 26 '14 at 11:02
  • This is not possible no... Even if this site was on the same production server u would get a base_restriction on this include – DarkBee Sep 26 '14 at 11:02
  • http://stackoverflow.com/a/1158392/3000179 – ʰᵈˑ Sep 26 '14 at 11:02
  • 1
    possible duplicate of [including a remote file in PHP](http://stackoverflow.com/questions/1158348/including-a-remote-file-in-php) – Jonathon Sep 26 '14 at 11:02
  • Warning: include(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in C:\xampp\htdocs\aff\ss.php on line 2 Warning: include(http://daplonline.in/short.php): failed to open stream: no suitable wrapper could be found in C:\xampp\htdocs\aff\ss.php on line 2 . Warning: include(): Failed opening 'http://daplonline.in/short.php' for inclusion (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\aff\ss.php on line 2 – Savan Paun Sep 26 '14 at 11:03
  • got 3 error in my file – Savan Paun Sep 26 '14 at 11:06

3 Answers3

1

What you're looking for is called RMI in Java. But as far as I know there's nothing in PHP which can do "Remote Method Invocations". So I would say it's not possible.

But you could write a script which calls the right function based on the URI of the request.

So let's say over a $_GET paramter:

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

Then in your rmi.php you have to call the right function based on the method paramter.

<?php

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

require $scriptName . '.php';

echo serialize(call_user_func($methodName));

To abstract it quick and dirty. You than can unserialize() the response of the rmi.php and get the returned data.

TiMESPLiNTER
  • 5,741
  • 2
  • 28
  • 64
  • [It is there in PHP, but not suggested due to secirty reasons](http://stackoverflow.com/questions/1158348/including-a-remote-file-in-php) :) – NoobEditor Sep 26 '14 at 11:04
0

Yes, you can. It's a huge security risk though.

allow_url_include boolean

This option allows the use of URL-aware fopen wrappers with the following functions: include, include_once, require, require_once.

ini_set('allow_url_include', 1);

Further reading: including a remote file in PHP

Community
  • 1
  • 1
ʰᵈˑ
  • 11,279
  • 3
  • 26
  • 49
0

You could connect to the file using CURL, and specify a parameter.

The file could check that parameter and call a particular function.

It's not exactly what you want, but it should work. It's better to make this like an API, instead of including external PHP files and then calling the functions from them. You could add more security this way, like accepting connections only from certain URLs.

short.php will contain something like

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

The file from the other server can contain

<?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 will be the output of the short.php script

cornelb
  • 6,046
  • 3
  • 19
  • 30
  • i want to call function writeMsg() from my other site only – Savan Paun Sep 26 '14 at 11:09
  • then maybe using the `allow_url_include` approach would be better. But if `writeMsg()` does not take any parameters, and you want to call only this function, you can use CURL too. That external file will call the function and can return the output. So you just display it, on the main site's page. – cornelb Sep 26 '14 at 11:11
  • allow_url_include is safe ?? – Savan Paun Sep 26 '14 at 11:21
  • As the other people were saying, enabling allow_url_include means people will be able to include the php file from that server, so it is not safe. CURL is normally used for this type of requests – cornelb Sep 26 '14 at 11:30
  • so can i create API ? for call the function ? – Savan Paun Sep 26 '14 at 11:34
  • Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request. – Savan Paun Sep 26 '14 at 12:09
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/61990/discussion-between-cornelb-and-user3705511). – cornelb Sep 26 '14 at 13:34
  • maybe you can play a bit with the curl settings. I added something in the answer – cornelb Sep 26 '14 at 13:43