Reputation: 11
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;
?>
Upvotes: 0
Views: 6079
Reputation: 1094
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'];
?>
Upvotes: 0
Reputation: 1168
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?
Upvotes: 1