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/file.php?email=aaa@a.com&name=abc");

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

file.php

<?php
 $email = $_REQUEST['email'];
 $name = $_REQUEST['name'];
 $a = "testing";
 echo $a;
 ?>
user3594231
  • 11
  • 1
  • 3
  • Request http://10.1.1.12/a/file.php?email=aaa@a.com&name=abc in your browser and tell what you see. Is it a valid php code in the response? – zerkms May 01 '14 at 21:25
  • Add some more details. – Tulon May 01 '14 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. – Dan Lugg May 01 '14 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. – Rolice May 01 '14 at 22:07

2 Answers2

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/interface.php?email=aaa@a.com&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?

Community
  • 1
  • 1
SuperJer
  • 1,148
  • 7
  • 18
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/file.php?email=aaa@a.com&name=abc'));
echo $data['email'], ' ', $data['name'], ' ', $data['a'];
?>
Manolis Agkopian
  • 943
  • 12
  • 21
  • Well, you can include the response. If the remote server serves .php files as-is - you definitely can include them. – zerkms May 01 '14 at 21:28
  • @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 May 01 '14 at 21:34
  • @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 May 01 '14 at 21:38
  • @zerkms I know, you are right I wasn't precise by saying you can't. I updated my answer. – Manolis Agkopian May 01 '14 at 21:45
  • @user3594231 You get nothing because the file.php does not output anything you need to do an `echo`. – Manolis Agkopian May 01 '14 at 21:49
  • After adding Echo getting empty html like :- – user3594231 May 02 '14 at 13:28
  • Can I get variable values in other php..? what will happen if i have to get more then 1 values.? – user3594231 May 02 '14 at 13:32
  • @user3594231 It doesn't make sense taking html back, you don't output html anywhere, unless the code you posted it not the full code. – Manolis Agkopian May 02 '14 at 14:05
  • it's not full code but there is no html tag any where in code. it's return only 123456789 like that number in echo but when getting values comeing up with html tag – user3594231 May 02 '14 at 14:09
  • But is there way to get variable value directly.? – user3594231 May 02 '14 at 14:15
  • @user3594231 You can't get a variable directly from another server, you need to output it first as a string and then parse it. One good way to do this is by doing serialize and unserialize like suggested in the other answer. You can also use json if you want. – Manolis Agkopian May 02 '14 at 14:20