使用cURL来POST形式到两个地方 [英] Using cURL to POST form to two places

查看:150
本文介绍了使用cURL来POST形式到两个地方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天我问一个问题,关于我如何去劫持我的MailChimp联系表单发送额外的电子邮件(当满足某些条件时)。

Yesterday I asked a question about how I could go about hijacking my MailChimp contact form to send an additional email (when certain conditions are met).

从MailChimp URL到我自己的 process_email.php action =... ,代码类似于以下内容: / p>

I wrote changed the action="... from the MailChimp URL to my own process_email.php with code resembling the following:

extract($_POST);
$url = 'http://myMailchimpUser.us2.list-manage.com/subscribe/post';
$fields = array(
    'u' => urlencode($u),
    'id' => urlencode($id),
    'group' => http_build_query ($group),
    'MERGE1' => urlencode($MERGE1),
    'MERGE2' => urlencode($MERGE2),
    'MERGE3' => http_build_query($MERGE3),
    'other_more_info_text' => urlencode($other_more_info_text),
    'submit' => urlencode($submit)
);

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);

我还没有添加发送电子邮件给自己的代码,但这应该是微不足道的。我使用这个代码的问题是,而不是重定向我到MailChimp页面,它实际上加载 process_email.php (及其加载不正确的引导。)

I have yet to add the code which sends the email to myself, but that should be trivial. The problem I'm having with this code is that instead of redirecting me to the MailChimp page, its actually loading within process_email.php (and its loading incorrectly to boot.)

我知道如果我使用JavaScript,我可以完成我想做的,但在我看来,这不是正确的方式对这个。任何人都可以给我任何帮助?

I know I can accomplish what I want to do if I used JavaScript, but it seems to me that isn't the proper way to go about this. Can anyone offer me any help?

推荐答案

如果我理解正确:你想POST数据本地首先, Mailchimp。如果这是你想要做的,那么使用一些JS连接到窗体(或窗体按钮)可能是最好的方式去。

If I understand correctly: you want to POST data locally first then let the form POST to Mailchimp. If that is what you are trying to do, then using some JS connected to the form (or a form button) is probably the best way to go. I think it is the proper way to go in your situation.

下面的jQuery可以用来在本地首先POST表单,并且一次该请求已完成,它将使用给定的操作url(mailchimp)提交表单。

Something jQuery like below would work to POST the form locally first, and once that request is complete it would submit the form using the given action url (mailchimp).

$(document).ready(function(){
    $('#submit-button').click(function(event){
        event.preventDefault();
        $.post("your_email_parser.php", $('#form-id').serialize(), function(){
            $('#form-id').submit();
        });
    });
});

...

<form id='form-id' action='http://myMailchimpUser.us2.list-manage.com/subscribe/post' method='post'>
    ...
    <input type='submit' id='submit-button' />
</form>

这篇关于使用cURL来POST形式到两个地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆