Call PHP file located in another server with parameter and read variable values in another server PHP

StackOverflow https://stackoverflow.com/questions/23416879

  •  13-07-2023
  •  | 
  •  

Question

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;
 ?>
Was it helpful?

Solution 2

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'];
?>

OTHER TIPS

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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top