我正在尝试通过PHP从unirest节点模块发送数据。这是我的节点代码:
unirest.post('http://example.com/api')
.headers({ 'Accept': 'application/json' })
.send({title: 'foo',body: 'bar',userId: 1})
.end(function (response) {
console.log(response.body);
});
我的PHP页面正在捕捉我能想到的所有内容,但我无法从send方法中获取数据。
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
header('Accept: application/json');
$message = array();
$raw = file_get_contents('php://input');
$message['request'] = $_REQUEST;
$message['raw_input'] = $raw;
$message['post'] = $_POST;
$message['get'] = $_GET;
$message['server'] = $_SERVER;
$message["username"] = 'myname';
$message["password"] = 'password';
echo json_encode($message);
PHP的示例响应如下:
{ request: [],
raw_input: [],
post: [],
get: [],
server:
{ CONTENT_LENGTH: '0',
DOCUMENT_ROOT: '/home/example/public_html',
GATEWAY_INTERFACE: 'CGI/1.1',
HTTP_ACCEPT: 'application/json',
HTTP_CONNECTION: 'keep-alive',
HTTP_HOST: 'example.com',
PATH: '/bin:/usr/bin',
QS_ConnectionId: '14074674016495001612072',
QUERY_STRING: '',
REDIRECT_STATUS: '200',
REMOTE_ADDR: 'XXXXXXXXXXXXX',
REMOTE_PORT: '58851',
REQUEST_METHOD: 'POST',
REQUEST_URI: '/api/',
SCRIPT_FILENAME: 'XXXXXXXXXXX',
SCRIPT_NAME: 'XXXXXXXXXXXX',
SERVER_ADDR: 'XXXXXXXXXXXX',
SERVER_ADMIN: 'XXXXXXXXX',
SERVER_NAME: 'XXXXXXX',
SERVER_PORT: '80',
SERVER_PROTOCOL: 'HTTP/1.1',
SERVER_SIGNATURE: '',
SERVER_SOFTWARE: 'Apache',
UNIQUE_ID: 'U@Q-iUXC5AMAAC8oJK8AAADY',
PHP_SELF: 'XXXXXXX',
REQUEST_TIME: 1407467401,
argv: [],
argc: 0 },
username: 'myname',
password: 'password' }
对我做错的任何帮助?
答案 0 :(得分:0)
试试这个:
unirest.post('http://example.com/api')
.type('json')
.send({title: 'foo',body: 'bar',userId: 1})
.end(function (response) {
console.log(response.body);
})