DotLibrary
[ class tree: DotLibrary ] [ index: DotLibrary ] [ all elements ]

Source for file Curl.php

Documentation is available at Curl.php

  1. <?php
  2. /**
  3. * DotBoost Technologies Inc.
  4. * DotKernel Application Framework
  5. *
  6. @category   DotKernel
  7. @package    DotLibrary
  8. @copyright  Copyright (c) 2009 DotBoost  Technologies (http://www.dotboost.com)
  9. @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
  10. @version    $Id: Curl.php 152 2010-06-18 07:39:40Z teo $
  11. */
  12.  
  13. /**
  14. * CURL with TOR and country proxy features
  15. @category   DotKernel
  16. @package    DotLibrary
  17. @author     DotKernel Team <team@dotkernel.com>
  18. */
  19. class Dot_Curl
  20. {
  21.     /**
  22.      * User agent
  23.      * @access public
  24.      * @var array 
  25.      */
  26.     public $userAgents = array(
  27.         'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3',
  28.         'Mozilla/5.0 (Windows; U; Windows NT 5.0; rv:1.7.3) Gecko/20040913 Firefox/0.10',
  29.         'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)',
  30.         'Microsoft Internet Explorer/Version (Platform)',
  31.         'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)''Googlebot/2.1 (+http://www.googlebot.com/bot.html)',
  32.         'msnbot/1.0 (+http://search.msn.com/msnbot.htm)',
  33.         'Mozilla/5.0 (compatible; Yahoo! Slurp; http://help.yahoo.com/help/us/ysearch/slurp)'
  34.     );
  35.     /**
  36.      * Used in case you want to specify userAgent
  37.      * @access public
  38.      * @var string 
  39.      */
  40.     public $defaultUserAgent = '';
  41.     /**
  42.      * Used in case you want to bypass tor proxy
  43.      * @access public
  44.      * @var bool 
  45.      */
  46.     public $useTor = true;
  47.     /**
  48.      * Use in case you want to use country proxy feature
  49.      * @access public
  50.      * @var integer 
  51.      */
  52.     public $countryId = 0;
  53.     /**
  54.      * localhost ip
  55.      * @access public
  56.      * @var string 
  57.      */
  58.     public $torIp = '127.0.0.1';
  59.     /**
  60.      * an array with available ports
  61.      * @access public
  62.      * @var array 
  63.      */
  64.     public $ports = array(900090019002900390049005900690079008900990109011901290139014901590169017901890199020);
  65.     /**
  66.      * General settings: sslVerifyPeer
  67.      * @access public
  68.      * @var bool 
  69.      */
  70.     public $sslVerifyPeer = false;
  71.     /**
  72.      * General settings: header
  73.      * @access public
  74.      * @var bool 
  75.      */
  76.     public $header = true;
  77.     /**
  78.      * General settings: returnTransfer
  79.      * @access public
  80.      * @var bool 
  81.      */
  82.     public $returnTransfer = true;
  83.     /**
  84.      * General settings: followLocation
  85.      * @access public
  86.      * @var bool 
  87.      */
  88.     public $followLocation = true;
  89.     /**
  90.      * General settings: timeOut
  91.      * @access public
  92.      * @var int 
  93.      */
  94.     public $timeOut = 200;
  95.     /**
  96.      * Errors , view these in case no data is shown
  97.      * @access public
  98.      * @var array 
  99.      */
  100.     public $errors = array();
  101.     /**
  102.      * Info , stores curl_getinfo() response
  103.      * @access public
  104.      * @var array 
  105.      */
  106.     public $info = array();
  107.     /**
  108.      * Post vars , leave empty if not needed
  109.      * @access public
  110.      * @var array 
  111.      */
  112.     public $postVars = array();
  113.     /**
  114.      * Cookies , leave empty if not needed
  115.      * @access public
  116.      * @var array 
  117.      */
  118.     public $cookies = array();
  119.     /**
  120.      * Set curl options
  121.      * @access private
  122.      * @param object $ch 
  123.      * @param string $url 
  124.      * @param string $referer [optional]
  125.      * @return void 
  126.      */
  127.     private function setOptions($ch$url$referer '')
  128.     {
  129.         //if no referer is provided, use the url as the referer
  130.         if ($referer == ''
  131.         {
  132.             $referer $url;
  133.         }
  134.         //general options
  135.         curl_setopt($chCURLOPT_URL$url);
  136.         curl_setopt($chCURLOPT_HEADER$this->header);
  137.         curl_setopt($chCURLOPT_TIMEOUT$this->timeOut);
  138.         curl_setopt($chCURLOPT_RETURNTRANSFER$this->returnTransfer);
  139.         curl_setopt($chCURLOPT_REFERER$referer);
  140.         curl_setopt($chCURLOPT_SSL_VERIFYPEER$this->sslVerifyPeer);
  141.         //follow redirects , be carefull to se open_basedir to none and that php is not in safe mode
  142.         curl_setopt($chCURLOPT_FOLLOWLOCATION$this->followLocation);
  143.         //get a random user agent
  144.         if ($this->defaultUserAgent != '')
  145.         {
  146.             curl_setopt($chCURLOPT_USERAGENT$this->defaultUserAgent);
  147.         }
  148.         else 
  149.         {
  150.             curl_setopt($chCURLOPT_USERAGENT$this->userAgents[rand(0count($this->userAgents-1)]);
  151.         }
  152.  
  153.         //if useTor is true connection will be done throu tor proxy
  154.         if ($this->useTor)
  155.         {
  156.             //for multy curl we build an array of already used ports to avoid using the same ports more then once
  157.             if (count($this->usedPorts&& count($this->usedPortscount($this->ports))
  158.             {
  159.                 $i 0;
  160.                 $ok false;
  161.                 while (!$ok && $i 20)
  162.                 {
  163.                     $port $this->ports[rand(0(count($this->ports1))];
  164.  
  165.                     if (!in_array($port$this->usedPorts)) 
  166.                     {
  167.                         $ok true;
  168.                     }
  169.                     $i++;
  170.                 }
  171.             }
  172.             //get a random port
  173.             else 
  174.             {
  175.                 $port $this->ports[rand(0(count($this->ports1))];
  176.             }
  177.             $this->usedPorts[$port;
  178.  
  179.             curl_setopt ($chCURLOPT_PROXY$this->torIp.':'.$port);
  180.             curl_setopt ($chCURLOPT_PROXYTYPECURLPROXY_SOCKS5);
  181.         }
  182.  
  183.         if(count($this->cookies0)
  184.         {
  185.             $cc array();
  186.             foreach($this->cookies as $k=>$v$cc[="$k=$v";
  187.             curl_setopt ($chCURLOPT_COOKIEimplode('; ',$cc));
  188.         }
  189.         if(count($this->postVars0)
  190.         {
  191.             curl_setopt ($chCURLOPT_POST1);
  192.             curl_setopt ($chCURLOPT_POSTFIELDS$this->postVars);
  193.         }
  194.     }
  195.     /**
  196.      * Fetch multiple urls at once
  197.      * @access public
  198.      * @param array $urls 
  199.      * @param array $referers [optional]
  200.      * @return array with requests results
  201.      */
  202.     public function getMulti($urls$referers array())
  203.     {
  204.         //reset every time to avoid stacking
  205.         $this->errors = array();
  206.         $this->usedPorts array();
  207.         //initialize multy curl
  208.         $mh curl_multi_init();
  209.         foreach ($urls as $key => $val)
  210.         {
  211.             $url $urls[$key];
  212.             $referer '';
  213.             if (array_key_exists($val$referers))
  214.             {
  215.                 $referer $referers[$key];
  216.             }
  217.             $obj[$keycurl_init($url);
  218.             //set options for each url
  219.             $this->setOptions($obj[$key]$url$referer);
  220.             //add it to the group
  221.             curl_multi_add_handle($mh$obj[$key]);
  222.         }
  223.         //execute all the urls at once
  224.         $running=null;
  225.         do 
  226.         {
  227.             curl_multi_exec($mh,$running);
  228.         }
  229.         while ($running 0);
  230.         //retriev results from each request
  231.         $htmls array();
  232.         foreach ($urls as $key => $val)
  233.         {
  234.             $htmls[$keycurl_multi_getcontent($obj[$key]);
  235.  
  236.             if (curl_errno($obj[$key]!= 0)
  237.             {
  238.                 curl_close($obj[$key]);
  239.                 $this->errors['Connection problem (cURL ERROR: '.curl_errno($obj[$key]).': '.curl_error($obj[$key]).')';
  240.             }
  241.             else 
  242.             {
  243.                 curl_close($obj[$key]);
  244.             }
  245.             curl_multi_remove_handle($mh$obj[$key]);
  246.         }
  247.         //close multy curl
  248.         curl_multi_close($mh);
  249.         //return the array wth all the results
  250.         return $htmls;
  251.     }
  252.     /**
  253.      * Fetch a single url
  254.      * @access public
  255.      * @param string $url 
  256.      * @param string $referer [optional]
  257.      * @return string
  258.      */
  259.     public function getSingle($url$referer '')
  260.     {
  261.         //reset every time to avoid stacking
  262.         $this->errors = array();
  263.         $this->usedPorts array();
  264.         $content '';
  265.  
  266.         $obj curl_init($url);
  267.         $this->setOptions($obj$url$referer);
  268.         if (count($this->errors<= 0)
  269.         {
  270.             $content curl_exec($obj);
  271.             $this->info = curl_getinfo($obj);
  272.             if (curl_errno($obj!= 0)
  273.             {
  274.                 $this->errors['Connection problem (DOT CURL ERROR: '.curl_errno($obj).': '.curl_error($obj).')';
  275.             }
  276.         }
  277.         curl_close($obj);
  278.         return $content;
  279.     }
  280. }

Documentation generated on Wed, 21 Jul 2010 07:34:38 +0000 by phpDocumentor 1.4.3