如何使用html post表单将数据发布到外部服务器后提取php响应查询字符串?

时间:2015-07-16 08:18:55

标签: php string forms

如何使用html post表单将数据发布到外部服务器后提取php响应查询字符串?

页: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>

我认为结果是xml,因为开发人员指南显示了

响应

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

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

如何获得回复?请在代码中显示。响应代码可以在delivery.php吗?

3 个答案:

答案 0 :(得分:1)

如果您要处理对远程服务器的帖子的响应,您必须在服务器端执行发布,而不是客户端(浏览器)。

所以这样的想法就是这个想法:

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

因此,您向用户显示一个页面,他可以在其中填写所需数据,然后将其发布到服务器上的表单处理程序页面。该处理程序页面可以向远程服务器发出请求并解析响应,它可以为客户端生成适当的答案。

例如,您可以使用CURL在服务器端发出远程请求。

一个例子是这样的: (CURL示例基于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
}
?>

这个例子并不完整,但希望能给你一个想法。

答案 1 :(得分:1)

我的印象是您只是在学习编程,但您希望使用http://www.resellerclub.com/domain-reseller/api的服务向客户销售产品。

一种选择是让客户提交直接转发给经销商俱乐部的表格,但他们的回报并不那么友好。另一种方法是让您的用户向您的服务器提交表单,然后您的服务器向resellerclub.com提出请求。 (通过CURL)您将收到经销商俱乐部的回复(强烈推荐http JSON选项!),您可以随心所欲地使用该数据(通常会向网站用户提供回复,感谢他们提交)

为此,您必须将表单action=更改为action="darrens_server.com/payment_submission.php",并在该文件中将您向resellerclub提交内容,等待回复,并将结果回显给网站用户。

我会说,如果涉及信用卡,这可能不是最佳方式。一般情况下,我避免处理信用卡交易,我想要一家货币公司为我做这件事(例如paypal)我不需要承担错误的责任或我公司的任何人甚至可以看到用户的信用卡号码。在这种情况下,整个处理过程真的取决于resellerclub.com。你需要遵守他们的规则。

祝你好运。

答案 2 :(得分:0)

将此表单提交给外部服务器后,响应将发送到您的浏览器,而不是发送到您的服务器。