D
D
Dmitry Tarasov2020-01-24 13:11:51
Laravel
Dmitry Tarasov, 2020-01-24 13:11:51

How to correctly send a request from nodejs to php script?

I am doing authorization on nodejs through puff, all the code is here .

requestify.get('https://site.com/check-auth.php', {
            dataType: 'json',
            headers: {
                'Cookie': socket.request.headers["cookie"],
                'User-Agent': socket.request.headers["user-agent"],
                'X-Requested-With': 'XMLHttpRequest',
                // 'Content-Type': 'application/json',
            }
        }).then(function(response) {
            // console.log(response);
            var user = JSON.parse(response.getBody());
            console.log('user: ', user);
            socket.handshake.user = user;
            next();
        }, function(err){
            console.log(err);
            next(new Error('not authorized'));
        });

Gives errors like this
{ Error: connect ECONNREFUSED 127.0.1.1:443
    at Object._errnoException (util.js:992:11)
    at _exceptionWithHostPort (util.js:1014:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1186:14)
  code: 'ECONNREFUSED',
  errno: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.1.1',
  port: 443 }

Puff code
startSession();

$user = [];

try {
    if(isset($_SESSION["username"]) and isset($_SESSION["password"]) and isset($_SESSION['ID_USER'])){
        $user['id'] = $_SESSION['ID_USER'];
    } else {
        throw new Exception('not authorized');
    }
    echo json_encode($user);
} catch (Exception $e) {
    http_response_code(404);
    echo json_encode(['error' => $e->getMessage()]);
}

Through the browser I run the php script, everything works okay.
What could be the problem ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
JhaoDa, 2019-04-23
@semki096

Have you read the documentation?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question