简体   繁体   English

如何将数据从托管在 Heroku 上的 Node.js 应用程序发送到托管在完全独立的(Cpanel)服务器上的 PHP 文件?

[英]How can I send data from a Node.js application hosted on Heroku to a PHP file hosted on a completely separate (Cpanel) server?

I can't seem to find a good answer to this question anywhere after searching for over 30 minutes.在搜索了 30 多分钟后,我似乎无法在任何地方找到这个问题的好答案。 I hope this hasn't been asked already.我希望这还没有被问到。

So, I want to build an online game.所以,我想建立一个在线游戏。 It will be a very simple online game (mostly for practice).这将是一个非常简单的在线游戏(主要用于练习)。 I want to host this game on Heroku, and I will code the game using Node.js.我想在 Heroku 上托管这个游戏,我将使用 Node.js 对游戏进行编码。 Now, I have a separate server (a shared server running Apache and Cpanel) where I will want to store all the permanent data for the game (in a MySQL database).现在,我有一个单独的服务器(一个运行 Apache 和 Cpanel 的共享服务器),我想在其中存储游戏的所有永久数据(在 MySQL 数据库中)。 So what I need to do is serve up the game using PHP on my Apache web server, allow users to create accounts/sign into accounts on the Apache server (using a MySQL database also located there), then I want them to be able to "join a room" and when they do that, I want to send them off to the Node.js server running on Heroku, and allow that server to handle everything related to gameplay (through WebSockets). So what I need to do is serve up the game using PHP on my Apache web server, allow users to create accounts/sign into accounts on the Apache server (using a MySQL database also located there), then I want them to be able to “加入房间”,当他们这样做时,我想将它们发送到在 Heroku 上运行的 Node.js 服务器,并允许该服务器处理与游戏相关的所有事情(通过 WebSockets)。 Now, after they're finished with their game, I want to save their scores and some other related data about them into my MySQL database over on my Apache server... Is there a way I can do this?现在,在他们完成游戏后,我想将他们的分数和其他一些关于他们的相关数据保存到我的 MySQL 数据库中,在我的 Apache 服务器上......有什么办法可以做到这一点吗? Is there any means by which I can send data from a Node.js server running on Heroku to a PHP file on my Apache server?有什么方法可以将数据从在 Heroku 上运行的 Node.js 服务器发送到 PHP 服务器上的 PHP 文件? (For example, handlepostgame.php or something similar) (例如,handlepostgame.php 或类似的东西)

I hope this is a clearly written question and that you're able to easily understand what I'm asking.我希望这是一个写得很清楚的问题,并且您能够轻松理解我在问什么。 If not, let me know and I'll try to make it more clear.如果没有,请告诉我,我会尽量让它更清楚。 Thanks in advance.提前致谢。

Thank you to @ADyson for pointing me in the right direction!感谢@ADyson 为我指明了正确的方向!

So, I found the answer that suits my needs.所以,我找到了适合我需要的答案。 This is my "requesttophp:js" file: (Node.js)这是我的“requesttophp:js”文件:(Node.js)

var http = require('http');

console.log("running");

var query = "something=cookie";
var queryLength = query.length;

var options = {
  host: 'www.mydomain.com',
  path: '/path/to/fromnodejs.php',
  method: 'POST',
  headers:{
    'Content-Type': 'application/x-www-form-urlencoded',
    'Content-Length': queryLength
  }
};

callback = function(response) {
  var str = ''
  response.on('data', function (chunk) {
    str += chunk;
  });

  response.on('end', function () {
    console.log(str);
  });
}

var req = http.request(options, callback);
req.write(query);
req.end();

respondtonodejs.php: (PHP)响应nodejs.php:(PHP)

<?php

echo "it worked\n";
echo $_POST["something"];

?>

The result is结果是

running
it worked
cookie

It does exactly what I want: .) Thank you.它正是我想要的:。)谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 php 文件从托管在服务器中的 MariaDB 中获取数据? - How can I bring data from MariaDB hosted in a server using a php file? 如何访问cpanel托管的PHP文件 - How to access cpanel hosted PHP file 如何在 Node.js 应用程序发送的 php 中获取 cookie - How to get cookie in php send from Node.js application 如何运行包含来自heroku托管的php库的python脚本? - How can I run the python script which contain libraries from php hosted on heroku? 如何使用托管在不同服务器上的PHP操作从Google广告表单发送数据 - How to send data from Google Ad form using php action hosted in different server 如何从Marklogic Server上托管的应用程序运行php代码? - How to run php code from an application hosted on Marklogic Server? 如何访问服务器上托管的PHP文件? - How do I access a PHP file hosted on a server? 从Node.js服务器到PHP服务器的安全文件上传 - Secure file upload from Node.js server to PHP server 为什么我不能在我的托管域中使用 swiftmailer 发送邮件,但是从我的本地主机服务器发送邮件时没有错误 - Why can I not send mail with swiftmailer in my hosted domain but there is no error when sending it from my localhost server 我可以使用远程托管的 phpmailer 文件从静态 html 页面发送电子邮件吗? - Can I use a remotely hosted phpmailer file to send email from static html page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM