Search code examples
phpincludephp-include

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


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

Solution

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