So I have GetMessages.php, this script gets data out of an database(MongoDB). Then it makes a cURL POST to MyMessagesMDB.php. This is the code:
<?php
$FirstName = '';
$LastName = '';
$Email = '';
$Subject = '';
$Message = '';
GetFromDB();
echo $FirstName." ".$LastName;
echo "<br>";
echo $Email;
echo "<br>";
echo $Subject;
echo "<br>";
echo $Message;
echo "<br>";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://192.168.0.163/MyMessagesMDB.php");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"FirstName=$FirstName&LastName=$LastName&Email=$Email&Subject=$Subject&TextMessage=$Message");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close($ch);
function GetFromDB(){
$client = new MongoDB\Driver\Manager('mongodb srv://####:####@####.tldyu.mongodb.net/Data');
$filter = [];
$options = [];
$query = new MongoDB\Driver\Query($filter, $options);
$cursor = $client->executeQuery('ContactMessages.ContactForm', $query);
foreach($cursor as $document){
$document = json_decode(json_encode($document),true);
global $FirstName, $LastName, $Email, $Subject, $Message;
$FirstName = $document['FirstName'];
$LastName = $document['LastName'];
$Email = $document['Email'];
$Subject = $document['Subject'];
$Message = $document['Message'];
}
}
?>
MyMessagesMDB.php is the script to get called first. It calls GetMessages.php and should then receive the information it has posted. Here is the code:
<?php
$FirstLoop = true;
if($FirstLoop == true){
file_get_contents('http://192.168.0.102/test/GetMessages.php');
$FirstLoop = false;
}
$FirstName = $_POST["FirstName"] ?? "";
$LastName = $_POST["LastName"] ?? "";
$Email = $_POST["Email"] ?? "";
$Subject = $_POST["Subject"] ?? "";
$Message = $_POST["TextMessage"] ?? "";
echo $FirstName." ".$LastName;
echo "<br>";
echo $Email;
echo "<br>";
echo $Subject;
echo "<br>";
echo $Message;
echo "<br>";
?>
But here starts my problem.
If I call MyMessagesMDB.php I get the error:
PHP Warning: file_get_contents(http://192.168.0.102/test/GetMessages.php): Failed to open stream: HTTP request failed!.
If I try to load the page manually I get no error and the data out of the database is printed on the screen.
When in MyMessagesMDB.php I comment the following line like this:
#file_get_contents('http://192.168.0.102/test/GetMessages.php');
I can load the page without any problem or error.
The Question
What do I have to do to call GetMessages.php from MyMessagesMDB.php and receive the information out of the database?
Note: I have tried
$curl_handle=curl_init();
curl_setopt($curl_handle,
CURLOPT_URL,'http://###.##.##.##/mp/get?
mpsrc=http://mybucket.s3.amazonaws.com/11111.mpg&mpaction=convert
format=flv');
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Your application
name');
$query = curl_exec($curl_handle);
curl_close($curl_handle);
CodePudding user response:
After our comments back and forth, I think your implementation is way more complicated than it needs to be. Instead of MyMessagesMDB.php
acting as both an initiator of the process and a recipient of data, it only needs to be an initiator.
In this implementation, mdb.php
acts as the script that would sit with your database, it's whole job is to take everything from the ContactMessages.ContactForm
query and output it as JSON.
<?php
$client = new MongoDB\Driver\Manager('mongodb srv://####:####@####.tldyu.mongodb.net/Data');
$filter = [];
$options = [];
$query = new MongoDB\Driver\Query($filter, $options);
$cursor = $client->executeQuery('ContactMessages.ContactForm', $query);
header('Content-Type: application/json');
echo json_encode($cursor);
Then you have get.php
, which you have on the server at school, which uses file_get_contents
to get the JSON, and then convert it back to a PHP array, which you can then output however you see fit.
<?php
$mdb_url = 'http://192.168.0.102/test/GetMessages.php';
$get = file_get_contents($mdb_url);
$decoded = json_decode($get, true);
foreach ($decoded as $document) {
echo $document['FirstName'] . '<br>' .
$document['LastName'] . '<br>' .
$document['Email'] . '<br>' .
$document['Subject'] . '<br>' .
$document['Message'] . '<br><hr>';
}