2011-09-04 35 views
0

我有一个很奇怪的问题。我的应用程序正在导致我的服务器狂暴。iOS应用程序导致内部服务器错误

我的iOS应用程序使用TFHPPLE解析来自服务器上的PHP页面的数据。它从页面抓取一个名称,并检查该广告系列剩余多少时间。

NSString *URL1 = @"http://alefdev.com/tzedakah/current.php"; 

NSData *webPage = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat: URL1]]]; 

TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:webPage];  
NSArray *elements = [xpathParser search:@"//name"]; 

if([elements count] == 0) 
{ 
    NSLog(@"array is empty"); 

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connection Error" message:@"Cannot connect to server." 
                delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 
    [adupdate invalidate]; 
    rc.enabled = YES; 
    rc.hidden = NO; 
} 
else{ 

    TFHppleElement *element = [elements objectAtIndex:0]; 
    NSString *ttl = [element content]; 
    NSLog(@"Charity Name: %@", ttl); 
    todayscharity.text = ttl; 


}  

每隔10秒钟,它会调用服务器,获取所需信息并将其放入数组中。如果数组为空,则定时器失效,并出现“重新连接”按钮。 '重新连接'按钮重新启动计时器。

定时器:

-(void)starttimer{ 

adupdate = [NSTimer scheduledTimerWithTimeInterval:(10.0) target:self  selector:@selector(updateAd) userInfo:nil repeats:YES]; 

NSLog(@"starttimer called");} 

重启功能

- (IBAction)reconnect:(id)sender 
{ 
[self starttimer]; 
} 

如果失败(空数组),然后重新启动,再次试图从我的服务器抓取信息,但不能。整个站点发出内部服务器错误(500)。

PHP脚本获取当前日期

$today = getdate(); 

$todayfull = $today['year'].$today['mon'].$today['mday']; 

检查日期:

if(($todayfull == 201192)||($todayfull == 201193)) 

然后呼应一些文本和天吻合后运行倒计时功能。

function countdown($year, $month, $day, $hour, $minute) 
{ 
$the_countdown_date = mktime($hour, $minute, 0, $month, $day, $year, -1); 
$today = time(); 
$difference = $the_countdown_date - $today; 
if ($difference < 0) $difference = 0; 

$days_left = floor($difference/60/60/24); 
$hours_left = floor(($difference - $days_left*60*60*24)/60/60); 
$minutes_left = floor(($difference - $days_left*60*60*24 - $hours_left*60*60)/60); 

if($minutes_left < 1) 
{ 
echo "<ends>Less than 1 minute</ends>"; 
} 
else{ 
echo "<ends>".$days_left." day ".$hours_left." hours ".$minutes_left." minutes</ends>"; 
} 

错误日志显示:

[Sun Sep 4 14:01:53 2011] [error] [client 184.100.79.143] File does not exist: /home/alefdev2/public_html/error_500.htm 
[Sun Sep 4 14:01:53 2011] [error] [client 184.100.79.143] Premature end of script headers: /home/alefdev2/public_html/tzedakah/current.php 
[Sun Sep 4 14:01:43 2011] [error] [client 184.100.79.143] File does not exist: /home/alefdev2/public_html/error_500.htm 
[Sun Sep 4 14:01:43 2011] [error] [client 184.100.79.143] Premature end of script headers: /home/alefdev2/public_html/tzedakah/current.php 
[Sun Sep 4 14:01:43 2011] [error] [client 184.100.79.143] File does not exist: /home/alefdev2/public_html/error_500.htm 
[Sun Sep 4 14:01:43 2011] [error] [client 184.100.79.143] Premature end of script headers: /home/alefdev2/public_html/tzedakah/current.php 
[Sun Sep 4 14:00:25 2011] [error] [client 184.100.79.143] File does not exist: /home/alefdev2/public_html/error_500.htm 

完整的PHP脚本是:

<?php 
$today = getdate(); 
$todayfull = $today['year'].$today['mon'].$today['mday']; 
echo "Todayfull: $todayfull"; 

if(($todayfull == 201192)||($todayfull == 201193)) 
{ 
echo "<name>The Lone Soldier Center in memory of Michael Levin</name> 
<description>Lone Soldier Center desc.</description>  
"; 

countdown(2011,9,4,0,0); 
} 
else if(($todayfull == 201194)||($todayfull == 201195)){ 

echo "<name>Friends of Itamar</name> 
<description>Friends of Itamar desc.</description> 
"; 

countdown(2011,9,6,0,0); 
} 

else{ 
echo "Error: Could not match dates."; 
} 
?> 

<?php 
function countdown($year, $month, $day, $hour, $minute) 
{ 
$the_countdown_date = mktime($hour, $minute, 0, $month, $day, $year, -1); 
$today = time(); 

$difference = $the_countdown_date - $today; 
if ($difference < 0) $difference = 0; 

$days_left = floor($difference/60/60/24); 
$hours_left = floor(($difference - $days_left*60*60*24)/60/60); 
$minutes_left = floor(($difference - $days_left*60*60*24 - $hours_left*60*60)/60); 

if($minutes_left < 1) 
{ 
echo "<ends>Less than 1 minute</ends>"; 
} 
else{ 
echo "<ends>".$days_left." day ".$hours_left." hours ".$minutes_left." minutes</ends>"; 
} 

} 
?> 

我的主机(SiteGround)说,我只是有太多的IMAP进程在运行,但该网站只有当我尝试重新连接这个应用程序时才会崩溃。

+0

服务器日志文件在获得500时会说什么?另外,你可以发布完整的服务器端代码吗? – DaveRandom

+0

我已经添加了日志条目和php脚本。 – bfost

回答

0

有两件事情正在进行。首先,你的服务器调用过多,所以我认为你的定时器失效是不正确的。其次,你的PHP脚本没有输出正确的头文件,所以你会得到服务器错误。

+0

感谢您的帮助。计时器继续前进,并不断拨打新电话。我在开始时移动并在应用中使通话无效。我还在php页面的顶部添加了<?xml version = \“1.0 \”encoding = \“UTF-8 \”?>,现在一切似乎都在进行中。谢谢! – bfost

相关问题