Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

From php file call another php file by giving it also a parameter

Tags:

post

php

I will explain with a simple example:

myphp1.php:

$html = get_html("myphp2.php", "parameter1"); //pseudocode

myphp2.php

<html>
  <head>
  </head>
  <body>
    <?php
      echo $_POST["parameter1"];
    ?>
  </body>
</html>

So basically the $html will hold the myphp2.php html output. Can I do that?

like image 623
biox Avatar asked Dec 01 '25 14:12

biox


1 Answers

If you are looking to interpret the php script, and save the output, you should send a new request.

Using PHP5 you can do this without curl:

$url = 'http://www.domain.com/mypage2.php';
$data = array('parameter1' => 'value1', 'parameter2' => 'value2');

$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data),
    ),
);
$context  = stream_context_create($options);
$html = file_get_contents($url, false, $context);

var_dump($html);
like image 105
David Houde Avatar answered Dec 03 '25 04:12

David Houde



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!