hilarl hilarl - 1 month ago 19
Javascript Question

Getting a 500 error code while trying to work with the Mailchimp API using PHP and JQuery

I have been trying to work with the Mailchimp API to add contacts to a Mailchimp list. I am using JQuery to process the form and a PHP script to communicate with the Mailchimp API. But I was following the example at: https://github.com/drewm/mailchimp-api/blob/master/README.md

HTML:

<form name="SubsribeForm1" id="SubsribeForm1">
<input name="email" id="emailAdd" type="text" />
<p class="err"><span id="msg"></span></p>
<button id="SubscribeEmail">Submit</button>
</form>


JS code:

<script>
$('#reset-modal').click(function() {
$('#msg').html("");
});
$(document).on("click", "#SubscribeEmail", function(e){
e.preventDefault();
var emailAdd = $('input[name="email"]').val();
console.log(emailAdd);

$.ajax({
url: "subscribe.php",
data: $('#SubsribeForm1').serialize(),
dataType: "json",
timeout: 15000,
success: function(response){
switch(response.status){
case 'saved':
$('#msg').html(response.message);
break;
case 'empty':
$('#msg').html(response.message);
break;
default:
$('#msg').html(response.message);
}
},
error: function(response){
$('#msg').html(response.status);
}
});
});
</script>


and subscribe.php:

require("_mc.php");

use \DrewM\MailChimp\MailChimp;

$MailChimp = new MailChimp('7d446f0db3c6f9e4b5f9b6f35a587fad-us10');
$result = $MailChimp->post('lists/02801ed82a/members', array(
'email_address' => $_POST["email"],
'status' => 'subscribed'
));

if($result){
$status = $result["status"];

if($status == "subscribed"){
echo "Great! We will keep you updated on our progress.";
}
else{
echo "Oops! Something went wrong. Please try again.";
}
}


I keep getting a 500 error and am unable to identify the problem here.

Apache error log:

[Wed Oct 26 16:09:13.836172 2016] [:error] [pid 11669] [client 185.11.237.250:58101] PHP Fatal error: Uncaught exception 'Exception' with message 'cURL support is required, but can't be found.' in /var/www/html/_mc.php:110\nStack trace:\n#0 /var/www/html/_mc.php(48): DrewM\\MailChimp\\MailChimp->makeRequest('post', 'lists/02801ed82...', Array, 10)\n#1 /var/www/html/subscribe.php(11): DrewM\\MailChimp\\MailChimp->post('lists/02801ed82...', Array)\n#2 {main}\n thrown in /var/www/html/_mc.php on line 110, referer: http://meanwise.com/

[Wed Oct 26 16:09:17.195803 2016] [:error] [pid 11667] [client 185.11.237.250:58100] PHP Notice: Undefined index: email in /var/www/html/subscribe.php on line 9, referer: http://meanwise.com/
[Wed Oct 26 16:09:17.195942 2016] [:error] [pid 11667] [client 185.11.237.250:58100] PHP Fatal error: Uncaught exception 'Exception' with message 'cURL support is required, but can't be found.' in /var/www/html/_mc.php:110\nStack trace:\n#0 /var/www/html/_mc.php(48): DrewM\\MailChimp\\MailChimp->makeRequest('post', 'lists/02801ed82...', Array, 10)\n#1 /var/www/html/subscribe.php(11): DrewM\\MailChimp\\MailChimp->post('lists/02801ed82...', Array)\n#2 {main}\n thrown in /var/www/html/_mc.php on line 110, referer: http://meanwise.com/

[Wed Oct 26 16:09:31.813947 2016] [:error] [pid 11668] [client 185.11.237.250:58136] PHP Notice: Undefined index: email in /var/www/html/subscribe.php on line 9
[Wed Oct 26 16:09:31.814129 2016] [:error] [pid 11668] [client 185.11.237.250:58136] PHP Fatal error: Uncaught exception 'Exception' with message 'cURL support is required, but can't be found.' in /var/www/html/_mc.php:110\nStack trace:\n#0 /var/www/html/_mc.php(48): DrewM\\MailChimp\\MailChimp->makeRequest('post', 'lists/02801ed82...', Array, 10)\n#1 /var/www/html/subscribe.php(11): DrewM\\MailChimp\\MailChimp->post('lists/02801ed82...', Array)\n#2 {main}\n thrown in /var/www/html/_mc.php on line 110

[Wed Oct 26 16:09:36.833363 2016] [:error] [pid 31170] [client 185.11.237.250:58140] PHP Notice: Undefined index: email in /var/www/html/subscribe.php on line 9
[Wed Oct 26 16:09:36.833503 2016] [:error] [pid 31170] [client 185.11.237.250:58140] PHP Fatal error: Uncaught exception 'Exception' with message 'cURL support is required, but can't be found.' in /var/www/html/_mc.php:110\nStack trace:\n#0 /var/www/html/_mc.php(48): DrewM\\MailChimp\\MailChimp->makeRequest('post', 'lists/02801ed82...', Array, 10)\n#1 /var/www/html/subscribe.php(11): DrewM\\MailChimp\\MailChimp->post('lists/02801ed82...', Array)\n#2 {main}\n thrown in /var/www/html/_mc.php on line 110


After fixing the curl issue, I am getting the following error (I am not a PHP pro):

[Thu Oct 27 11:00:54.503907 2016] [:error] [pid 16992] [client 85.53.86.120:64765] PHP Notice: Undefined index: email in /var/www/html/subscribe.php on line 9, referer: http://meanwise.com/

Answer

At this moment your server doesn't support curl. To download curl with f.ex. Ubuntu and Apache server:

apt-get install php5-curl
/etc/init.d/apache2 restart

If you are using something like XAMPP uncomment line:

;extension=php_curl.dll

in xampp\apache\bin\php.ini or xampp\php\php.ini

And restart your Apache service.

There is no index email in your $_POST array. Check what it contents with echo $_POST, maybe you have made some mistake in spelling.