Posted By

pantuts on 10/25/13


Tagged

email curl php extract address parallel


Versions (?)

AyeEmtract - PHP Class that implements Curl in Parallel to extract Email Addresses


 / Published in: PHP
 

  1. <?php
  2.  
  3. /**
  4.  * AyeEmtract a class that crawls pages and
  5.  * then searches for email addresses using curl in parallel.
  6.  *
  7.  * PHP version >= 5.x
  8.  *
  9.  * @category PHP
  10.  * @author PANTUTS
  11.  * @license http://www.gnu.org/licenses/gpl.txt
  12.  * @link http://www.pantuts.com
  13.  */
  14. class AyeEmtract
  15. {
  16. // variable to store data
  17. private $_results = array();
  18. // array of urls
  19. private $_urls = array();
  20. // addition curl options
  21. private $_options = array();
  22. // errors
  23. public $errors = array();
  24.  
  25. /**
  26.   * Creates object and store array of urls and additional options
  27.   *
  28.   * @param array $urls array of urls
  29.   * @param array $options addition curl options
  30.   */
  31. public function __construct($urls, $options = array())
  32. {
  33. $this->_urls = $urls;
  34. $this->_options = $options;
  35. }
  36.  
  37. /**
  38.   * Sets curl_multi, curl options, and executes curl
  39.   * per url.
  40.   */
  41. public function start()
  42. {
  43. // initialize curl multi handle
  44. $curlMaster = curl_multi_init();
  45. // curl singles array
  46. $curlh = array();
  47.  
  48. // loop all urls
  49. foreach ($this->_urls as $i => $url) {
  50. // curl_init each url
  51. $curlh[$i] = curl_init();
  52.  
  53. curl_setopt_array(
  54. $curlh[$i], array(
  55. CURLOPT_URL => htmlentities(trim($url)),
  56. CURLOPT_SSL_VERIFYPEER => false,
  57. CURLOPT_USERAGENT => self::_setUserAgent(),
  58. CURLOPT_HEADER => false,
  59. CURLOPT_RETURNTRANSFER => true,
  60. CURLOPT_MAXREDIRS => 7,
  61. CURLOPT_CONNECTTIMEOUT => 20,
  62. CURLOPT_FRESH_CONNECT => true )
  63. );
  64.  
  65. // check additional options
  66. if (!empty($this->_options)) {
  67. curl_setopt_array($curlh[$i], $this->_options);
  68. }
  69. // now add multi handle
  70. curl_multi_add_handle($curlMaster, $curlh[$i]);
  71. }
  72.  
  73. // execute multi handles
  74. $running = null;
  75. do {
  76. // save errors if encountered
  77. if (curl_multi_exec($curlMaster, $running) === false) {
  78. $this->errors[$i] = 'ERROR: url = ' . curl_error($curlh[$i]) .
  79. ', code = ' . curl_errno($curlh[$i]);
  80. }
  81. } while ($running > 0);
  82.  
  83. // get content on each url from curlh
  84. foreach ($curlh as $j => $ch) {
  85. // save results and remove handle
  86. $this->_results[$j] = curl_multi_getcontent($ch);
  87.  
  88. curl_multi_remove_handle($curlMaster, $ch);
  89. }
  90.  
  91. // close handle
  92. curl_multi_close($curlMaster);
  93. }
  94.  
  95. /**
  96.   * Returns array of emails
  97.   *
  98.   * @return array_unique(array)
  99.   */
  100. public function getEmails()
  101. {
  102. $emails = array();
  103. // regex to find valid emails
  104. $re = "/([\s]*)([_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*([ ]+|)@([ ]+|)([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,}))([\s]*)/i";
  105.  
  106. foreach ($this->_results as $i => $res) {
  107. // decode htmlentities like %3C
  108. $res = html_entity_decode($res);
  109. preg_match_all($re, $res, $matches);
  110.  
  111. foreach ($matches[0] as $match) {
  112. // save to array found emails
  113. $emails[$i] = trim($match);
  114. }
  115. }
  116. // remove duplicates
  117. return array_unique($emails);
  118. }
  119.  
  120. /**
  121.   * Returns random user-agent
  122.   *
  123.   * @return $ua
  124.   */
  125. private static function _setUserAgent()
  126. {
  127. $userAgents = array(
  128. 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:24.0) Gecko/20100101 Firefox/24.0',
  129. 'Mozilla/5.0 (Windows NT 6.2; rv:22.0) Gecko/20130405 Firefox/22.0',
  130. 'Opera/9.80 (J2ME/MIDP; Opera Mini/5.0 (Windows; U; Windows NT 5.1; en) AppleWebKit/886; U; en) Presto/2.4.15',
  131. 'Opera/9.80 (Windows NT 6.0) Presto/2.12.388 Version/12.14',
  132. 'Mozilla/5.0 (Windows; U; MSIE 9.0; WIndows NT 9.0; en-US))',
  133. 'Mozilla/5.0 (Windows; U; MSIE 7.0; Windows NT 6.0; en-US)',
  134. 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1623.0 Safari/537.36',
  135. 'Mozilla/5.0 (X11; CrOS i686 4319.74.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 Safari/537.36',
  136. 'Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25',
  137. 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.13+ (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2'
  138. );
  139. $ua = $userAgents[array_rand($userAgents)];
  140. return $ua;
  141. }
  142. }
  143. ?>

Report this snippet  

You need to login to post a comment.