0

I have a class in a server1. FILE1 in server1

<?php
class myObject
  {
    public function __construct()
    {
      echo 'Hello, World';
    }
  }

?>

FILE 2 in server2

$section = require('http://xx.xxx.xxx.x/plugins/myObject.php');
$intance = new myObject();

When file2 is called from a php file in server1 itself, Object is created. I can see 'Hello World' in browser. But it fails when file2 is called from server2. I get fatal error class not found. I have tried include/file_get_contents/read/ __autoload /spl_autoload_register methods also. Nothing helps to invoke my class from another server.

Is this possible? Can anyone please suggest an alternative? Please help

UPDATE: i have fopen and include url on in server2 from where iam trying to include file. Actually I needed the class and my website to be two servers.

SCENARIO: I am tring to build a wallet website in server2. I have necessary plugins in another server [s1]. I have written a class file interacting with plugins in server 1 itself. Iam planning to have wallet websites in more servers. but all of these websites will interact with class in server1. If I could somehow get the code in that class to my website, then i could create objects and call class methods from other servers also. Please suggest other way to implement this.

UPDATE 2: Can I build somthing like API where all my websites will send request to main class in S1 and get get response. An example would be helpful

SMJ
  • 716
  • 1
  • 9
  • 23
  • including a file from another server will trigger a GET request, and the script on the remote server will be executed on the remote end. Can't see how you can 'require' a remote script. Maybe you should think about writing your code another way, there shouldn't be any need to require a remote script. Try dependency tools such as Composer – Pierre Feb 28 '18 at 10:33
  • Possible duplicate of [including php file from another server with php](https://stackoverflow.com/questions/2752783/including-php-file-from-another-server-with-php) – Ayaz Ali Shah Feb 28 '18 at 10:33
  • Why do you want to do this? What are you trying to achieve? – Josh Woodcock Feb 28 '18 at 10:35
  • @Mr.Developer I saw above link and tried require as said in answer. – SMJ Feb 28 '18 at 10:36
  • according to your recent edits, you do need a dependency tool such as Composer ; it will replicate the php scripts on all your servers (although code will run locally) – Pierre Feb 28 '18 at 10:46
  • @JoshWoodcock I want many websites to interact with one class. – SMJ Feb 28 '18 at 10:51
  • @Pierre thanks. do you have any documents about using composer. I haven't used it before. Iam having a hard time understanding the official documentation – SMJ Feb 28 '18 at 10:53
  • @SMJ why do you want your websites to interact with 1 physical class? Why not have the same class on both servers? – Josh Woodcock Feb 28 '18 at 10:54
  • @JoshWoodcock Thats the problem the plugins have other underlying softwares which are also installed in s1. The plugins are initialised with localhost values. So my class will work only on that server. I can't install all necessary core softwares needed by plugin in all my wallet servers. The core softwares take alot of RAM and disk space. So a better option seemed like calling that class from all website – SMJ Feb 28 '18 at 10:58

5 Answers5

2

Is this possible?

No. PHP code never leaves the server. That's why there are dependency management tools like Composer.

Dormilich
  • 927
  • 5
  • 11
2

If you want to run code on server 2 from server 1 you need to implement a webservice that does that. So server 2 "calls" the php file on server 1 it does not "require" it. Try something like this:

File 1 Server 1

   <?php
    class myObject
    {

        public function __construct()
        {
          echo json_encode(['result'=>'really cool data result'])
        }
    }

   new myObject();

    ?>

File 2 Server 2:

<?php

set_time_limit(0);

$url = "http://xx.xxx.xxx.x/plugins/myObject.php";

$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result_json=curl_exec($ch);
// Closing
curl_close($ch);

$result_obj = json_decode($result_json);

$result = $result_obj->result

If you want to run code on server 1 you have to do it like this and then use the result from server 1 and do something with it on server 2. You cannot run code on server 2 using software that exists on server 1 because by definition you cannot run something on one machine that exists on another machine.

Josh Woodcock
  • 2,683
  • 1
  • 22
  • 29
0

You may need to first download the file and then requireing it.

Anne Douwe
  • 681
  • 1
  • 5
  • 19
0

No you can't do that. And even if there are really bad work arounds to do that, you should NEVER do that.

It makes your code very vulnerable, if you care about security.

buckyBadger
  • 236
  • 1
  • 6
0

Can you require from remote file? No. Can you include from remote file? Yes. This is known as Remote File Inclusion and usually it is considered a security risk. From PHP's documentation of include:

If "URL include wrappers" are enabled in PHP, you can specify the file to be included using a URL (via HTTP or other supported wrapper - see Supported Protocols and Wrappers for a list of protocols) instead of a local pathname. If the target server interprets the target file as PHP code, variables may be passed to the included file using a URL request string as used with HTTP GET. This is not strictly speaking the same thing as including the file and having it inherit the parent file's variable scope; the script is actually being run on the remote server and the result is then being included into the local script.

To use this feature, open your php.ini file and set the allow_url_include as 1 or "On".

After that, you can now do

$section = include('http://xx.xxx.xxx.x/plugins/myObject.php');
$intance = new myObject();

Be warned though, if you allow a user to manipulate the argument to include, he would be able to inject arbitrary PHP code.

0xcrypto
  • 109
  • 1
  • 4