php scipt works properly alone, but on ajax call...

Discussion in 'Programming' started by Sleeping Troll, Jun 14, 2009.

  1. #1
    This is my script (don't worry the problem occurs early on!):
    <?php
    require_once('../../Connections/Sites.php');
    mysql_select_db($database, $OrbzOrbz);
    $SQL="Select Count(*) From Twitter";
    $empty[0]=1;
    $Exist=mysql_query($SQL)or die(mysql_error());
    $empty=mysql_fetch_array($Exist);
    //echo "<br>".$empty[0]."<br>";
    if($empty[0]==0){ 
    	$ch = curl_init(); 
    	$timeout = 0; // set to zero for no timeout 
    	curl_setopt ($ch, CURLOPT_URL, "http://twitter.com/statuses/public_timeline.json");
    	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
    	curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 
    	$twitter = curl_exec($ch); 
    	curl_close($ch);
    	$twitterArray = json_decode($twitter);
    	foreach ($twitterArray as $tweet){ 
    		$id = mysql_real_escape_string($tweet->id);
    		$screen_name = mysql_real_escape_string($tweet->user->screen_name); 
    		$text = mysql_real_escape_string($tweet->text);
    		$location = mysql_real_escape_string($tweet->user->location); 
    		$url = mysql_real_escape_string($tweet->user->url); 
    		$profile_image_url = mysql_real_escape_string($tweet->user->profile_image_url);  
    		$description = mysql_real_escape_string($tweet->user->description);  
    		$followers = mysql_real_escape_string($tweet->user->followers_count);  
    		$friends = mysql_real_escape_string($tweet->user->friends_count);
    		$SQL="Insert Into Twitter (`id`, `screen_name` ,`location` ,`url` ,`profile_image_url` ,`description` ,`text` ,`followers` ,`friends`)VALUES ('$id','$screen_name','$location','$url','$profile_image_url','$description','$text','$followers','$friends')";
    		mysql_query($SQL)or die(mysql_error());
    	}
    }
    $SQL="Select * From Twitter limit 0,1";
    $tweets=mysql_query($SQL);
    $tweet=mysql_fetch_row($tweets)or die(mysql_error());
    $SQL="Delete From Twitter Where id = $tweet[0]";
    mysql_query($SQL);
    $Image = imagecreatefromstring(file_get_contents($tweet[4]));
    $imageFile=("../../images/twitter/Orbs/".$tweet[0].".png");
    $Orbback = imagecreatefrompng("../../images/main/OrbBack.png"); 
    $Orbfront = imagecreatefrompng("../../images/main/OrbFront.png"); 
    imagecopymerge($Orbback,$Image,50-(imagesx($Image)/2),50-(imagesy($Image)/2),0,0,imagesx($Image),imagesy($Image),75); 
    imagecopy($Orbback,$Orbfront,0,0,0,0,100,100); 
    imagesavealpha($Orbback, true); 
    imagepng($Orbback,$imageFile);
    $DataFile = "LatestTweet.txt"; 
    $Handle = fopen($DataFile, 'w');
    $Data = ("
    <table border='0'>
    	<tr>
    <img src='$tweet[4]'/>
    	<tr>
    		<td>Screen Name:			</td>
    		<td>$tweet[2]</td>
    	</tr>
    	<tr>
    		<td>Location:				</td>
    		<td>$tweet[5]</td>
    	</tr>
    	<tr>
    		<td>Description:				</td>
    		<td>$tweet[7]</td>
    	</tr>
    	<tr>
    		<td>Tweet:				</td>
    		<td>$tweet[3]</td>
    	</tr>
    	<tr>
    		<td>Followers:				</td>
    		<td>$tweet[8]</td>
    	</tr>
    	<tr>
    		<td>Friends:     </td>
    		<td>$tweet[9]</td>
    	</tr>
    </table>
    ");
    fwrite($Handle, $Data); 
    fclose($Handle); 
    echo ($imageFile."~".$DataFile."~".$tweet[6]);
    ?> 
    PHP:
    in the first part of this script I check to see if 'Twitter' is empty, if it is I "refill" it, if not I move on.
    This works fine if I preview the script in my browser(//echo "<br>".$empty[0]."<br>"; un-commented) but if I call it via ajax it refills on every call! I am thinking it may be because of multiple requests causing a mysql error, hence the "default" value for $empty[0]. but that is not working, I would love to have you visit the site and check it out, however Twitter limits calls to 100/hr and that limit is exceeded in minutes by the script because of this problem. Any ideas? Do you agree it is the multiple ajax requests?

    P.S. This is the calling script, which seems to be the actual culprit.
    function getOrbData(i){//Store new orb data in database.
    	getOrbDataRequest=new Ajax.Request('../php/'+Orb[i].TYPE+'/getOrbData.php',{
    		method:'get',
    		onSuccess:function(Data){
    			OrbData=Data.responseText.split("~");
    			Orb[i].Img.src=OrbData[0];
    			Orb[i].Img.Snapshot=OrbData[1];
    			Orb[i].Img.Link=OrbData[2];
    		}
    	});
    	Orb[i].Img.static=0;
    }
    Code (markup):
     
    Sleeping Troll, Jun 14, 2009 IP
  2. Sleeping Troll

    Sleeping Troll Peon

    Messages:
    217
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Sleeping Troll, Jun 14, 2009 IP