通过NodeJS将对象传递给PHP
我试图通过两个键/值传递对象,我想在另一端读取它,当我连接到Apache。
我的nodejs服务器看起来像这样:
var sWeather = {"url1": "something", "url2": "another"}; var oWeather = JSON.stringify(sWeather); console.log(sWeather); var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end(sWeather+'\n'); }).listen(1340, '#'); console.log('Server running at http://###/');
现在,我对nodejs真的很陌生,而且我已经尝试了很多东西,所以我不确定在发送它之前是否需要将sWeather
进行string化。
我的PHP文件是这样的:
<?php $responseFromNode = file_get_contents("IP address"); var_dump($responseFromRuby);
现在,由于var_dump
原因,我在网页上得到了string '[object Object]
“。
我试过做类似的事情
$url1 = $responseFromNode->url1
要么
$url1 = $responseFromNode['url1']
我只想访问这两个url作为一个string,所以我可以存储它。
任何提示将不胜感激。
oWeather
是JSONstring。 更改
res.end(sWeather+'\n');
至
res.end(oWeather+'\n');
然后PHP端必须解码JSON
$responseFromNode = json_decode( file_get_contents("IP address") );
笔记:
- 你也应该改变你的内容types:
res.writeHead(200, {'Content-Type': 'application/json'});
正如Brian Glaz所提到的那样 - 你的variables名是倒置的:
-
oWeather
应该是对象 -
sWeather
应该是JSONstring
-