Advertisement
Guest User

Untitled

a guest
Nov 15th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. <?php
  2.  
  3. include('simple_html_dom.php');
  4.  
  5. ini_set('memory_limit', '-1');
  6. ini_set('max_execution_time', 3600000);
  7.  
  8. $page = 'http://www.getrandomthings.com/';
  9. $html = new simple_html_dom();
  10. $html->load_file($page);
  11.  
  12. $items = $html->find('div[class=caption] a');
  13.  
  14. //db
  15.  
  16. $servername = "localhost";
  17. $username = "root";
  18. $password = "";
  19. $dbname = "getrandomthings";
  20.  
  21. // Create connection
  22. $conn = new mysqli($servername, $username, $password, $dbname);
  23. // Check connection
  24. if ($conn->connect_error) {
  25. die("Connection failed: " . $conn->connect_error);
  26. }
  27.  
  28. /*
  29. $sql = "SELECT id, firstname, lastname FROM MyGuests";
  30. $result = $conn->query($sql);
  31.  
  32. if ($result->num_rows > 0) {
  33. echo "<table><tr><th>ID</th><th>Name</th></tr>";
  34. // output data of each row
  35. while($row = $result->fetch_assoc()) {
  36. echo "<tr><td>".$row["id"]."</td><td>".$row["firstname"]." ".$row["lastname"]."</td></tr>";
  37. }
  38. echo "</table>";
  39. } else {
  40. echo "0 results";
  41. }*/
  42.  
  43.  
  44. // curl
  45. $ch = curl_init();
  46.  
  47. foreach ($items as $k => $item){
  48.  
  49. curl_setopt($ch, CURLOPT_URL,$page.'data/'.$item->href);
  50. curl_setopt($ch, CURLOPT_POST, 1);
  51. curl_setopt($ch, CURLOPT_POSTFIELDS,
  52. "num=100&add=address&unique=true");
  53.  
  54.  
  55. // in real life you should use something like:
  56. // curl_setopt($ch, CURLOPT_POSTFIELDS,
  57. // http_build_query(array('postvar1' => 'value1')));
  58.  
  59. // receive server response ...
  60. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  61.  
  62. $max = 0;
  63. $category = str_replace(' ','_',$item->innertext);
  64.  
  65. if($k >= 39){
  66.  
  67. while (1) {
  68.  
  69. $_check = true;
  70.  
  71. $server_output = curl_exec($ch);
  72.  
  73. $html_child = str_get_html($server_output);
  74. $data = $html_child->firstChild()->children();
  75. foreach ($data as $_item) {
  76. $value = $_item->plaintext;
  77. if ($_item->find('[class=sdes]', 0)) {
  78. $sub_value = $_item->find('[class=sdes]', 0)->innertext;
  79. } else {
  80. $sub_value = '';
  81. }
  82.  
  83. if ($_item->find('img', 0)) {
  84. $image = $_item->find('img', 0)->src;
  85. } else {
  86. $image = '';
  87. }
  88.  
  89. $sql_exist = 'SELECT 1 FROM all_things ats
  90. WHERE ats.value = "' . $value . '"
  91. AND ats.category = "' . $category . '"
  92. LIMIT 1';
  93. $result = $conn->query($sql_exist);
  94.  
  95. if ($result->num_rows == 0) {
  96. $_check = false;
  97. }
  98.  
  99. if ($result->num_rows == 0) {
  100. $sql = 'INSERT INTO all_things (value, category , sub_value, image, origin_image)
  101. VALUES ("' . $value . '", "' . $category . '","' . $sub_value . '","' . $image . '","' . $image . '")';
  102. $h = $conn->query($sql);
  103. // var_dump($sql);
  104. }
  105. }
  106. if ($_check || $max >= 10000) {
  107. break;
  108. }
  109.  
  110. $max += count($data);
  111. }
  112. }
  113. // break;
  114. }
  115.  
  116. echo 'done';
  117.  
  118. curl_close ($ch);
  119.  
  120. $conn->close();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement