I want to send a POST request to some file when I load index.php. I use this code:
$query = http_build_query(array('ajax' => 'gwonline', 'session' => $current_session));
$contextData = array (
'method' => 'POST',
'header' => "Content-Type: application/x-www-form-urlencoded
".
"Connection: close
".
"Content-Length: ".strlen($query)."
",
'content'=> $query );
$context = stream_context_create (array ( 'http' => $contextData ));
$result = file_get_contents(PATH::Includes . 'ajax.php', false, $context);
$result_decoded = json_decode($result, true);
echo '<div id="gwonline">' . ($result_decoded['isonlinestr'] ?: '<span class="fa fa-circle" style="color:orange"></span> Unavailable') . '</div>';
The problem is, $result
ends with getting the PHP code instead of whatever the file actually printed. How can I fix it without changing the site to http://...
as it's not an option for me at the moment.
Use include
with output buffering. file_get_contents is reading the file from the filesystem, it isn't going to use PHP to analyze it. The only reason it works when you use http://
is because then the web server is serving the file, not the filesystem.
ob_start();
include "ajax.php";
$result = ob_get_clean();