gpt4 book ai didi

php - Unirest Node 模块无法从PHP获取POST数据

转载 作者:太空宇宙 更新时间:2023-11-04 02:32:39 27 4
gpt4 key购买 nike

我正在尝试通过 PHP 获取从 unirest Node 模块发送的数据。这是我的 Node 代码:

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' }

对我做错的事情有什么帮助吗?

最佳答案

试试这个:

unirest.post('http://example.com/api')
.type('json')
.send({title: 'foo',body: 'bar',userId: 1})
.end(function (response) {
console.log(response.body);
})

关于php - Unirest Node 模块无法从PHP获取POST数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25195547/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com