Jump to content

Remote functions in PHP


Auriga

Recommended Posts

Hi!

I have two servers: Server A and Server B with a website. Server A contains some general useful code in php functions, which will be used in the website on Server B.

Now, how can I include the functionality in a php file on Server A in the website on Server B?

For example:
[myfunctionfile.php on Server A]
function MyFunction(myArgs)
{
  return "this is a test" + myArgs;
}

[myWebPage.php on Server B]
<?php
  include("http://www.servera.com/myfunctionfile.php");

  print MyFunction("for server B");
?>


The above should print out on myWebPage.php:

this is a test for server B


I already tried to rename the myfunctionfile.php to myfunctionfile.php.inc, but that didn't work.


Thanks in advance!
Link to comment
Share on other sites

I'm not real experienced in this, but I know that the way you are doing this cannot work for one reason.
You are retrieving the evaluated php page, not the raw page.
eg: include("http://www.servera.com/myfunctionfile.php"); includes the html page that you get when visiting the page on your browser, not the php textfile that your code is looking for.

Now I have three ideas, and i don't know how well any of them work, since I have never tried them.

1) You could rename myfunctionfile.php myfunctionfile.txt so that Apache doesn't try to execute it before sending it.
Downfalls: I don't know if Webpage.php will be able to figure out that the php commands in a text file are php commands and not text, but i suspect it will work as long as you surround the commands with <?php ?>
  Also, this will have the unfortunate effect of making your php function file publicly available for veiwing, but if this doesn't bother you, go ahead.

2) You could change the include functuion to say: require("ftp://username:password@www.servera.com/myfunctionfile.php");
Downfalls: This will not work with versions of PHP prior to PHP 4.3.0
  I don't know if it accepts FTP even after 4.3.0

3) If number 2 doesn't work, you could include something like this: (I have not actually tried this yet, and their might be some bugs)
[code]
$handle = fopen("temp.php", 'w');
$conn_id = ftp_connect("ftp://www.servera.com/myfunctionfile.php");
$login_result = ftp_login($conn_id, $YOURUSERNAME, $YOURPASSWORD);
if ((!$conn_id) || (!$login_result)) {
      echo "FTP connection has failed!";
      exit;
  }
if (!ftp_fget($conn_id, $handle, myfunctionfile.php, FTP_ASCII, 0))
{
      echo "Could not get myfunctionfile.php";
      exit;
}
ftp_close($conn_id);
fclose($handle);
require("temp.php");
unlink("temp.php");
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.