Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Extra LibCurl Class
- *
- * Libcurl does not simplify the use of curl,
- * but merely transfers it to the structure of the OOP,
- * which could be comfortable for code.
- *
- * <code>
- * <?php
- * # examples
- * require_once 'extra.curl.php';
- * $curl = new libcurl;
- * echo $curl->upoint('http://example.com')->exec();
- *
- * $curl->upoint('http://example.com');
- * $curl->and->postfields = 'test=blabla';
- * $curl->and->exec();
- *
- * $curl->point('ch', array('url' => 'http://example.com'));
- * $curl->point('ch', array('url' => 'http://google.com'));
- * $curl->point('ch', array('url' => 'http://yandex.ru'));
- * $curl();
- * </code>
- *
- * @author Mars
- * @version 1.3.0
- * @link http://sourceforge.net/projects/extracurl/
- * @package libcurl_core
- */
- class libcurl
- {
- /**
- * Information about open points
- * @access public
- */
- public $points = array();
- /**
- * <p><b>Create new point</b></p>
- *
- * <p>By default, any point receives additional two parameters
- * (if they have not set) <b>header</b> = 0 and <b>returntransfer</b> = 1</p>
- *
- * @param string|false $pn name of point, if u put false - point will be named automatically
- * @param array|false $pointdata curldata of point, if u put false - curldata will be blank
- * @uses curlpoint Implementation curlpoint class into libcurl
- * @return self
- * @see upoint, ipoint
- */
- public function point($pn=false, $pointdata=false)
- {
- $point_name = (!$pn) ? 'ch'.count($this->points) : $pn;
- $this->$point_name = new curlpoint($pointdata);
- $this->points[] = $point_name;
- $this->and = $this->$point_name;
- return $this;
- }
- /**
- * Create new point without specifying name or data,
- * created point will be named automatically
- * @param string $url simple point url
- * @return self
- * @see point, ipoint
- */
- public function upoint($url)
- {
- return $this->point(false, array('url' => $url));
- }
- /**
- * Create new point without specifying name,
- * created point will be named automatically
- * @param array $pointdata curldata of point
- * @return self
- * @see point, upoint
- */
- public function ipoint($pointdata)
- {
- return $this->point(false, $pointdata);
- }
- /**
- * <p>Feature makes it easier to work with options</p>
- * <p>This function make real name of option from simplicity</p>
- * <p>For CURLINFO_, CURLCLOSEPOLICY_, CURLE_ use info::, closepolicy:: and e:: (eg info:: total_time)</p>
- * @param string $option curl option, without a prefix CURLOPT_ and lowercase
- * @return string
- */
- private function join_option($option)
- {
- switch (strtok($option, '::'))
- {
- case 'info': return constant('CURLINFO_'.strtoupper($option)); break;
- case 'closepolicy': return constant('CURLCLOSEPOLICY_'.strtoupper($option)); break;
- case 'e': return constant('CURLE_'.strtoupper($option)); break;
- default: return constant('CURLOPT_'.strtoupper($option));
- }
- }
- /**
- * <p>Execute all opened points and destroy they, except those which have the status of secured</p>
- * <p>For a single point function returns result as string, for several points - as an array</p>
- * @return mixed
- * @see __invoke
- */
- public function exec()
- {
- $cp = count($this->points);
- if ($cp == 0)
- {
- return false;
- }
- elseif ($cp == 1)
- {
- $ch = curl_init();
- $point_name = $this->points[0];
- $ps = $this->$point_name->params;
- if ($ps)
- {
- foreach ($ps as $k => $v)
- {
- curl_setopt($ch, $this->join_option($k), $v);
- }
- if (!$data = curl_exec($ch))
- {
- trigger_error(curl_error($ch));
- }
- }
- curl_close($ch);
- if (!$this->$point_name->s)
- {
- unset($this->$point_name);
- $key = array_search($point_name, $this->points);
- unset($this->points[$key]);
- }
- //$this->points = array();
- return $data;
- }
- else
- {
- $index = 0;
- $channels = array();
- $return_array = array();
- foreach ($this->points as $point_name)
- {
- $index++;
- $chname = 'ch'.$index;
- $$chname = curl_init();
- $ps = $this->$point_name->params;
- foreach ($ps as $k => $v)
- {
- curl_setopt($$chname, $this->join_option($k), $v);
- }
- if (!$this->$point_name->s)
- {
- unset($this->$point_name);
- $key = array_search($point_name, $this->points);
- unset($this->points[$key]);
- }
- $channels[] = $chname;
- }
- $mh = curl_multi_init();
- foreach ($channels as $chof)
- {
- curl_multi_add_handle($mh, $$chof);
- }
- $active = null;
- do
- {
- $mrc = curl_multi_exec($mh, $active);
- } while ($mrc == CURLM_CALL_MULTI_PERFORM);
- while ($active && $mrc == CURLM_OK)
- {
- if (curl_multi_select($mh) != -1)
- {
- do
- {
- $mrc = curl_multi_exec($mh, $active);
- } while ($mrc == CURLM_CALL_MULTI_PERFORM);
- }
- }
- foreach ($channels as $chof)
- {
- $content = curl_multi_getcontent($$chof);
- if (!empty($content)) $return_array[] = $content;
- curl_multi_remove_handle($mh, $$chof);
- }
- //$this->points = array();
- curl_multi_close($mh);
- return $return_array;
- }
- }
- /**
- * Parse string with cookies into array
- * @static
- * @return array
- * @see cookies
- */
- public static function parse_cookies($req)
- {
- preg_match_all('/Set-Cookie:\040(.+?)(\n|$)/s', $req, $cookies_requested);
- return (isset($cookies_requested[1])) ? $cookies_requested[1] : false;
- }
- /**
- * Parse array with cookies into curlopt string and normalize it
- * @static
- * @return string
- * @see cookies
- */
- public static function prepare_cookies($req)
- {
- $r = trim(preg_replace(array('/((expires|path|domain)=.+?|secure|httponly|Secure|HttpOnly|\S+=deleted)(;|$)/', '/\s\s/'), '',
- str_replace(array("\n", "\r", ';;'), array('', '', ';'), implode('; ', $req))));
- return implode(';', array_unique(explode(';', $r)));
- }
- /**
- * Parse string with cookies into curlopt string and normalize it
- * @static
- * @return string
- */
- public static function cookies($req)
- {
- return self::prepare_cookies(self::parse_cookies($req));
- }
- /**
- * Execute request and parse string with cookies into curlopt string and normalize it
- * @return string|false
- */
- public function getcookies()
- {
- $cp = count($this->points);
- $this->and->header = 1;
- return ($cp == 1) ? self::prepare_cookies(self::parse_cookies($this->exec())) : false;
- }
- /**
- * Synonym of $curl->exec() function.
- * Called by $curl();
- * @return mixed
- * @see exec
- */
- public function __invoke()
- {
- return $this->exec();
- }
- }
- /**
- * Modular CurlPoint Class
- *
- * @package libcurl_core
- * @subpackage libcurl_modular
- * @property array $params read property
- */
- class curlpoint
- {
- public $params = array();
- public $s = false;
- /** Default point constructor */
- public function __construct($pdata)
- {
- $this->add_default_option($pdata, 'header');
- $this->add_default_option($pdata, 'returntransfer', 1);
- $this->add_default_option($pdata, 'timeout', 10);
- $this->add_default_option($pdata, 'ssl_verifypeer');
- $this->add_default_option($pdata, 'ssl_verifyhost');
- $this->set($pdata);
- }
- /** Overload __set() function */
- public function __set($name, $value)
- {
- if ($name !== 's') $this->params[$name] = $value;
- }
- /** Pointdata default options helper */
- private function add_default_option(&$pdata, $option, $value=0)
- {
- $pdata[$option] = (isset($pdata[$option])) ? $pdata[$option] : $value;
- }
- /**
- * Set the class keys by array
- * @param array $pdata
- * @return self
- */
- public function set($pdata)
- {
- foreach ($pdata as $key => $value)
- {
- $this->$key = $value;
- }
- return $this;
- }
- /**
- * Set the curlpoint postfields string by array
- * @param array $pdata
- * @return self
- */
- public function post($array)
- {
- $this->postfields = http_build_query($array);
- return $this;
- }
- /**
- * Set the curlpoint postfields string by array
- * @param array $pdata
- * @return self
- */
- public function login($login, $password)
- {
- $this->userpwd = $login.':'.$password;
- return $this;
- }
- /**
- * Turns on/off secured status of point
- * @param bool $key
- * @return self
- */
- public function secure($key=true)
- {
- $this->s = $key;
- return $this;
- }
- }
- /**
- * Easy LibCurl Class
- *
- * <p>Additional extending class of libcurl, for the simplify work with him</p>
- * <ul>
- * <li>easy web browser simulator</li>
- * <li>uses CURLOPT_AUTOREFERER</li>
- * <li>autoadd headers postscript, for the points of parent class</li>
- * </ul>
- * <p>using:</p>
- * <code>
- * <?php
- * $curl = new easy_libcurl;
- * </code>
- *
- * @package libcurl_core
- * @subpackage libcurl_extended
- * @uses libcurl parent class
- * @see libcurl
- * @property array $browser autoadd headers (web browser simulator)
- */
- class easy_libcurl extends libcurl
- {
- /** Default browser constructor */
- public function __construct()
- {
- $this->http = array();
- $this->http[] = 'Accept: text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1';
- $this->http[] = 'Accept-Language: ru-ru,ru;q=0.7,en-us;q=0.5,en;q=0.3';
- $this->http[] = 'Pragma: no-cache';
- $this->http[] = 'Keep-Alive: 300';
- $this->browser = array(
- 'useragent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12',
- 'followlocation' => 1,
- 'timeout' => 10,
- 'autoreferer' => true,
- 'returntransfer' => true,
- 'header' => true,
- 'httpheader' => $this->http,
- );
- }
- /** See the {@link libcurl#point()} */
- public function point($pn=false, $pointdata=false)
- {
- return parent::point($pn, array_merge($pointdata, $this->browser));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement