在PHP方面,从axios对象获取所有键=>值对的正确语法是什么,这些对象被发布到简单的PHP邮件程序中?
我的目标:
let post = {
message: this.message, // string
email: this.email, // string
links: this.links // array of objects { name: item.name, id: item.id }
};
axios.post('/mail.php', JSON.stringify(post)).then(() => { ... });
在我的邮件中,我正在这样做:
$_POST = json_decode(array_keys($_POST)[0], true);
if (isset($_POST['message'])) {
$body = $_POST['message'];
}
if (isset($_POST['email'])) {
$email = $_POST['email'];
}
if (isset($_POST['links'])) {
$links = $_POST['links'];
}
但是这并不能得到全部...我想是因为[0]
,但如果我删除[0],则json_decode会抱怨。 (我还不是非常擅长PHP)。
我会爱是这样分配var的:
$message = json_decode(array_keys($_POST)[0], true);
$email = json_decode(array_keys($_POST)[1], true);
$links = json_decode(array_keys($_POST)[2], true);
但是那也不起作用。
我从axios帖子中删除了stringify,然后在我使用的PHP邮件程序中删除了:
if (isset($_POST)) {
$data = json_decode(file_get_contents('php://input'), true);
}
if (isset($data['email'])) {
$to = $data['email'];
}
if (isset($data['message'])) {
$body = $data['message'];
}
if (isset($data['links'])) {
$to = $data['links'];
}