解析错误:语法错误,第1行意外$结束

时间:2023-02-10 22:46:52

I'm writing a wordpress plugin that integrates with MailChimp's API to store email addresses in a MailChimp list.

我正在编写一个wordpress插件,它与MailChimp的API集成,用于在MailChimp列表中存储电子邮件地址。

I have a 'store-address.php' that run's via AJAX on the submission of a form.

我有一个'store-address.php'在提交表单时通过AJAX运行。

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'.

该插件适用于AJAX在本地或GoDaddy WordPress安装上的URL。但是对于我在'MediaTemple.net'上托管的临时站点不起作用。

When I make an ajax call to 'store-address.php' I receive this error...

当我对'store-address.php'进行ajax调用时,我收到此错误...

Parse error: syntax error, unexpected { in /wp-content/plugins/plugin-name/mailchimp-api/inc/store-address.php on line 1

解析错误:语法错误,意外{在第1行的/wp-content/plugins/plugin-name/mailchimp-api/inc/store-address.php

Here is my ajax function

这是我的ajax功能

$('#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.

正如我之前提到的,此代码适用于本地环境和goDaddy托管站点。只是没有在MediaTemple上我也扫描了任何PHP语法错误的代码,我找不到任何东西。

Any help or point in the right direction would be a godsend. Thanks

任何正确方向的帮助或观点都是天赐之物。谢谢

2 个解决方案

#1


3  

The error was caused due to FileZilla's transfer type being set to "Auto", which disrupted linebreaks.

该错误是由于FileZilla的传输类型设置为“自动”而导致的,这会中断换行符。

After switching the transfer type to "Binary" and restarting FileZilla, I re-uploaded the plugin and everything works great.

将传输类型切换为“二进制”并重新启动FileZilla后,我重新上传了该插件,一切运行良好。

Resource: Filezilla removes line breaks on php files

资源:Filezilla删除php文件的换行符

#2


0  

unexpected $end on line 1

An unexpected $end indicates a mismatch of { curly braces } and thus unclosed code or control blocks.

意外的$ end表示{花括号}不匹配,因此未关闭的代码或控制块。

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.

如果解析器抱怨第1行,那么只有当你的php脚本确实只是一行时才会发生这种情况。第1行中的初始

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.

而这种情况发生的唯一方法是不匹配的换行符。 PHP只关心LF(0x0A)。如果您在经典Mac OS或默认编辑器上进行开发,则CR(0x0D)可能会用于换行。 CRLF的旧DOS / Windows组合也可以工作。但那不是你拥有的。

In essence, while the code displays correctly in your editor, PHP will see it as:

实质上,当代码在编辑器中正确显示时,PHP会将其视为:

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

就是这样。回车CR不起作用。 PHP将理解前几个语句,但第一个注释// Validate简单地掩盖了其余的代码。这就是为什么打开的函数声明导致悬空的“$ end”。

#1


3  

The error was caused due to FileZilla's transfer type being set to "Auto", which disrupted linebreaks.

该错误是由于FileZilla的传输类型设置为“自动”而导致的,这会中断换行符。

After switching the transfer type to "Binary" and restarting FileZilla, I re-uploaded the plugin and everything works great.

将传输类型切换为“二进制”并重新启动FileZilla后,我重新上传了该插件,一切运行良好。

Resource: Filezilla removes line breaks on php files

资源:Filezilla删除php文件的换行符

#2


0  

unexpected $end on line 1

An unexpected $end indicates a mismatch of { curly braces } and thus unclosed code or control blocks.

意外的$ end表示{花括号}不匹配,因此未关闭的代码或控制块。

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.

如果解析器抱怨第1行,那么只有当你的php脚本确实只是一行时才会发生这种情况。第1行中的初始

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.

而这种情况发生的唯一方法是不匹配的换行符。 PHP只关心LF(0x0A)。如果您在经典Mac OS或默认编辑器上进行开发,则CR(0x0D)可能会用于换行。 CRLF的旧DOS / Windows组合也可以工作。但那不是你拥有的。

In essence, while the code displays correctly in your editor, PHP will see it as:

实质上,当代码在编辑器中正确显示时,PHP会将其视为:

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

就是这样。回车CR不起作用。 PHP将理解前几个语句,但第一个注释// Validate简单地掩盖了其余的代码。这就是为什么打开的函数声明导致悬空的“$ end”。