Jump to content


Photo

Remote functions in PHP


  • Please log in to reply
1 reply to this topic

#1 Auriga

Auriga
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 21 October 2006 - 06:04 PM

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.c...nctionfile.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!

#2 ksteuber

ksteuber
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts
  • LocationCalifornia, USA

Posted 23 October 2006 - 01:17 AM

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.c...nctionfile.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)
$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");





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users