1

Sorry for my bad English

Is it possible if I read text as code for a function on one server and execute it on another server?

For example, i have a code in my server1 index.php with domain1.com

<?php
    function outputString()
    {
        echo "Test";
    }
?>

And I want to call the function above on server2 for example index.php with domain2.com

I have tried using the code below but there is an error

<?php
    require_once('http://domain1.com/index.php');

    outputString()
?>

the error that appears

[25-Jan-2019 16:30:37 UTC] PHP Fatal error:  Call to undefined function outputstring() in /home/website/domain2.com/index.php on line 3

So, can I call functions from other servers using other methods? Or is this impossible? Thank you

AbsoluteƵERØ
  • 7,816
  • 2
  • 24
  • 35
nurfaizfy
  • 11
  • 4
  • not like this, *in theory* - you could download a webpage, and with file_put_contents recreate the functions on your server - but I guess.. what's the point. If you know the function, you might as well just recreate in a .php file without the overheads – treyBake Jan 25 '19 at 16:37
  • In the error output, it states `undefined function outputstring()` but your code says `outputString()` is there possibly a typo in the code? – K Richardson Jan 25 '19 at 16:39
  • 3
    you could make an api and use something like if ($_GET['outputstring'] == "true") { echo "Test"; } and you should use something like file_get_contents instead of require_once() – TimeParadox Jan 25 '19 at 16:39
  • @EPKgames not a bad shout - an API probs is the best way to go - though if the OP has control over both domains (as it seems), probs best to just have the function in both projects - though.. if domain1 is wanting public accessibility outside of OPs projects, then API is a good way to go – treyBake Jan 25 '19 at 16:41
  • @treyBake tnx, also it is indeed better to make the functions on the other domain because the page waiting time will be very long because it needs to wait for the other webserver to respond. – TimeParadox Jan 25 '19 at 16:45
  • @KRichardson it's not a typo, but it's like that written in error_log – nurfaizfy Jan 25 '19 at 17:02
  • @EPKgames Thank you for your advice, I will try it – nurfaizfy Jan 25 '19 at 17:03
  • 1
    The duplicate isn't correct. That's about interacting between 2 servers. Not simply running a function from one on another. – AbsoluteƵERØ Jan 25 '19 at 17:08
  • 1
    Store the code on server1 in a string *without – AbsoluteƵERØ Jan 25 '19 at 17:20
  • 3
    [`eval` is evil!](https://blogs.msdn.microsoft.com/ericlippert/2003/11/01/eval-is-evil-part-one/) – Aleks G Jan 25 '19 at 17:26
  • CURL will do !!!! check the examples on http://php.net/manual/pt_BR/book.curl.php – Diego Favero Jan 25 '19 at 20:33

0 Answers0