How to redirect to another server in PHP

0

I am using a service in PHP as a Redsys payment gateway in which it connects, performs card payment and obtains a response code.

This answer needs to be redirected to another external server with the order code to notify the response given by this service.

The answer must be sent by POST method but I do not know how to do it since I am not using any form, I simply receive the data by POST and that same POST I want to redirect it to another external server.

Greetings.

    
asked by F Delgado 09.02.2018 в 09:02
source

1 answer

1

I can think of three ways:

* Curl
* Generas un form dinámico
* Petición AJAX

In curl mode it would be:

<?php
    $url = 'http://domain.ltd/';
    $fields = array(
        'variable' => urlencode($_POST['valor'])
    );
    foreach($fields as $key => $value) {
        $fields_string .= $key .'=' . $value . '&';
    }
    rtrim($fields_string, '&');
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, count($fields));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
    $result = curl_exec($ch);
    curl_close($ch);
?>

Generate a dynamic form (Here I would do it with jQuery):

    $('<form action="http://domain.ltd/"><input type="text" name="variable" value="valor"></form>').appendTo('body').submit();

In AJAX request (jQuery again):

$.ajax({
    method: "POST",
    url: "http://domain.ltd",
    data: {
        variable: "valor"
    }
});

There may be more ways, but this is the basics.

    
answered by 09.02.2018 / 09:35
source