<?php
/**
* FastCurlMulti (PHP object-oriented wrapper for {@link http://curl.haxx.se/ cURL} inspired on {@link http://jamessocol.com/projects/oocurl.php OOCurl})
*
* Copyright (c) 2010 Antonio López Vivar
*
* LICENSE:
*
* This library is free software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser General
* Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any
* later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* @category Library
* @package FastCurl
* @author Antonio López Vivar <hide@address.com>
* @copyright 2010 Antonio López Vivar
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @version 0.6
*/
/**
* @property mixed $_mh curl_multi handle.
* @property array $_ch FastCurl objects locked to this container.
* @property bool $_multiset If it's activated, you can set the same CURLOPT to all FastCurl objects making $fastmulti_handle->curlopt=value;
*/
class FastCurlMulti
{
const VERSION='0.6';
private $_mh;
private $_ch;
private $_multiset;
/**
* @param FastCurl $chandle (It can be a single FastCurl object or an array of FastCurl objects)
* @param bool $multiset If it's activated, you can set the same CURLOPT to all FastCurl objects making $fastmulti_handle->curlopt=value;
* @return FastCurlMulti $this
*/
public function __construct($chandle=NULL, $multiset=FALSE)
{
$this->_mh=curl_multi_init();
$this->_ch=array();
if($chandle)
{
if(is_array($chandle))
{
foreach($chandle as $ch)
$this->add($ch);
}
else
$this->add($chandle);
}
$this->_multiset=is_bool($multiset)?$multiset:FALSE;
return $this;
}
/**
* @return bool Set ok
*/
public function set_multiset($value)
{
if(($ret=is_bool($value)))
$this->_multiset=$value;
return $ret;
}
/**
* @return bool
*/
public function get_multiset()
{
return $this->_multiset;
}
/**
* Before closing curl_multi_handle, we remove any FastCurl locked object.
*/
public function __destruct()
{
foreach($this->_ch as $ch)
$this->remove($ch);
curl_multi_close($this->_mh);
}
/**
* @param FastCurl $ch
*/
public function add(FastCurl $ch)
{
$ch->lockMulti($this);
}
/**
* @param FastCurl $ch
*/
public function remove(FastCurl $ch)
{
$ch->unlockMulti($this);
}
/**
* @param FastCurl $ch
*/
public function refresh(FastCurl $ch)
{
$this->remove($ch);
$this->add($ch);
}
/**
* Executes all curl handles parallelly.
*
* @param bool $verbose Prints '+' while looping for all requests (useful to avoid browser's timeout (PHP output buffer must be off)).
*/
public function exec($verbose=FALSE)
{
$active=NULL;
do
{
$mrc=curl_multi_exec($this->_mh, $active);
}while($mrc==CURLM_CALL_MULTI_PERFORM);
while ($active && $mrc==CURLM_OK)
{
if(curl_multi_select($this->_mh)!=-1)
{
do
{
$mrc=curl_multi_exec($this->_mh, $active);
}while($mrc==CURLM_CALL_MULTI_PERFORM);
if($verbose)
echo '+ ';
}
}
}
/**
* @param FastCurl $ch
*/
public function accept(FastCurl $ch)
{
$this->_ch[] = $ch;
curl_multi_add_handle($this->_mh, $ch->get_ch());
}
/**
* @param FastCurl $ch
*/
public function release(FastCurl $ch)
{
if(($key=array_search($ch, $this->_ch, TRUE))!==FALSE)
{
curl_multi_remove_handle($this->_mh, $this->_ch[$key]->get_ch());
unset($this->_ch[$key]);
}
}
/**
* Sets a CURLOPT for every FastCurl object in the container (if _multiset is ON)
*
* @param string $opt
* @param mixed $value
* @return bool CURLOPT set ok
*/
public function __set($opt, $value)
{
if(($ret=($this->_multiset && defined('CURLOPT_'.strtoupper($opt)))))
{
foreach($this->_ch as $ch)
$ch->$opt=$value;
}
return $ret;
}
/**
* @param bool HTML format output
* @param int $rows
* @param int $cols
*/
public function dump($html=TRUE, $rows=50, $cols=100)
{
if($html)
echo '<div><textarea rows="'.(is_int($rows)?$rows:50).'" cols="'.(is_int($cols)?$cols:100).'">';
var_dump($this);
if($html)
echo '</textarea></div>';
}
}
/* ?> */