How to extract the response string from php after posting data to an external server using the html publishing form?

advertisements

How extract php response query string after posting data to external server using html post form?

page:delivery.php

<form name="form2" method="post" action="http://dev.site.com/pay/transactionresult" id="form1">
    <input type="hidden" value="<?php echo $amt;?>" name="amt" />
    <input type="hidden" name="merchant" value="abc">
    <input type="hidden" name="orderid" value="<?php echo $orderid;?>">
    <input type="hidden" name="referenceid" value="<?php echo  $referenceid;?>">
    <input type="submit" value="submit"/>
</form>

I think the outcome is xml as the developer guide shows

Response

<response>
    <response_code>
        success
    </response_code>
</response>

<response>
    <response_code>
        failure
    </response_code>
</response>

How to get the response ? Please show in code. can the response code be in the delivery.php?


IF you want to handle the response to a post to a remote server you have to perform the post on the server side, not by the client (browser).

So something like this would be the idea:

client --POST--> your server --response--> client
                  |      ^
                 POST    |
                  |   response
                  V      |
                remote server

So you show a page to the user where he can fill in the required data then post it to a form handler page on your server. That handler page can make the request to the remote server and parsing the response it can generate the appropriate answer to the client.

You can use CURL for example to make the remote request on the server side.

An example would be something like this: (CURL example based on an other SO answer)

<?php
if (empty($_POST['amt'])) {
  // show form
} else {
    $data = ...; // collect and filter data from form

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL,"http://remote.example.com/transaction");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

    // receive server response ...
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $server_output = curl_exec ($ch);

    curl_close ($ch);

    // further processing ....
    if ($server_output == "OK") {
        // parse $server_output
        $message = 'success';
    } else {
        $message = 'error';
    }

    // show $message to user
}
?>

This example by no means complete but hopefully gives you an idea.