Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- *
- *This class will take a URL, grab the page info,
- *use regex to parse out all item numbers and
- *stores into a database with 2 columns - item id, category
- *
- **/
- /**
- *
- *USAGE:
- *$egu = new eBayGetUrls ();
- *$egu->set_details ($category, $url);
- *$egu->set_mysql_details ($host, $user, $pass, $database, $table);
- *$egu->execute_request ();
- *
- **/
- class eBayGetUrls
- {
- private $url = null;
- private $category = null;
- private $mysql = null;
- private $host = null;
- private $user = null;
- private $pass = null;
- private $database = null;
- private $table = null;
- //returns html string
- private function get_html ($url)
- {
- $ch = curl_init ();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- return curl_exec ($ch);
- }
- //returns array of ids
- private function get_item_ids ($html)
- {
- $regex = "/http:\/\/cgi.ebay.com(.*?)\"/i";
- preg_match_all ($regex, $html, $matches);
- $matches = $matches[0];
- $matches = array_unique ($matches);
- unset ($matches[0]);
- $regex = "/[0-9]{8,12}/";
- foreach ($matches as $url)
- {
- if (preg_match ($regex, $url, $match))
- $ids[] = trim ($match[0]);
- else
- die ('Regex is not working properly');
- }
- return array_unique ($ids);
- }
- //sets the object mysql handle
- private function setup_mysql_handle ($host, $user, $pass, $database)
- {
- $handle = mysql_connect ($host, $user, $pass);
- if (!$handle)
- die ("Could not connect");
- mysql_select_db ($database);
- $this->mysql = $handle;
- }
- //inserts info into database
- private function insert_into_db ($handle, $table, $id, $category)
- {
- $query = "INSERT INTO $table (item_id, category) VALUES ('$id', '$category')";
- if (!$handle)
- die ('MySQL Handle is not valid');
- $result = mysql_query ($query, $handle);
- if (!$result)
- die ("Could not insert. Error: " . mysql_error());
- }
- public function execute_request ()
- {
- $html = $this->get_html ($this->url);
- $ids = $this->get_item_ids ($html);
- $this->setup_mysql_handle ($this->host, $this->user, $this->pass, $this->database);
- if (!$this->mysql)
- die ('setup_mysql_function is not working correctly');
- $count = 0;
- foreach ($ids as $id)
- {
- $query = "SELECT * FROM item_id_to_category WHERE item_id = '$id' AND category = '$this->category';";
- $result = mysql_query ($query, $this->mysql);
- if (mysql_fetch_assoc ($result))
- continue;
- $this->insert_into_db ($this->mysql, $this->table, $id, $this->category);
- echo "ID $id inserted.", "<br/>";
- ++$count;
- }
- echo "Total Item Numbers Added: $count";
- }
- public function set_url ($url)
- {
- $this->url = trim ($url);
- }
- public function set_category ($cat)
- {
- $this->category = trim ($cat);
- }
- public function set_details ($cat, $url)
- {
- $this->category = trim ($cat);
- $this->url = trim ($url);
- }
- public function set_mysql_details ($host, $user, $pass, $database, $table)
- {
- $this->host = trim ($host);
- $this->user = trim ($user);
- $this->pass = trim ($pass);
- $this->database = trim ($database);
- $this->table = trim ($table);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement