![postman 调用本地 php api 时,为何找不到 $_server[\'http_origin\']?](https://img.php.cn/upload/article/001/246/273/173003419680824.jpg)
postman 调用本地 php api 时出现“notice: undefined index: http_origin”错误,是因为 origin 头是浏览器 cors 中用来区分请求来源的,而其他环境不受其限制。
要模拟 origin 头,在 postman 的 headers 标签中添加一个名为 origin 的自定义头,并将值设置为网站的 url(格式为 <schema>://<host>[:<port=80>])。例如,对于 https://example.com,origin 值应为 https://example.com。
由于不是所有用户代理都会发送 origin 头,因此在 php 中应使用 isset() 或 ?? 操作符检查其是否存在后再取用。例如:
$origin = $_server['http_origin'] ?? '';
或
立即学习“PHP免费学习笔记(深入)”;
$origin = '';
if (isset($_SERVER['HTTP_ORIGIN'])) {
$origin = $_SERVER['HTTP_ORIGIN'];
}以上就是Postman 调用本地 PHP API 时,为何找不到 $_SERVER['HTTP_ORIGIN']?的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号