Uitvoeren van een cronjob werkt niet goed

Status
Niet open voor verdere reacties.

slabbetje

Gebruiker
Lid geworden
5 mei 2007
Berichten
290
HAllo helpers ik ben voor de fun bezig met een Bot bezig voor SocialCity op facebook...
Dit werkt allemaal prima tot ik het zelfde script wat werkt wanneer ik hem handmatig uitvoer alleen niet meer wanneer ik hem in een cronjob zet van 6 minuten...
Dus nu vraag wat kan hier een oorzaak van zijn?

Om even het idee te geven wat het moet doen:
Social city is opgebouwt uit districts (1 t/m 9) hier in kan je een stad bouwen en geld verdienen door contracten op fabrieken te zetten...
Het laagste contract duur 5 minuten en hier verdien je ook het meest aan wanneer je ook echt elke 5 minuten klikt.

PHP:
<?php
///////////////////////////////////////////////
//				BOT BASE
///////////////////////////////////////////////
	$Dir = dirname( __FILE__ );
	$Dir = substr( $Dir, 0, -5 ). "/";

	require( $Dir . "settings.php" );
	require( $Dir . "config.php" );
 
	$DB = new Database();
	$Curl = new Curl();
	$Settings_Sql = $DB->Query("SELECT * FROM `scb_settings` LIMIT 1");
	
	if (isset($Settings_Sql) && $Sql==true)
	{
		$Settings = $Settings_Sql->fetch_array();		
		
		$auth_key = $Settings['auth_key'];
		$auth_id = $Settings['auth_id'];
		$id_contract = $Settings['contract'];
		
///////////////////////////////////////////////
//				STARTING THE BOT
///////////////////////////////////////////////
		
		//Check if the bot is enabled...
		if ($Settings['enabled'] == 0)
		{
			echo "Bot is not enabled<br/>";
			exit();
		}
		//Starting bot message
		echo "Starting the bot...<br/>";
		
		//Set running to 1
		$DB->Query( "UPDATE `scb_settings` SET `running`='1', `lastupdate`='?' WHERE `auth_key`='?'", array( time(), $auth_key ) );
///////////////////////////////////////////////
//				SELECTING THE DISTRICTS
///////////////////////////////////////////////
		echo "Selecting the districts...<br/>";
		
		$District_Sql = $DB->Query("SELECT * FROM `scb_districts` LIMIT 1");
		$District = $District_Sql->fetch_array();
		
		//Array Corrigeren omdat hij 10 waardes laat zien ipv 9...
		$District_total = count($District)-1;
		unset($District[$District_total]);
		
		//Districts ophalen
		foreach ($District as $dKey => $dValue)
		{
			//Corrigeer District ID			
			$id_district = $dKey+1;
			
			//Status variables
			$Stats = Array();
			$Stats[$id_district]['harvest'] = 0;
			$Stats[$id_district]['expired'] = 0;
			$Stats[$id_district]['clean'] = 0;
			$Stats[$id_district]['faclean'] = 0;
			$Stats[$id_district]['population'] = 0;
			$Stats[$id_district]['contracts'] = 0;
			
			
			if ($dValue == 1)
			{
				echo "<br/><b>Starting District ".$id_district."</b><br/>";
				
				$Data = @file_get_contents("http://city-fb-apache-active-vip.playdom.com/j/player/buildings?auth_key=".$auth_key."&auth_id=".$auth_id."&id=".$auth_id."&districtId=".$id_district."");
				$City = @json_decode($Data);
				
///////////////////////////////////////////////
//				DATA VERWERKEN
///////////////////////////////////////////////
				if ( isset($City))
				{
					foreach ($City->data as $Key => $Value)
					{
						$UpdateTime=false;
						$id_building = $Value->buildingId;
						$corner_building = $Value->corner;
						
						//Check if the building exists
						$bQuery = $DB->Query("SELECT * FROM `scb_buildings` WHERE `id_building`='". $id_building . "'");
						if ($bQuery->num_rows > 0)
						{
							$bBuilding = $bQuery->fetch_array();
							$bType = $bBuilding['type_building'];
							$bTime = $bBuilding['waittime_building'];
							$bCost = $bBuilding['cost_building'];
							$bValue = $bBuilding['add_value'];
							
							//
							// KLIK HUIZEN
							//
							if ($bType == "house")
							{
								if ( time() >= ($Value -> growthStart + $bTime) )
								{
									$Curl -> requestPage("http://city-fb-apache-active-vip.playdom.com/j/player/building/reapCivs?auth_key={$auth_key}&auth_id={$auth_id}&corner={$corner_building}&id={$auth_id}&districtId={$id_district}");
									$Curl -> Headers['http_code'];
									
									if ($Curl -> Headers['http_code'] == 200)
									{
										$Stats[$id_district]['population'] += $bValue;
										$UpdateTime=true;
									} else if ($Curl -> Headers == 500)
									{
										//Overbevolkt...
									} else {
										//Gebouw niet klaar...
									}
								}
							}
							
							//
							// KLIK LEISURE
							//
							else if ($bType == "leisure")
							{
								if( time() >= $Value -> leisureStart + $bTime )
								{
									$Stats[$id_district]['clean']++;
									$UpdateTime=true;
									
									@file_get_contents("http://city-fb-apache-active-vip.playdom.com/j/player/building/leisure/clean?auth_key={$auth_key}&auth_id={$auth_id}&corner={$corner_building}&id={$auth_id}&districtId={$id_district}");									
								}
							}
							
							//
							// KLIK FACTORY
							//
							else if ($bType == "factory")
							{
								$Harvested = false;
																
								//Fabrieken die klaar zijn harvesten
								if( $Value -> playerContract != "" && $Value -> playerContract -> endTime < time() )
								{
									$Stats[$id_district]['harvest']++;
									$UpdateTime=true;
									$Harvest=true;
									
									@file_get_contents("http://city-fb-apache-active-vip.playdom.com/j/player/contract/harvest?auth_key={$auth_key}&auth_id={$auth_id}&corner={$corner_building}&id={$auth_id}&districtId={$id_district}");
								}
								
								//Fabrieken die niet clean zijn...
								if ( $Value -> playerContract != "" && $Value -> playerContract -> endTime < time() )
								{
									$Stats[$id_district]['expired']++;
									$UpdateTime=true;
									$Harvest=true;
									@file_get_contents("http://city-fb-apache-active-vip.playdom.com/j/player/contract/clean?auth_key={$auth_key}&auth_id={$auth_id}&corner={$corner_building}&id={$auth_id}&districtId={$id_district}");
								}
								
								//Fabrieken cleanen
								if ( $Value -> state == "dirty" OR $Harvested == true)
								{
									$Stats[$id_district]['faclean']++;
									$UpdateTime=true;
									
									@file_get_contents("http://city-fb-apache-active-vip.playdom.com/j/player/building/clean?auth_key={$auth_key}&auth_id={$auth_id}&corner={$corner_building}&id={$auth_id}&districtId={$id_district}");
								}
								
								//Nieuw contract toevoegen
								if( $Value -> playerContract == "" OR $Harvested == true )
								{
									$Stats[$id_district]['contracts']++;
									$UpdateTime=true;
									
									@file_get_contents("http://city-fb-apache-active-vip.playdom.com/j/player/contract/new?auth_key={$auth_key}&auth_id={$auth_id}&corner={$corner_building}&contractId={$id_contract}&districtId={$id_district}&id={$auth_id}");
								}
								
							}
						}							
					}
				} else {
					echo"District not available";
				}
				
				echo "Contracten opgehaald: " . $Stats[$id_district]['harvest'] . "<br/>";
				echo "Contracten Mislukt: " . $Stats[$id_district]['expired'] . "<br/>";
				echo "Nieuwe contracten toegevoegt: " . $Stats[$id_district]['contracts'] . "<br/>";
				echo "Huizen schoongemaakt: " . $Stats[$id_district]['clean'] . "<br/>";
				echo "Fabrieken schoongemaakt: " . $Stats[$id_district]['faclean'] . "<br/>";
				echo "Populatie groei: " . $Stats[$id_district]['population'] . "<br/>";
				
				$DB->Query("UPDATE `scb_statics` 
				SET 
				`population` = `population`+".$Stats[$id_district]['population'].",
				`contracts` = `contracts`+".$Stats[$id_district]['contracts'].",
				`faclean` = `faclean`+".$Stats[$id_district]['faclean'].",
				`harvest` = `harvest`+".$Stats[$id_district]['harvest'].",
				`expired` = `expired`+".$Stats[$id_district]['expired'].",
				`clean` = `clean`+".$Stats[$id_district]['clean']."
				WHERE 
				`id_district` = '".$id_district."'");
				
				
			}			
		}		
///////////////////////////////////////////////
//				STOP THE BOT AND SHOW STATS
///////////////////////////////////////////////		
		$DB->Query("UPDATE `scb_settings` SET `running` = '0' WHERE `auth_key` = '".$auth_key."'");
	}
 
Laatst bewerkt door een moderator:
Hier op Helpmij dienen we bij een cronjob dit als eerste regel te zetten:
Code:
#!/usr/bin/php
en daarna de rest van je code.

Wellicht is dat de oplossing :)
 
Nee dat is het niet dat is namelijk het vreemde... want gek genoeg werkt het volgende wel...

PHP:
<?php  $Dir = dirname( __FILE__ );
 $Dir = substr( $Dir, 0, -5 ). "/";

 require( $Dir . "config.php" );

 // Classes laden
 $DB = new Database( );
 $Curl = new Curl( );

 $Sql = $DB -> Query( "SELECT `auth_id`, `auth_key`, `enabled`, `contract` FROM `bot_settings` LIMIT 1" );

 if( $Sql )
 {
	$Citydata = $Sql -> fetch_array();
	
	if( $Citydata['enabled'] == "0")
	{
		echo json_encode(
			array(
					"status" => "404",
					"message" => "Bot not enabled"
				)
		);
		exit();
	}
	
	$DB -> Query( "UPDATE `bot_settings` SET `running`='1', `lastupdate`='?' WHERE `auth_key`='?'", array( time(), $Citydata['auth_key'] ) );
	$DB -> Query( "INSERT INTO `bot_logs` (`time`, `message`) VALUES ('?', '?')", array( time(), "Bot started..." ) );
		
	$Data = file_get_contents( "http://city-mobile-tomcat-active-vip.playdom.com/sc_mobile_fb/player_buildings?pickupMessages=false&id=".$Citydata['auth_id']."&auth_id=".$Citydata['auth_id']."&auth_key=".$Citydata['auth_key'] );

	$City = json_decode( $Data );
	//print_r( $City );
	//exit( );
	
	$Status['status'] = "200";
	$Status['message'] = "bot ok";
	
	$Status['stats'] = array();
	$Status['stats']['harvest'] = 0;
	$Status['stats']['clean'] = 0;
	$Status['stats']['faclean'] = 0;
	$Status['stats']['contract'] = 0;
	$Status['stats']['population'] = 0;
	$Status['stats']['expired'] = 0;
	
	// Pre-set config.
	$config['auth']['id'] = $Citydata['auth_id'];
	$config['auth']['auth_id'] = $Citydata['auth_id'];
	$config['auth']['auth_key'] = $Citydata['auth_key'];
	
	// Totals berekenen
	$FactoryTotal = 0;
	
	// Gebouwen ophalen
	foreach( $City -> data AS $Key => $Value )
	{	
		$UpdateTime = false;
		
		$bSql = $DB -> Query( "SELECT `waittime`, `type` FROM `bot_buildings` WHERE `id`='?' LIMIT 1", array( $Value -> buildingId ) );
		if( $bSql -> num_rows != 0 )
		{
			$bBuilding = $bSql -> fetch_array();
			
			if( $bBuilding['type'] == "house" )
			{
				// Huizen ophalen
				if( time() >= $Value -> growthStart + $bBuilding['waittime'] )
				{	
					$Curl -> requestPage( "http://city-fb-apache-active-vip.playdom.com/j/player/building/reapCivs?auth_id=".$config['auth']['auth_id']."&auth_key=".$config['auth']['auth_key']."&auth_time=1274897009&corner=".$Value -> corner."&cacheBuster=9940136&id=".$config['auth']['id']."&districtId=1" );

					$Curl -> Headers['http_code'];
					
					if( $Curl -> Headers['http_code'] == 200 )
					{
						$Status['stats']['population']++;
						$UpdateTime = true;
						
					} elseif( $Curl -> Headers['http_code'] == 500 ) {
						// Overbevolking? wtf!
						
					} else {
						$DB -> Query( "INSERT INTO `bot_logs` (`time`, `message`, `type`) VALUES ('?', '?', '?')", array( time(), "House not ready yet: ".$Value -> corner, "warning" ) );
					}
				}
			} elseif( $bBuilding['type'] == "leisure" )
			{
				// Leisures cleanen
				if( time() >= $Value -> leisureStart + $bBuilding['waittime'] )
				{
					$Curl -> requestPage( "http://city-mobile-backend-vip.playdom.com/j/player/building/leisure/clean?id=".$config['auth']['id']."&corner=".$Value -> corner."&auth_id=".$config['auth']['auth_id']."&auth_key=".$config['auth']['auth_key'] );

					$Curl -> Headers['http_code'];
					
					if( $Curl -> Headers['http_code'] == 200 )
					{
						$Status['stats']['clean']++;
						$UpdateTime = true;
						
					} else {
						$DB -> Query( "INSERT INTO `bot_logs` (`time`, `message`, `type`) VALUES ('?', '?', '?')", array( time(), "Leisure not ready yet: ".$Value -> corner, "warning" ) );
					}
				}
			} elseif( $bBuilding['type'] == "factory" )
			{
				$Harvested = false;
				
				// Contracts ophalen
				if( $Value -> playerContract != "" && $Value -> playerContract -> endTime < time() )
				{
					$contractId = $Citydata['contract'];

					$Curl -> requestPage( "http://city-fb-apache-active-vip.playdom.com/j/player/contract/harvest?auth_id=".$config['auth']['auth_id']."&auth_key=".$config['auth']['auth_key']."&auth_time=1274895559&corner=".$Value -> corner."&id=".$config['auth']['id']."&districtId=1&cacheBuster=216475&token=1274895591647" );

					$Curl -> Headers['http_code'];
					
					if( $Curl -> Headers['http_code'] == 200 )
					{
						$Status['stats']['harvest']++;
						$UpdateTime = true;
							
						$cSql = $DB -> Query( "SELECT `cost`, `income` FROM `bot_contracts` WHERE `id`='".$contractId."'" );
						$cResult = $cSql -> fetch_array();

						$FactoryTotal = $FactoryTotal + ( $cResult['income'] - $cResult['costs'] );
						
						$Harvested = true;
						
					} else {
						$DB -> Query( "INSERT INTO `bot_logs` (`time`, `message`, `type`) VALUES ('?', '?', '?')", array( time(), "Factory not ready yet: ".$Value -> corner, "error" ) );
					}
				}
				
				// Factory's expired cleanen - beta
				if( $Value -> playerContract != "" && $Value -> playerContract -> endTime < time() )
				{
					$Status['stats']['expired']++;
					$UpdateTime = true;

					@file_get_contents( "http://city-fb-apache-active-vip.playdom.com/j/player/contract/clean?auth_key=".$config['auth']['auth_key']."&auth_id=".$config['auth']['id']."&auth_time=1274957371&corner=".$Value -> corner."&districtId=1&id=".$config['auth']['id'] );
				}
				
				
				// Factory's cleanen
				if( $Value -> state == "dirty" OR $Harvested == true )
				{
					$Status['stats']['faclean']++;
					$UpdateTime = true;

					@file_get_contents( "http://city-mobile-tomcat-active-vip.playdom.com/sc_mobile_fb/player_building_clean?id=".$config['auth']['id']."&corner=".$Value -> corner."&auth_id=".$config['auth']['auth_id']."&auth_key=".$config['auth']['auth_key']."&auth_time" );
				}
				
				// Nieuw contract
				if( $Value -> playerContract == "" OR $Harvested == true )
				{
					$Status['stats']['contract']++;
					$UpdateTime = true;
					$contractId = $Citydata['contract'];

					@file_get_contents( "http://city-fb-apache-active-vip.playdom.com/j/player/contract/new?auth_id=".$config['auth']['auth_id']."&auth_key=".$config['auth']['auth_key']."&auth_time=1274896646&contractId=".$contractId."&corner=".$Value -> corner."&id=".$config['auth']['id']."&cacheBuster=8301138&districtId=1" );	
				}
			}
		}
		
		/*
		//Contracten ophalen
		if( $Value -> playerContract != "" && $Time = $Value -> playerContract -> endTime < time() )
		{
			$Status['stats']['harvest']++;
			$UpdateTime = true;
			
			@file_get_contents( "http://city-fb-apache-active-vip.playdom.com/j/player/contract/harvest?auth_id=".$config['auth']['auth_id']."&auth_key=".$config['auth']['auth_key']."&auth_time=1274895559&corner=".$Value -> corner."&id=".$config['auth']['id']."&districtId=1&cacheBuster=216475&token=1274895591647" );
		}
		
		//Gebouwen cleanen
		if( $Value -> leisureStart != "" )
		{
			$Status['stats']['clean']++;
			$UpdateTime = true;
			
			@file_get_contents( "http://city-mobile-backend-vip.playdom.com/j/player/building/leisure/clean?id=".$config['auth']['id']."&corner=".$Value -> corner."&auth_id=".$config['auth']['auth_id']."&auth_key=".$config['auth']['auth_key'] );
		}
		
		// Factory's cleanen
		if( $Value -> state == "dirty" )
		{
			$Status['stats']['faclean']++;
			$UpdateTime = true;
			
			@file_get_contents( "http://city-mobile-tomcat-active-vip.playdom.com/sc_mobile_fb/player_building_clean?id=".$config['auth']['id']."&corner=".$Value -> corner."&auth_id=".$config['auth']['auth_id']."&auth_key=".$config['auth']['auth_key']."&auth_time" );
		}
		
		// Factory's expired cleanen - BETA
		if( $Value -> playerContract != "" && $Time = $Value -> playerContract -> endTime < time() )
		{
			$Status['stats']['expired']++;
			$UpdateTime = true;
			
			@file_get_contents( "http://city-fb-apache-active-vip.playdom.com/j/player/contract/clean?auth_key=".$config['auth']['auth_key']."&auth_id=".$config['auth']['id']."&auth_time=1274957371&corner=".$Value -> corner."&districtId=1&id=".$config['auth']['id'] );
		}
		
		// Nieuw contract
		if( ( $Value -> buildingId == "95" OR $Value -> buildingId == "120" ) && ( $Value -> playerContract == "" ) )
		{
			$Status['stats']['contract']++;
			$UpdateTime = true;
			$contractId = $Citydata['contract'];
			
			@file_get_contents( "http://city-fb-apache-active-vip.playdom.com/j/player/contract/new?auth_id=".$config['auth']['auth_id']."&auth_key=".$config['auth']['auth_key']."&auth_time=1274896646&contractId=".$contractId."&corner=".$Value -> corner."&id=".$config['auth']['id']."&cacheBuster=8301138&districtId=1" );	
		}
		
		// Mensen in stad halen
		if( $Value -> growthStart != "" && $Value -> state == "new" )
		{
			$Status['stats']['population']++;
			$UpdateTime = true;
			
			@file_get_contents( "http://city-fb-apache-active-vip.playdom.com/j/player/building/reapCivs?auth_id=".$config['auth']['auth_id']."&auth_key=".$config['auth']['auth_key']."&auth_time=1274897009&corner=".$Value -> corner."&cacheBuster=9940136&id=".$config['auth']['id']."&districtId=1" );
		}
		*/
		
		/*
		if( $UpdateTime == true )
		{
			$bSql = $DB -> Query( "SELECT `waittime` FROM `bot_buildings` WHERE `id`='?' LIMIT 1", array( $Value -> buildingId ) );
			if( $bSql -> num_rows != 0 )
			{
				$bTime = $bSql -> fetch_array();

				$Time = time() + $bTime['waittime'];
			} else {
				$Time = time();
			}

			//Building time cachen
			$bSql = $DB -> Query( "SELECT `time`, `id`, `buildingtype` FROM `bot_times` WHERE `bid`='?' LIMIT 1", array( $Key ) );
			if( $bSql -> num_rows == 0 )
			{
				$DB -> Query( "INSERT INTO `bot_times` (`bid`, `time`, `buildingtype`) VALUES ('?', '?', '?')", array( $Key, $Time, $Value -> buildingId ) );
			} else {
				$bData = $bSql -> fetch_array();
				$DB -> Query( "UPDATE `bot_times` SET `bid`='?', `time`='?', `buildingtype`='?' WHERE `bid`='?'", array( $bData['id'], $Time, $bData['id'], $Value -> buildingId ) );
			}
		}
		*/
	}
	
	$updatedStats = "";
	
	if( $Status['stats']['contract'] != 0 )
	{
		$updatedStats .= "New contracts: ".$Status['stats']['contract']." ";
	}
	if( $Status['stats']['harvest'] != 0 )
	{
		$updatedStats .= "New harvest: ".$Status['stats']['harvest']." ";
	}
	if( $Status['stats']['faclean'] != 0 )
	{
		$updatedStats .= "Cleaned factorys: ".$Status['stats']['faclean']." ";
	}
	if( $Status['stats']['expired'] != 0 )
	{
		//$updatedStats .= "Expired factorys: ".$Status['stats']['expired']." ";
	}
	if( $Status['stats']['population'] != 0 )
	{
		$updatedStats .= "New population: ".$Status['stats']['population']." ";
	}
	if( $Status['stats']['clean'] != 0 )
	{
		$updatedStats .= "Cleaned buildings: ".$Status['stats']['clean']." ";
	}
	
	if( $updatedStats != "" )
	{
		$DB -> Query( "INSERT INTO `bot_logs` (`time`, `message`) VALUES ('?', '?')", array( time(), "Bot finished: ".$updatedStats ) );
	} else {
		$DB -> Query( "INSERT INTO `bot_logs` (`time`, `message`) VALUES ('?', '?')", array( time(), "Bot finished. No new updates." ) );
	}
	
	$DB -> Query( "UPDATE `bot_settings` SET `running`='0', `factorytotal`=`factorytotal`+'?' WHERE `auth_key`='?'", array( $FactoryTotal, $Citydata['auth_key'] ) );
	
	echo json_encode(
		$Status
	);
 } else {
	echo json_encode(
		array(
				"status" => "404",
				"message" => "Bot not configured"
			)
	);
 } ?>

Dit heeft allen als minpunt dat de districts niet allemaal rond gaan...
 
Lijkt mij vreemd dat dat werkt.

Ik kijk nu eens beter naar de code en zie dat je methode-referenties met "$DB -> Query();" aangeeft.

Voor zover ik weet dienen daar geen spaties tussen te zitten dus in plaats van
PHP:
$DB -> Query();
PHP:
$DB->Query()
 
Nee dat maakt niet meer uit zijn veel mensen die denken dat dit zo maar ik heb er nooit problemen mee gehad en is voor het overzicht toch wel prettiger vind ik

Dus het blijft vreemd dat die cronjob niet word uitgevoerd terwijl dit wel iets is wat gewoon zou moeten werken want handmatig uitvoeren werkt wel gewoon
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan