【发布时间】:2020-05-29 00:56:48
【问题描述】:
在 PHP 方面,从发布到简单 PHP 邮件程序的 axios 对象中获取所有 key=>value 对的正确语法是什么?
我的对象:
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)。
我希望 喜欢 是这样分配变量:
$message = json_decode(array_keys($_POST)[0], true);
$email = json_decode(array_keys($_POST)[1], true);
$links = json_decode(array_keys($_POST)[2], true);
但这也不起作用。
【问题讨论】:
-
你不能像普通邮件一样发送吗?我认为 stringify 增加了更多的复杂性,然后像往常一样访问 post 键,我只是阅读文档 github.com/axios/axios 它不需要是
stringified,只需按原样发送对象 -
并且您的预期分配(理想情况下您想要的分配)将不可能,因为有效负载是作为整个 json 字符串发送的,您不能单独重新应用它。你使用
json_decode,然后访问数据
标签: javascript php axios phpmailer