0

I'm sending CURL query from my test file (to imitate POST request from another service in future) to my working file. The thing is I'm trying to redirect user after I've received that POST request but no luck, neither through JS nor through PHP. Maybe you can take a look and tell me where is the bug because there is no errors neight in debuger or consol or network tab? i.e. session_id - session starts in User class so it works fine. Thanks in advance

Main file code:

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']);
}
?>

Test file:

<?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);
?>

I expect that after test.php will send a request the URL should be changed from test.php to whatever I'm sending in headers.

MightyMike
  • 34
  • 5

1 Answers1

0

If curl receives a redirect response, it doesn't redirects the script in which it is used
To make the redirect, you need to read the headers from curl response and output a php location header.

I recommend to use Postman for this type of script/API end point testing https://www.getpostman.com/

  • OMG, I completely forgot about it, I just wanted to train CURL queries and completely forgot about postman on my PC. Thx for reminder ! – MightyMike Aug 14 '19 at 16:35