Guest User

Extra Curl 1.3.0

a guest
Jan 4th, 2011
130
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /**
  3.  * Extra LibCurl Class
  4.  *
  5.  * Libcurl does not simplify the use of curl,
  6.  * but merely transfers it to the structure of the OOP,
  7.  * which could be comfortable for code.
  8.  *
  9.  * <code>
  10.  * <?php
  11.  * # examples
  12.  * require_once 'extra.curl.php';
  13.  * $curl = new libcurl;
  14.  * echo $curl->upoint('http://example.com')->exec();
  15.  *
  16.  * $curl->upoint('http://example.com');
  17.  * $curl->and->postfields = 'test=blabla';
  18.  * $curl->and->exec();
  19.  *
  20.  * $curl->point('ch', array('url' => 'http://example.com'));
  21.  * $curl->point('ch', array('url' => 'http://google.com'));
  22.  * $curl->point('ch', array('url' => 'http://yandex.ru'));
  23.  * $curl();
  24.  * </code>
  25.  *
  26.  * @author Mars
  27.  * @version 1.3.0
  28.  * @link http://sourceforge.net/projects/extracurl/
  29.  * @package libcurl_core
  30.  */
  31. class libcurl
  32. {
  33.     /**
  34.      * Information about open points
  35.      * @access public
  36.      */
  37.     public $points = array();
  38.  
  39.     /**
  40.      * <p><b>Create new point</b></p>
  41.      *
  42.      * <p>By default, any point receives additional two parameters
  43.      * (if they have not set) <b>header</b> = 0 and <b>returntransfer</b> = 1</p>
  44.      *
  45.      * @param string|false $pn name of point, if u put false - point will be named automatically
  46.      * @param array|false $pointdata curldata of point, if u put false - curldata will be blank
  47.      * @uses curlpoint Implementation curlpoint class into libcurl
  48.      * @return self
  49.      * @see upoint, ipoint
  50.      */
  51.     public function point($pn=false, $pointdata=false)
  52.     {
  53.         $point_name = (!$pn) ? 'ch'.count($this->points) : $pn;
  54.         $this->$point_name = new curlpoint($pointdata);
  55.         $this->points[] = $point_name;
  56.         $this->and = $this->$point_name;
  57.         return $this;
  58.     }
  59.    
  60.     /**
  61.      * Create new point without specifying name or data,
  62.      * created point will be named automatically
  63.      * @param string $url simple point url
  64.      * @return self
  65.      * @see point, ipoint
  66.      */
  67.     public function upoint($url)
  68.     {
  69.         return $this->point(false, array('url' => $url));
  70.     }
  71.    
  72.     /**
  73.      * Create new point without specifying name,
  74.      * created point will be named automatically
  75.      * @param array $pointdata curldata of point
  76.      * @return self
  77.      * @see point, upoint
  78.      */
  79.     public function ipoint($pointdata)
  80.     {
  81.         return $this->point(false, $pointdata);
  82.     }
  83.    
  84.     /**
  85.      * <p>Feature makes it easier to work with options</p>
  86.      * <p>This function make real name of option from simplicity</p>
  87.      * <p>For CURLINFO_, CURLCLOSEPOLICY_, CURLE_ use info::, closepolicy:: and e:: (eg info:: total_time)</p>
  88.      * @param string $option curl option, without a prefix CURLOPT_ and lowercase
  89.      * @return string
  90.      */
  91.     private function join_option($option)
  92.     {
  93.         switch (strtok($option, '::'))
  94.         {
  95.             case 'info': return constant('CURLINFO_'.strtoupper($option)); break;
  96.             case 'closepolicy': return constant('CURLCLOSEPOLICY_'.strtoupper($option)); break;
  97.             case 'e': return constant('CURLE_'.strtoupper($option)); break;
  98.             default: return constant('CURLOPT_'.strtoupper($option));
  99.         }
  100.     }
  101.    
  102.     /**
  103.      * <p>Execute all opened points and destroy they, except those which have the status of secured</p>
  104.      * <p>For a single point function returns result as string, for several points - as an array</p>
  105.      * @return mixed
  106.      * @see __invoke
  107.      */
  108.     public function exec()
  109.     {
  110.         $cp = count($this->points);
  111.        
  112.         if ($cp == 0)
  113.         {
  114.             return false;
  115.         }
  116.         elseif ($cp == 1)
  117.         {
  118.             $ch = curl_init();
  119.             $point_name = $this->points[0];
  120.             $ps = $this->$point_name->params;
  121.             if ($ps)
  122.             {
  123.                 foreach ($ps as $k => $v)
  124.                 {
  125.                     curl_setopt($ch, $this->join_option($k), $v);
  126.                 }
  127.                 if (!$data = curl_exec($ch))
  128.                 {
  129.                     trigger_error(curl_error($ch));
  130.                 }
  131.             }
  132.             curl_close($ch);
  133.             if (!$this->$point_name->s)
  134.             {
  135.                 unset($this->$point_name);
  136.                 $key = array_search($point_name, $this->points);
  137.                 unset($this->points[$key]);
  138.             }
  139.             //$this->points = array();
  140.             return $data;
  141.         }
  142.         else
  143.         {
  144.             $index = 0;
  145.             $channels = array();
  146.             $return_array = array();
  147.             foreach ($this->points as $point_name)
  148.             {
  149.                 $index++;
  150.                 $chname = 'ch'.$index;
  151.                 $$chname = curl_init();
  152.                 $ps = $this->$point_name->params;
  153.                 foreach ($ps as $k => $v)
  154.                 {
  155.                     curl_setopt($$chname, $this->join_option($k), $v);
  156.                 }
  157.                 if (!$this->$point_name->s)
  158.                 {
  159.                     unset($this->$point_name);
  160.                     $key = array_search($point_name, $this->points);
  161.                     unset($this->points[$key]);
  162.                 }
  163.                 $channels[] = $chname;
  164.             }
  165.            
  166.             $mh = curl_multi_init();
  167.            
  168.             foreach ($channels as $chof)
  169.             {
  170.                 curl_multi_add_handle($mh, $$chof);
  171.             }
  172.            
  173.             $active = null;
  174.            
  175.             do
  176.             {
  177.                 $mrc = curl_multi_exec($mh, $active);
  178.             }   while ($mrc == CURLM_CALL_MULTI_PERFORM);
  179.  
  180.             while ($active && $mrc == CURLM_OK)
  181.             {
  182.                 if (curl_multi_select($mh) != -1)
  183.                 {
  184.                     do
  185.                     {
  186.                         $mrc = curl_multi_exec($mh, $active);
  187.                     }   while ($mrc == CURLM_CALL_MULTI_PERFORM);
  188.                 }
  189.             }
  190.            
  191.             foreach ($channels as $chof)
  192.             {
  193.                 $content = curl_multi_getcontent($$chof);
  194.                 if (!empty($content)) $return_array[] = $content;
  195.                 curl_multi_remove_handle($mh, $$chof);
  196.             }
  197.            
  198.             //$this->points = array();
  199.            
  200.             curl_multi_close($mh);
  201.            
  202.             return $return_array;
  203.         }
  204.     }
  205.    
  206.     /**
  207.      * Parse string with cookies into array
  208.      * @static
  209.      * @return array
  210.      * @see cookies
  211.      */
  212.     public static function parse_cookies($req)
  213.     {
  214.         preg_match_all('/Set-Cookie:\040(.+?)(\n|$)/s', $req, $cookies_requested);
  215.         return (isset($cookies_requested[1])) ? $cookies_requested[1] : false;
  216.     }
  217.    
  218.     /**
  219.      * Parse array with cookies into curlopt string and normalize it
  220.      * @static
  221.      * @return string
  222.      * @see cookies
  223.      */
  224.     public static function prepare_cookies($req)
  225.     {
  226.         $r = trim(preg_replace(array('/((expires|path|domain)=.+?|secure|httponly|Secure|HttpOnly|\S+=deleted)(;|$)/', '/\s\s/'), '',
  227.         str_replace(array("\n", "\r", ';;'), array('', '', ';'), implode('; ', $req))));
  228.         return implode(';', array_unique(explode(';', $r)));
  229.     }
  230.    
  231.     /**
  232.      * Parse string with cookies into curlopt string and normalize it
  233.      * @static
  234.      * @return string
  235.      */
  236.     public static function cookies($req)
  237.     {
  238.         return self::prepare_cookies(self::parse_cookies($req));
  239.     }
  240.    
  241.     /**
  242.      * Execute request and parse string with cookies into curlopt string and normalize it
  243.      * @return string|false
  244.      */
  245.     public function getcookies()
  246.     {
  247.         $cp = count($this->points);
  248.         $this->and->header = 1;
  249.         return ($cp == 1) ? self::prepare_cookies(self::parse_cookies($this->exec())) : false;
  250.     }
  251.      
  252.     /**
  253.      * Synonym of $curl->exec() function.
  254.      * Called by $curl();
  255.      * @return mixed
  256.      * @see exec
  257.      */
  258.     public function __invoke()
  259.     {
  260.         return $this->exec();
  261.     }
  262. }
  263.  
  264. /**
  265.  * Modular CurlPoint Class
  266.  *
  267.  * @package libcurl_core
  268.  * @subpackage libcurl_modular
  269.  * @property array $params read property
  270.  */
  271. class curlpoint
  272. {
  273.     public $params = array();
  274.     public $s = false;
  275.    
  276.     /** Default point constructor */
  277.     public function __construct($pdata)
  278.     {
  279.         $this->add_default_option($pdata, 'header');
  280.         $this->add_default_option($pdata, 'returntransfer', 1);
  281.         $this->add_default_option($pdata, 'timeout', 10);
  282.         $this->add_default_option($pdata, 'ssl_verifypeer');
  283.         $this->add_default_option($pdata, 'ssl_verifyhost');
  284.         $this->set($pdata);
  285.     }
  286.    
  287.     /** Overload __set() function */
  288.     public function __set($name, $value)
  289.     {
  290.         if ($name !== 's') $this->params[$name] = $value;
  291.     }
  292.    
  293.     /** Pointdata default options helper */
  294.     private function add_default_option(&$pdata, $option, $value=0)
  295.     {
  296.         $pdata[$option] = (isset($pdata[$option])) ? $pdata[$option] : $value;
  297.     }
  298.    
  299.     /**
  300.      * Set the class keys by array
  301.      * @param array $pdata
  302.      * @return self
  303.      */
  304.     public function set($pdata)
  305.     {
  306.         foreach ($pdata as $key => $value)
  307.          {
  308.              $this->$key = $value;
  309.          }
  310.         return $this;
  311.     }
  312.    
  313.     /**
  314.      * Set the curlpoint postfields string by array
  315.      * @param array $pdata
  316.      * @return self
  317.      */
  318.     public function post($array)
  319.     {
  320.         $this->postfields = http_build_query($array);
  321.         return $this;
  322.     }
  323.    
  324.     /**
  325.      * Set the curlpoint postfields string by array
  326.      * @param array $pdata
  327.      * @return self
  328.      */
  329.     public function login($login, $password)
  330.     {
  331.         $this->userpwd = $login.':'.$password;
  332.         return $this;
  333.     }
  334.    
  335.     /**
  336.      * Turns on/off secured status of point
  337.      * @param bool $key
  338.      * @return self
  339.      */
  340.     public function secure($key=true)
  341.     {
  342.         $this->s = $key;
  343.         return $this;
  344.     }
  345.    
  346. }
  347.  
  348. /**
  349.  * Easy LibCurl Class
  350.  *
  351.  * <p>Additional extending class of libcurl, for the simplify work with him</p>
  352.  * <ul>
  353.  * <li>easy web browser simulator</li>
  354.  * <li>uses CURLOPT_AUTOREFERER</li>
  355.  * <li>autoadd headers postscript, for the points of parent class</li>
  356.  * </ul>
  357.  * <p>using:</p>
  358.  * <code>
  359.  * <?php
  360.  * $curl = new easy_libcurl;
  361.  * </code>
  362.  *
  363.  * @package libcurl_core
  364.  * @subpackage libcurl_extended
  365.  * @uses libcurl parent class
  366.  * @see libcurl
  367.  * @property array $browser autoadd headers (web browser simulator)
  368.  */
  369. class easy_libcurl extends libcurl
  370. {
  371.     /** Default browser constructor */
  372.     public function __construct()
  373.     {
  374.         $this->http = array();
  375.         $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';
  376.         $this->http[] = 'Accept-Language: ru-ru,ru;q=0.7,en-us;q=0.5,en;q=0.3';
  377.         $this->http[] = 'Pragma: no-cache';
  378.         $this->http[] = 'Keep-Alive: 300';
  379.        
  380.         $this->browser = array(
  381.             'useragent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12',
  382.             'followlocation' => 1,
  383.             'timeout' => 10,
  384.             'autoreferer' => true,
  385.             'returntransfer' => true,
  386.             'header' => true,
  387.             'httpheader' => $this->http,
  388.         );
  389.     }
  390.    
  391.     /** See the {@link libcurl#point()} */
  392.     public function point($pn=false, $pointdata=false)
  393.     {
  394.         return parent::point($pn, array_merge($pointdata, $this->browser));
  395.     }
  396. }
RAW Paste Data