4

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


And my 'store-address.php' looks like this.
<?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

  • Is this being included via `include` or `require` in another file? – elixenide Jan 27 '15 at 05:14
  • No, I'm calling the file with jQuery AJAX in the function described above – Curtis Blanchette Jan 27 '15 at 05:20
  • Yeah, I see that, but I also see that this is a Wordpress site, which means a lot of other stuff is running before you get this far. The error `unexpected $end` in PHP means that a brace, parenthesis, or bracket is unmatched somewhere in your code; I'm betting it's actually before you get to this file. – elixenide Jan 27 '15 at 05:23
  • might be a dumb suggestion, but can you try putting `session_start();` inside `storeAddress()`? – Reigel Gallarde Jan 27 '15 at 05:26
  • "Line 1" indicates incorrect linebreaks. When developing on a Mac, you'll have to save the file with LF instead of CR. – mario Jan 27 '15 at 05:27
  • Thanks @EdCottrell, although it's weird that it works fine on my local environment with a clone of the staging site. – Curtis Blanchette Jan 27 '15 at 05:46
  • That is weird. Same versions of PHP, Wordpress, the theme, all child themes, and all plugins? – elixenide Jan 27 '15 at 05:47
  • **Update** @EdCottrell my php versions are actually different, server is running 5.3 and my local environment is running 5.5 – Curtis Blanchette Jan 27 '15 at 06:20
  • That could do it. Any chance you can update the version on the server? 5.3 is really outdated. 5.3.0 came out in 2009! Wordpress and most WP themes and plugins will do better on a more current platform. – elixenide Jan 27 '15 at 06:28
  • I'm crossing my fingers, just need to get access to the host to update the version in media temple. – Curtis Blanchette Jan 27 '15 at 06:31
  • by any chance you want to change your implementation?? why not use `admin-ajax.php` instead? – Reigel Gallarde Jan 27 '15 at 06:56
  • @EdCottrell, updated to php5.5, issue still persists. – Curtis Blanchette Jan 27 '15 at 07:02
  • @CurtisBlanchette Hmm... Did you check mario's comment about line endings? You might also double-check your wp-config.php file. Have you checked the server's logs to see if they contain more detail? – elixenide Jan 27 '15 at 12:56
  • @mario I am developing on a mac, with SublimeText although I have never run across this before, can you elaborate on LF and CR? – Curtis Blanchette Jan 27 '15 at 17:10
  • Use a hexeditor and check. See http://en.wikipedia.org/wiki/Newline. Albeit that's more likely for classic Mac OS. Ever since OS X, and with Sublime, you should get LFs per default. Anyway, convert with `php -w` to a comment-free script for testing. – mario Jan 27 '15 at 17:13
  • @mario, tried uploading a comment free script, I've never had any issues in the past with php developed on my mac – Curtis Blanchette Jan 27 '15 at 17:31
  • Like I said, it's rather unlikely, and pretty seldom fringe case, but the only feasible explanation here. Might be an editor misconfiguration. Upload your actual script somewhere, or make a hexdump, and screenshot of a hexdump, else nobody will be able to say. – mario Jan 27 '15 at 17:35
  • Btw, not just the editor, but also FTP tool/server can be at fault. If you're uploading it as TEXT instead of BINARY file, then one of the two parties might decide to convert linebreaks. And obviously, incorrectly at that. – mario Jan 27 '15 at 17:37
  • Tested the plugin on a WordPress site I have on godaddy and it's working fine... – Curtis Blanchette Jan 28 '15 at 01:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/69709/discussion-between-curtis-blanchette-and-mario). – Curtis Blanchette Jan 28 '15 at 05:13

2 Answers2

4

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.

Resource: Filezilla removes line breaks on php files

Community
  • 1
  • 1
0

unexpected $end on line 1

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 CRLF 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".

mario
  • 144,265
  • 20
  • 237
  • 291
  • I've changed my comments to multiline /**/, and now get "Parse error: syntax error, unexpected '{' in /wp-content/plugins/curts-modal-overlay/mailchimp-api/inc/store-address.php on line 1 – Curtis Blanchette Jan 28 '15 at 02:51
  • This has little to do with the hoster, or request types. It's entirely dependent on how the file got mangled during uploads. Use SCP instead of FTP. Without having thre actual uploaded file binary available somewhere (or again: a hexdump!) it's impossible to make further guesses. – mario Jan 28 '15 at 10:58
  • **FIXED** Thanks to @mario I was able to re-think what was causing the error. Which led me to this ... http://stackoverflow.com/questions/4547516/filezilla-removes-line-breaks-on-php-files – Curtis Blanchette Jan 28 '15 at 18:35