[英]Parse error: syntax error, unexpected $end on line 1
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. 当AJAX在本地或GoDaddy WordPress安装上的url时,该插件可以正常工作。 But does not work on my staging site wich is hosted on 'MediaTemple.net'.
但是不能在我的临时站点上工作,它位于“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;
});
<?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 phpVersion 5.5
As I mentioned before this code works on a local environment and a goDaddy hosted site. 正如我之前提到的,此代码适用于本地环境和goDaddy托管站点。 Just not on MediaTemple I have also swept the code for any PHP syntax errors and I can't find anything.
只是没有在MediaTemple上我也扫描了任何PHP语法错误的代码,我找不到任何东西。
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. 该错误是由于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文件的换行符
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. 如果解析器抱怨
line 1
,那么只有当你的php脚本确实只是一行时才会发生这种情况。 The initial <?php
in line 1 couldn't possibly trigger this by itself. 第1行中的初始
<?php
不可能单独触发它。
And the only way for this to occur is for mismatched linebreaks. 而这种情况发生的唯一方法是不匹配的换行符。 PHP cares about LF (
0x0A
) only. PHP只关心LF (
0x0A
)。 If you're developing on classic Mac OS, or an editor which defaults to that, CR ( 0x0D
) might be used for linebreaks however. 如果您在经典Mac OS或默认编辑器上进行开发,则CR (
0x0D
)可能会用于换行。 The old DOS/Windows combo of CR LF would also work. CR LF的旧DOS / Windows组合也可以工作。 But that's not what you have.
但那不是你拥有的。
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.
回车CR不起作用。 PHP will understand the first few statements, but the first comment
// Validate
simply masks the rest of the code. PHP将理解前几个语句,但第一个注释
// Validate
简单地掩盖了其余的代码。 Which is why the opened function declaration leads to a dangling " $end
". 这就是为什么打开的函数声明导致悬挂的“
$end
”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.