2012-06-12 29 views
5

Estoy haciendo un archivo php que ejecutará un evento después de que hayan transcurrido cinco minutos. De los documentos, parece que esperar cinco minutos solo requeriría , pero esto no está funcionando. He probado todo el otro código, y funciona bien hasta que agregue la línea sleep.PHP sleep() no funciona

<?php 
/** 
* Twitter App 
* bagelBack.php 
* Takes parameters from $_POST and creates a tweet 
* RKoutnik, 2012 
* Code originally found on http://140dev.com/twitter-api-programming-tutorials/hello-twitter-oauth-php/ 
*/ 

$name = '@'.$_POST['twitterName']; 
$type = $_POST['bagelType']; 

/* BEGIN CONTENT SPINNER TO IMPRESS LYNK */ 
$bagels = array(
    0 => "bagel", 
    1 => "breakfast treat", 
    2 => "doughy food-type item", 
    3 => "round yeast-raised munchie", 
    4 => "doughnut-shaped roll", 
    5 => "hard-crusted treat" 
); 
$finished = array(
    0 => "finished toasting", 
    1 => "completed toasting", 
    2 => "stopped being raw", 
    3 => "concluded the toasting phase", 
    4 => "been sucessfully executed", 
    5 => "been roasted to a crisp" 
); 

$food = $bagels[array_rand($bagels)]; 
$fin = $finished[array_rand($finished)]; 
sleep(300); 
$tweet_text = $name.", Your ".$type." ".$food." has ".$fin; 

$result = post_tweet($tweet_text); 
echo "Response code: " . $result . "\n"; 

function post_tweet($tweet_text) { 

    // Use Matt Harris' OAuth library to make the connection 
    // This lives at: https://github.com/themattharris/tmhOAuth 
    require_once('tmhOAuth.php'); 

    // Set the authorization values 
    // In keeping with the OAuth tradition of maximum confusion, 
    // the names of some of these values are different from the Twitter Dev interface 
    // user_token is called Access Token on the Dev site 
    // user_secret is called Access Token Secret on the Dev site 
    // The values here have asterisks to hide the true contents 
    // You need to use the actual values from Twitter 
    $connection = new tmhOAuth(array(
    'consumer_key' => '[redacted]', 
    'consumer_secret' => '[redacted]', 
    'user_token' => '[redacted]', 
    'user_secret' => '[redacted]', 
    'curl_ssl_verifypeer' => false 
)); 

    // Make the API call 
    $connection->request('POST', 
    $connection->url('1/statuses/update'), 
    array('status' => $tweet_text) 
); 

    return $connection->response['code']; 
} 
?> 
+1

¿Qué quiere decir con que no está funcionando? ¿La secuencia de comandos PHP deja de funcionar por completo cuando tiene la llamada de suspensión()? ¿Duerme, pero no durante cinco minutos? – andrewsi

+0

No funciona en absoluto. No publica nada en Twitter, como debería. – SomeKittens

+1

¿Cuál es su 'max_execution_time' en su php.ini? Tal vez el script se está ejecutando demasiado tiempo y, por lo tanto, existe antes de que se haga nada. – enricog

Respuesta

8

Pruebe agregar set_time_limit(0); en la parte superior del documento. Lo más probable es que esté llegando al "tiempo máximo de ejecución" y que el script finalice.

+0

Parece que solucionará el problema, le avisaré en cinco minutos si funcionó. – SomeKittens

+0

¡Impresionante! Muchas gracias. – SomeKittens

+2

Alternativamente, 'set_time_limit (315);', o 300 + cualquiera que sea el tiempo de ejecución máximo esperado. Si por alguna razón los procesos se atascan, es mejor no agotar su mesa de proceso por accidente. – ghoti