0

include A server .php file in B server .php file and get variable values from A server Php File

I'm trying to call PHP file which is located in another server which is connected with RPG(Report Program Generator, AS400). I would like to call that PHP file from my web server and would like to have access to variable and functions.

I tried Include but not working

I would like to call .PHP file which in RPG side with parameter.

A.php

 <?php

  include("http://10.1.1.12/a/[email protected]&name=abc");

  echo $a; //values from file.php
  ?>

file.php

<?php
 $email = $_REQUEST['email'];
 $name = $_REQUEST['name'];
 $a = "testing";
 echo $a;
 ?>
4
  • Request 10.1.1.12/a/[email protected]&name=abc in your browser and tell what you see. Is it a valid php code in the response? Commented May 1, 2014 at 21:25
  • Add some more details. Commented May 1, 2014 at 21:48
  • 2
    @Tulon Please, please, stop editing questions merely for formatting (namely, as it seems, BOLDING THE SHIT OUT OF STUFF) If you have content edits to clarify a question, that's great. But stop it. Commented May 1, 2014 at 21:56
  • You need to implement inter-server communication. Simple way is to file_get_contents, like in answers below and return (output), for example serialized array of variables, which you have to unserialize and extract on the receiver script. Think about the possible security implications with such approaches. Commented May 1, 2014 at 22:07

2 Answers 2

1

If you have control over the php applications on both servers, it might be advisable to build a php interface on B. This is fairly simply accomplished. A basic implementation would be:

A:

<?php
    $a = file_get_contents("http://10.1.1.12/a/[email protected]&name=abc");
    $a = unserialize($a);
    print_r($a);
?>

interface.php on B:

<?php
    $email = $_REQUEST['email'];
    $name = $_REQUEST['name'];

    $result = doSomething($email, $name); //return an array with your data
    $result = serialize($result);

    echo $result;
?>

Of course there is no validation here, so you'll need to add checks for valid data.

I'm not sure if you'd run in to character encoding issues between servers, and you'll want to make sure you write correct headers on B.

Also, be advised that anyone on the same network can use the interface. Make sure you implement some security checks.

If you need to send the request with more parameter data than a url will allow (1024 characters I think?), this may be helpful: How do I send a POST request with PHP?

Sign up to request clarification or add additional context in comments.

Comments

0

Getting the response of another server using include is disabled by default in the php.ini for security reasons, most likely you won’t be able to use it. Use file_get_contents instead.

In your file.php you can make a json response using your data and echo it:

<?php
$email = $_REQUEST['email'];
$name = $_REQUEST['name'];
$a = "testing";

header('Content-type: application/json');
echo json_encode(
    'email' => $email,
    'name' => $name,
    'a' => $a
);
?>

And in the A.php you need to parse the json string to get your data:

<?php
$data = json_decode(file_get_contents('http://10.1.1.12/a/[email protected]&name=abc'));
echo $data['email'], ' ', $data['name'], ' ', $data['a'];
?>

11 Comments

Well, you can include the response. If the remote server serves .php files as-is - you definitely can include them.
@zerkms Yes, but for security reasons allow_url_include in the php.ini is disabled by default and why to enable it when you have an alternative.
@Manolis Agkopian: well, I didn't talk about security or whatnot. I just made a note that "You can't include a php file from an other server" is technically incorrect.
@zerkms I know, you are right I wasn't precise by saying you can't. I updated my answer.
@user3594231 You get nothing because the file.php does not output anything you need to do an echo.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.