Skip to content

Instantly share code, notes, and snippets.

@arnold-almeida
Created September 24, 2009 04:02
Show Gist options
  • Save arnold-almeida/192505 to your computer and use it in GitHub Desktop.
Save arnold-almeida/192505 to your computer and use it in GitHub Desktop.
function request($data , $retry = 1)
{
//Configure::write('debug' , 2);
$start_time_unix = time();
$max_time_unix = time() + $this->request_timeout;
$success = false;
// Are there any servers left to try ???
if(false != $this->select_server())
{
// set the socket host with this active server...
$this->config['host'] = $this->active_server;
// import the Socket lib
App::import('Core', 'HttpSocket');
/**
* While the data has not been found and we are within the time boundary....
*/
while((false == $success) || ($start_time_unix < $max_time_unix))
{
$HttpSocket = new HttpSocket( $this->config );
$this->log('Posting to: '.$this->active_server.$this->settings['request_uri'] , LOG_DEBUG );
$this->log('With Data: '.implode(',' , array_keys($data) ) , LOG_DEBUG );
// Start Timer
$start_time = $this->microtime_float();
// Post,Parse, and fix the response...
$response = $HttpSocket->post( $this->active_server.$this->settings['request_uri'] , $data );
$response = str_replace('Array' , '' , $response ); // Fix the response....
// End time
$end_time = $this->microtime_float();
$this->log('Response' , LOG_DEBUG );
$this->log($response , LOG_DEBUG );
$success = true;
$this->log('Connected' , LOG_DEBUG );
}
// While condition has been met so close the current socket...
$HttpSocket->disconnect();
$total_request_time = $end_time - $start_time;
$msg = 'Request to ['.$this->active_server.'] processed in '.$total_request_time.' seconds';
$this->log( $msg , LOG_DEBUG );
$this->log('Address ['.$this->active_server.'] resolves to '. $HttpSocket->address() , LOG_DEBUG );
if( false == $success)
{
// If we can rety again with another server lets have another crack...
if($retry < $this->max_retry)
{
$retry++;
$this->log( 'Attempting retry '.$retry.' of '.$this->max_retry , LOG_DEBUG );
$this->request( $data , $retry );
}
else
{
$this->log( 'No response after '.$this->max_retry . ' attempts. Returning False' , LOG_DEBUG );
return false;
}
}
else
{
return $response;
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment