Trouble POSTing Form Fill Data to Mailwizz API & a Ping Tree at Same Time

Dubbsy

New Member
Hey All,

I have a lead generation business and I'm trying to post the form fill data to my ping tree (with cURL) and then also to Mailwizz DB with the PHP sdk example provided by Twisted.

However it's not working... I can process each of them separately, but when I combine them on the same file it gives me a 200 response but then {error: code:200} or a 400 error as well.

This is my cURL and API code:

PHP:
<?php
$email = $_POST['email'];
$first = $_POST['first_name'];
$last =  $_POST['last_name'];
/* removed sensitive info */


$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'removed sensitive info');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$post = array(
    'key' => "emoved sensitive info ",
    'campaign' => "removed sensitive info",
    'email' => $email,
    'first_name' => $first,
    'last_name' => $last
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);


if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
$result = curl_exec($ch);

$ret = json_decode($result);

$code = $ret->code;
echo $code;
//exit;
curl_close($ch);

if($code == 200){

    // require the setup which has registered the autoloader
    require_once dirname(__FILE__) . '/setup.php';

    //create the endpoint
    $endpoint   = new MailWizzApi_Endpoint_ListSubscribers();

    // ADD SUBSCRIBER
    $response = $endpoint->create('removed sensitive info', [
        'EMAIL'    => $email, // the confirmation email will be sent!!! Use valid email address
        'FNAME'    => $first,
        'LNAME'    => $last
    ]);

    $response = $response->body;

    $result = $response->itemAt('status');
    echo $result;

}
$data = array();
$data['code'] = 200;
$data['data'] = "email=$email&first_name=$first&last_name=$last";
echo json_encode($data);
exit;

Here is the form js processing code as well:

Code:
<script type="text/javascript">
    $("#msForm").validate({
        rules:{
            first_name:"required",
            last_name:"required",
            email:{email: true,required: true}

        },

        messages: {
            first_name:"This field is required",
            last_name:"This field is required",
            email:{email:"A Valid Email is required",
                required:"Email address is required"
                }
        },

        submitHandler: function(form){
            $('#submitBtn').val("Working...");
            $.ajax({
                url: "curl_and_api.php",
                type: 'POST',
                data: $('#msForm').serialize(),
                success: function(result){
                    var res = $.parseJSON(result);
                    if(res.code == 200) {
                        window.location.href = '../thankyou.php?'+res.data;
                        return true;
                    } else {
                        $(".span7").show();
                        enableButton();
                    }
                }
            });           
        }
});


</script>

Any help would be greatly appreciated!

thanks Mailwizz fam!
 
but when I combine them on the same file it gives me a 200 response but then {error: code:200} or a 400 error as well.
What do you mean by that? The PHP file should reside on the server and it will be processed there, and the JS part should reside in your HTML that contains the form and it will be processed on the client side.

Cosmin
 
What do you mean by that? The PHP file should reside on the server and it will be processed there, and the JS part should reside in your HTML that contains the form and it will be processed on the client side.

Cosmin
The html and form code is on a separate file than the PHP code that processes the POST request.

What I mean is that I can process the post request for mailwizz api (alone, without the cURL code) and it works... and then I can also process the post request with the cURL code for my ping tree software (alone, without the mailwizz api code) and it works.

but when I combine the cURL and the mailwizz API, it doesn't work.

Hope that clears it up.
 
Hello,
Once you are echoing something, that will be the response for the ajax call made. So remove all echos except the last one.

Cosmin
 
ok the CURL header needs to be set with API key
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-API-KEY:7ZZZZZZZZZZWWWWWWEEE44444444GGGGGGGG'
));
 
Back
Top