I'm writing a wordpress plugin that integrates with MailChimp's API to store email addresses in a MailChimp list.
I have a 'store-address.php' that run's via AJAX on the submission of a form.
The plugin works when AJAX'ing the url on a local , or GoDaddy WordPress install. But does not work on my staging site wich is hosted on 'MediaTemple.net'.
When I make an ajax call to 'store-address.php' I receive this error...
Parse error: syntax error, unexpected { in /wp-content/plugins/plugin-name/mailchimp-api/inc/store-address.php on line 1
Here is my ajax function
$('#subscribe').submit(function(e) {
$.ajax({
url: $plugin_url '/plugin-name/mailchimp-api/inc/store-address.php',
data: 'ajax=true&email=' + escape($('#email').val()),
success: function(msg) {
$('#response').html(msg);
}
});
return false;
});
<?php if(session_id()==''){ session_start(); } function storeAddress(){ /* * Validation */ if(!$_GET['email']){ return "No email address provided"; } if(!preg_match("/^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*$/i", $_GET['email'])) { return "Email address is invalid"; } require_once('MCAPI.class.php'); /* * get MailChimp API details from the plugin settings stored in the session. */ $mcKey = $_SESSION['mc_api_key']; $mcID = $_SESSION['mc_list_id']; $api = new MCAPI($mcKey); $list_id = $mcID; if($api->listSubscribe($list_id, $_GET['email'], '') === true) { return 'Success! Check your email to confirm sign up.'; }else{ return 'Error: ' . $api->errorMessage; } } /* * If being called via ajax, autorun the function */ if($_GET['ajax']){ echo storeAddress(); } ?>
phpVersion 5.5
As I mentioned before this code works on a local environment and a goDaddy hosted site. Just not on MediaTemple I have also swept the code for any PHP syntax errors and I can't find anything.
Any help or point in the right direction would be a godsend. Thanks
The error was caused due to FileZilla's transfer type being set to "Auto", which disrupted linebreaks.
After switching the transfer type to "Binary" and restarting FileZilla, I re-uploaded the plugin and everything works great.
An unexpected $end
indicates a mismatch of {
curly braces }
and thus unclosed code or control blocks.
If the parser complains about line 1
, then this could only ever happen if your php script was indeed just a single line. The initial <?php
in line 1 couldn't possibly trigger this by itself.
And the only way for this to occur is for mismatched linebreaks. PHP cares about LF ( 0x0A
) only. If you're developing on classic Mac OS, or an editor which defaults to that, CR ( 0x0D
) might be used for linebreaks however. The old DOS/Windows combo of CR LF would also work. But that's not what you have.
In essence, while the code displays correctly in your editor, PHP will see it as:
<?php⏎if(session_id()=='')⏎{ session_start();⏎}⏎function storeAddress(){⏎// Validation⏎if(!$_GET['email']){ ...
And that's just it. The carriage returns CR take no effect. PHP will understand the first few statements, but the first comment // Validate
simply masks the rest of the code. Which is why the opened function declaration leads to a dangling " $end
".
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.