This is a piggy back off this question -- I've discovered some more information such that the question, itself, needed to change.
I'm attempting to pass data from javascript SPA to a php file (dbPatch.php
) to another php file (mongoPatch_backend.php
). dbPatch.php
is effectively acting as a middle-man to get data to appropriate servers.
My javascript fetch looks like this:
const API = PHP_FILE_LOCATION + 'dbPatch.php/';
const query =
"needleKey=" + encodeURIComponent(needleKey) + "&" +
"needle=" + encodeURIComponent(needle) + "&" +
"newData=" + encodeURIComponent(JSON.stringify(newData));
let URI = API;
fetch(URI, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: query
}).then.... blah...blah....
This calls my php file, dbPatch...
<?php
$API = "https://SERVER/php/mongoPatch_backend.php?";
$needleKey = $_REQUEST['needleKey'];
$needle = $_REQUEST['needle'];
$newData = $_REQUEST['newData'];
$postData = "needleKey=".urlencode($needleKey);
$postData .= "&needle=".urlencode($needle);
$postData .= "&newData=".urlencode($newData); //THIS IS THE LINE I TALK ABOUT BELOW
$data = file_get_contents($API.$postData);
echo $data;
?>
which in turn calls my mongoPatch_backend.php
file...
<?php
$user = "xxx";
$pwd = 'xxx';
$needleKey = $_REQUEST['needleKey'];
$needle = $_REQUEST['needle'];
$filter = [$needleKey => $needle];
$newData = $_REQUEST['newData'];
$filter = ['x' => ['$gt' => 1]];
$options = [
'projection' => ['_id' => 0],
'sort' => ['x' => -1],
];
$bson = MongoDB\BSON\fromJSON($newData);
$value = MongoDB\BSON\toPHP($bson);
$manager = new MongoDB\Driver\Manager("mongodb://${user}:${pwd}@DBSERVER:27017");
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->update(
[$needleKey => $needle],
['$set' => $value],
['multi' => false, 'upsert' => true]
);
$results = $manager->executeBulkWrite('dbname.users', $bulk);
var_dump($results);
?>
This does not work.
If I call mongoPatch_backend.php directly from the javascript, it DOES work. This leads me to believe the problem is in the passing of the data located in the dbPatch.php file.
Further, if I call dbPatch with different 'newData' (shorter) it DOES work. This leads me to believe it's something with the data being passed in (but remember, it works if I call directly... so it's right coming out of the javascript).
Spitting out $newData from dbPatch.php via var_dump($_REQUEST['newData']);
gives me JSON data which has been stringified but it is not character-escaped. It's about 5,000 characters.
Here is the interesting part.
If I change mongoPatch_backend.php to JUST <?php echo "Hello World"; ?>
I STILL do not get anything passed back through dbPatch.php to my SPA. This REALLY makes me think something is wrong in the dbPatch.php file.
So... I comment out the $postData .= "&newData=".urlencode($newData);
line from the dbPatch.php ... I DO get the "Hello World" back.
if I just remove .urlencode and instead just have $postData .= "&newData=".$newData;
I still get nothing back.
So the problem seems to be with putting $newData in my post. The mongoPatch_backend.php is not even doing anything with the $newData... dbPatch.php (it appears) is simply having trouble sending that data.
Unfortunately... I"m not sure where to go from here... given, I do, indeed, need to send the $newData to the backend.
EDIT: In reponse to suggestions that I use "POST" instead of "GET" I did a search and found this Stack question: POST data to a URL in PHP
From that, I now have this:
dbPatch.php:
$url = 'https://SERVERNAME/php/mongoPatch_backend.php';
$myvars = 'myvar1=' . "TEST" . '&myvar2=' . "ALSOTEST";
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $myvars);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec( $ch );
echo $response;
and I changed my mongoPatch_backend.php
to:
<?php
echo "HELLO WORLD";
?>
... and I get nothing as the response. (that is, I do not get "HELLO WORLD" form the backend).
My PHP log shows no errors.
My curl config from phpinfo()
is:
cURL support enabled
cURL Information 7.59.0
Age 4
Features
AsynchDNS Yes
CharConv No
Debug No
GSS-Negotiate No
IDN Yes
IPv6 Yes
krb4 No
Largefile Yes
libz Yes
NTLM Yes
NTLMWB No
SPNEGO Yes
SSL Yes
SSPI Yes
TLS-SRP No
HTTP2 Yes
GSSAPI No
KERBEROS5 Yes
UNIX_SOCKETS No
PSL No
Protocols dict, file, ftp, ftps, gopher, http, https, imap, imaps, ldap, pop3, pop3s, rtsp, scp, sftp, smb, smbs, smtp, smtps, telnet, tftp
Host x86_64-pc-win32
SSL Version OpenSSL/1.1.0h
ZLib Version 1.2.11
libSSH Version libssh2/1.8.0