我已经在并行 plesk 10.4.4 服务器上的 cron 作业中设置了 php 文件。在通知电子邮件中它显示了错误。
syntax error near unexpected token `<'
syntax error near unexpected token `newline'
/newsletter_auto.php: line 2: `<html>'
newsletter_auto.php 文件的代码如下
<title>Market Trend Newsletter</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var abc=encodeURIComponent($("#newsletter").html());
$.ajax({
type:'POST',
url:'newsletter_automation.php?abc='+abc,
success:function(data){
}
});
});
</script>
</head>
<body>
<?php
function checkForNULLString($str)
{
if($str == "" || $str == " " || $str == " " || $str == " " || $str == " " || $str == " " || $str == " " || $str == null)
{
return false;
}
else
{
return true;
}
}
function cleanForDatabase($str)
{
$str = preg_replace('/[^a-zA-Z0-9\s-_#@.,]/', '', $str);
$str = mysql_real_escape_string($str);
return $str;
}
function runSQLQuery($query,$mysqlConnect)
{
$result = mysql_query($query,$mysqlConnect);
if(mysql_errno($mysqlConnect) != 0){
echo "\n !<u>!ERROR:</u>mysqlConnect:".$mysqlConnect." <br/>query:".$query."<br/> Query.result:" . mysql_errno($mysqlConnect) . ": " . mysql_error($mysqlConnect) . "\n";
//Log the Query Error
$result = mysql_query($query,$mysqlConnect); exit();
}
return($result);
}
function insertIconAdminLink($link,$name,$type,$text=NULL,$target="_self")
{
//print"<a href=\"".$link."\" onmouseout=\"Javascript:roll_over_switch('".$name."',false);\" onmouseover=\"Javascript:roll_over_switch('".$name."',true);\"><img name=\"".$name."\" id=\"".$name."\" border=\"0\" src=\"/images/".$type.".gif\" />".$text."</a>";
print" <a class=\"iconLinkText\" href=\"".$link."\" target=\"".$target."\"><img name=\"".$name."\" id=\"".$name."\" border=\"0\" src=\"/images/".$type.".gif\" width=\"24\" height=\"24\" />".$text."</a> ";
}
<div id="newsletter"> <p>Stocks traded <?php echo $stock_direction; ?> today with the <?php echo $stock_index; ?> closing at <?php echo $stock_index_close; ?>, <?php echo $stock_direction; ?> <?php echo $stock_index_difference; ?> or <?php echo $stock_index_percentage_difference; ?> from the previous close. The number of NYSE advances were <?php echo $nyse_advances; ?>, the number of decliners was </div>
</body>
</html>
请告诉我如何解决这个问题?
答案1
如果你在 Bash 脚本解释器下执行 php 模板文件(例如 test.php)时,例如创建一个基本的 php 脚本文件,那么就会出现这样的错误;
$ echo -e "<html>\n</body><?php echo 'test'?></body></html>" > ~/test.php
$ cat test.php
<html>
</body><?php echo 'test'?></body></html>
并使其可执行,然后尝试直接执行它......
$ chmod +x test.php
$ ./test.php
./test.php: line 1: syntax error near unexpected token `newline'
./test.php: line 1: `<html>'
这是因为 shell 试图在默认脚本解释器(即 bash)下运行该脚本,它相当于这个;
$ bash test.php
test.php: line 1: syntax error near unexpected token `newline'
test.php: line 1: `<html>'
至少,像这样的东西至少会在 php 下运行 php 脚本;
$ /usr/bin/php test.php
<html>
</body>test</body></html>