Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. *
  5. *This class will take a URL, grab the page info,
  6. *use regex to parse out all item numbers and
  7. *stores into a database with 2 columns - item id, category
  8. *
  9. **/
  10.  
  11. /**
  12. *
  13. *USAGE:
  14. *$egu = new eBayGetUrls ();
  15. *$egu->set_details ($category, $url);
  16. *$egu->set_mysql_details ($host, $user, $pass, $database, $table);
  17. *$egu->execute_request ();
  18. *
  19. **/
  20.  
  21. class eBayGetUrls
  22. {
  23. private $url = null;
  24. private $category = null;
  25.  
  26. private $mysql = null;
  27. private $host = null;
  28. private $user = null;
  29. private $pass = null;
  30. private $database = null;
  31. private $table = null;
  32.  
  33. //returns html string
  34. private function get_html ($url)
  35. {
  36. $ch = curl_init ();
  37. curl_setopt($ch, CURLOPT_URL, $url);
  38. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  39. return curl_exec ($ch);
  40. }
  41.  
  42. //returns array of ids
  43. private function get_item_ids ($html)
  44. {
  45. $regex = "/http:\/\/cgi.ebay.com(.*?)\"/i";
  46. preg_match_all ($regex, $html, $matches);
  47. $matches = $matches[0];
  48. $matches = array_unique ($matches);
  49. unset ($matches[0]);
  50. $regex = "/[0-9]{8,12}/";
  51. foreach ($matches as $url)
  52. {
  53. if (preg_match ($regex, $url, $match))
  54. $ids[] = trim ($match[0]);
  55. else
  56. die ('Regex is not working properly');
  57. }
  58.  
  59. return array_unique ($ids);
  60. }
  61.  
  62. //sets the object mysql handle
  63. private function setup_mysql_handle ($host, $user, $pass, $database)
  64. {
  65. $handle = mysql_connect ($host, $user, $pass);
  66. if (!$handle)
  67. die ("Could not connect");
  68. mysql_select_db ($database);
  69. $this->mysql = $handle;
  70. }
  71.  
  72. //inserts info into database
  73. private function insert_into_db ($handle, $table, $id, $category)
  74. {
  75. $query = "INSERT INTO $table (item_id, category) VALUES ('$id', '$category')";
  76. if (!$handle)
  77. die ('MySQL Handle is not valid');
  78. $result = mysql_query ($query, $handle);
  79. if (!$result)
  80. die ("Could not insert. Error: " . mysql_error());
  81. }
  82.  
  83. public function execute_request ()
  84. {
  85. $html = $this->get_html ($this->url);
  86. $ids = $this->get_item_ids ($html);
  87.  
  88. $this->setup_mysql_handle ($this->host, $this->user, $this->pass, $this->database);
  89. if (!$this->mysql)
  90. die ('setup_mysql_function is not working correctly');
  91.  
  92. $count = 0;
  93. foreach ($ids as $id)
  94. {
  95. $query = "SELECT * FROM item_id_to_category WHERE item_id = '$id' AND category = '$this->category';";
  96. $result = mysql_query ($query, $this->mysql);
  97. if (mysql_fetch_assoc ($result))
  98. continue;
  99.  
  100. $this->insert_into_db ($this->mysql, $this->table, $id, $this->category);
  101. echo "ID $id inserted.", "<br/>";
  102. ++$count;
  103. }
  104. echo "Total Item Numbers Added: $count";
  105. }
  106.  
  107.  
  108.  
  109. public function set_url ($url)
  110. {
  111. $this->url = trim ($url);
  112. }
  113.  
  114. public function set_category ($cat)
  115. {
  116. $this->category = trim ($cat);
  117. }
  118.  
  119. public function set_details ($cat, $url)
  120. {
  121. $this->category = trim ($cat);
  122. $this->url = trim ($url);
  123. }
  124.  
  125. public function set_mysql_details ($host, $user, $pass, $database, $table)
  126. {
  127. $this->host = trim ($host);
  128. $this->user = trim ($user);
  129. $this->pass = trim ($pass);
  130. $this->database = trim ($database);
  131. $this->table = trim ($table);
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement