【问题标题】:PHP headers dont work with no good reason [duplicate]PHP标头没有充分理由不起作用[重复]
【发布时间】:2019-12-21 04:48:23
【问题描述】:

我正在将 CURL 查询从我的测试文件(以模仿将来来自另一个服务的 POST 请求)发送到我的工作文件。问题是我在收到 POST 请求后尝试重定向用户,但没有运气,既不是通过 JS 也不是通过 PHP。也许您可以看一下并告诉我错误在哪里,因为调试器或控制台或网络选项卡中没有错误?即 session_id - 会话在 User 类中开始,所以它工作正常。提前致谢

主文件代码:

require_once('../../api/classes/User.php');
header('Access-Control-Allow-Origin: *');
$user = new User();

if(isset($_POST['user_id']) && isset($_POST['game']) && isset($_POST['level'])){
            // echo "<script type='text/javascript'> window.location = '../pages/Games/".$_POST['game']."/game.php?token=".session_id()."&level=".$_POST['level'] . "</script>";
            header("Location: someurl/pages/Games/".$_POST['game']."/game.php?token=".session_id()."&level=".$_POST['level']);
}
?>

测试文件:

<?php

$ch = curl_init();
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$params = array(
    // "creditBalance"=>"4900",
    // "level"=>1,
    // "result"=>10,
    "user_id"=>"3974ac1f-ec60-c3ea-586e-e822fed8d326",
    "map"=>"",
    "game" => "PetroPro",
    "level" => "2"
    // "token"=>"u39ukhtcjffg86rdj8i011jd82"
);
curl_setopt($ch,CURLOPT_URL,"http://localhost/someurl/startSingleGame.php");
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,http_build_query($params));
$result = curl_exec($ch);
curl_close($ch);
?>

我希望在 test.php 发送请求之后,URL 应该从 test.php 更改为我在标头中发送的任何内容。

【问题讨论】:

  • PHP 不会在任何选项卡上的检查元素窗口中显示错误。这是为客户端错误/日志保留的
  • 这和 JS 有什么关系? o.O
  • 另外,header('Location ..') 后面应该跟 exit()
  • @GrumpyCrouton 我正在为 Visual Studio 使用 xDebug,它通常会显示标题是否有问题
  • @msg 看到了,试过了,没用 =(

标签: javascript php curl


【解决方案1】:

如果 curl 收到重定向响应,它不会重定向使用它的脚本
要进行重定向,您需要从 curl 响应中读取标头并输出 php 位置标头。

我建议使用 Postman 进行此类脚本/API 端点测试 https://www.getpostman.com/

【讨论】:

  • 天啊,我完全忘记了,我只是想训练 CURL 查询,完全忘记了我 PC 上的邮递员。谢谢提醒!
猜你喜欢
  • 1970-01-01
  • 2010-09-30
  • 2017-09-18
  • 2016-08-21
  • 2015-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多