<?php

class MultiCurl {
	
	protected $multi_handle;
	protected $success_callbacks;
	protected $failure_callbacks;
	
	public function __construct() {
		$this->multi_handle = curl_multi_init();
		$this->success_callbacks = array();
		$this->failure_callbacks = array();
	}
	
	public function AddHandle( $handle, $success, $failure ) {
		curl_multi_add_handle( $this->multi_handle, $handle );
		$this->success_callbacks[ $handle ] = $success;
		$this->failure_callbacks[ $handle ] = $failure;
	}
	
	public function Exec() {
		$running = null;
		do {
			usleep( 10000 );
			curl_multi_exec( $mh, $running );
			
			while ( $done = curl_multi_info_read( $this->multi_handle ) ) {
				$info = curl_getinfo( $done[ 'handle' ] );
				$output = curl_multi_getcontent( $done[ 'handle' ] );
				if ( $info[ 'http_code' ] == 200 ) {
					$callback = $this->success_callbacks[ $done[ 'handle' ] ];
				} else {
					$callback = $this->failure_callbacks[ $done[ 'handle' ] ];
				}
				$callback( $info, $output );
				curl_multi_remove_handle( $this->multi_handle, $done[ 'handle' ] );
			}
		} while ( $running > 0 );
	}
	
}

?>