calling php script within php with return value calling php script within php with return value curl curl

calling php script within php with return value


If you want to capture the echoed values of an included script, you can use output buffering:

<?phpob_start();    // start output bufferinginclude("caller.php");$returned_value = ob_get_contents();    // get contents from the bufferob_end_clean();    // stop output buffering?>


As far as I know, you need a valid http (or other protocol) with a full URL to the script for curl to work e.g. http://localhost/caller.php

Or try jeroen's solution and get the output of the script.


In the script to be called, make sure "<?php" start from the first line, first character. And, remove the "?>" away.

<?php//this is 'to-be-called.php'include("config.php");echo "complete";

Now call it this way:

<?php//this is 'caller.php'//should it come with full url?$Url      = "http://localhost/path/to-be-called.php"; $Handle   = curl_init($Url);$Response = curl_exec($Handle);curl_close($Handle);echo $Response;

Or take Jeroen's answer, it's the best way for calling on the same server! If you need to pass in parameters in POST/GET, tell 'caller.php' to save these values in global variables, then tell 'to-be-called.php' to get from these global ones.