简体   繁体   English

PHP:将表单数据发送到php脚本,然后将数据发布到另一个表单处理器(例如zapier.com)吗?

[英]PHP: Send form data to php script which then posts data to another form processor like zapier.com?

I use a 3rd party form processor which is zapier.com. 我使用的是zapier.com的第三方表单处理程序。 The issue is, I need a way to redirect the user after submitting data to the 3rd party form processor. 问题是,在将数据提交给第三方表单处理器之后,我需要一种重定向用户的方法。 Zapier.com accepts post, get, and put submissions. Zapier.com接受发布,获取和放置提交。

I was thinking of: 我在想:

  1. Client submits form 客户提交表格
  2. My php form-processor captures the data 我的PHP表单处理器捕获数据
  3. My php form-processor then Submits the data via POST or GET to the 3rd party form processor 我的php表单处理器然后通过POST或GET将数据提交给第三方表单处理器
  4. My php form-processor then redirects the user to a thank you page. 然后,我的PHP表单处理器将用户重定向到“谢谢”页面。

I might be over thinking this, but the only way I see of doing this is making a form that posts data that has been posted to it. 我可能会对此有所考虑,但是我看到的唯一方法是制作一个表单,该表单发布已发布到其上的数据。 Otherwise the form will just send my user to the 3rd party processor without redirecting them to whatever page I choose. 否则,该表单只会将我的用户发送到第三方处理器,而无需将他们重定向到我选择的任何页面。 The 3rd party form processor doesn't have a way of me using custom redirects. 第三方表单处理器无法使用自定义重定向。

Using javascript and Ajax it can be done like this (with jQuery): 使用javascript和Ajax可以做到这一点(使用jQuery):

$("#idOfTheForm").submit(function(e) {
    e.preventDefault();
    $.ajax({
        method : "post",
        url : this.action,
        data : $(this).serialize(),
        success : function() {
            window.location = "yourUrlOfThanks.html";
        },
        error : function() {
            alert("Something went bad");
        }
    });
});

So basically it is: sent a post request to the action url of this form, and once it throws an 200 code (found and everything went ok) then redirect to my page. 基本上就是这样:向此表单的操作网址发送一个发布请求,一旦它抛出200个代码(找到并一切正常),然后重定向到我的页面。 If soemthing went wrong, then the server will throw an 40* status code and the script will go into error function. 如果发生错误,则服务器将抛出40 *状态代码,并且脚本将进入错误功能。

You can use Guzzle to proxy the user request. 您可以使用Guzzle 代理用户请求。

    $client = new Guzzle\Http\Client();

    $request = $client->post('/3rd.party.php')
        ->addPostField('user_field_1', $_POST['user_field_1'])
        ->addPostField('user_field_2', $_POST['user_field_2']);

    $response = $request->send();

    if ($response->isSuccessful()) {
        //show message
    }

The downside is that you can't be 100% sure that the form submission was indeed successful. 缺点是您不能100%确定表单提交确实成功。 In order to achieve that you could scrap the $request->getBody() and check if a known success message is present. 为了实现此目的,您可以$request->getBody()并检查是否存在已知的成功消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM